Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

py3: use format_exc instead of print_exception #3078

Merged
merged 2 commits into from Jan 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions dvc/logger.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Manages logging configuration for dvc repo."""

import io
import traceback
import logging.config
import logging.handlers

Expand Down Expand Up @@ -101,34 +101,24 @@ def _description(self, message, exception):
return description.format(message=message, exception=exception)

def _walk_exc(self, exc_info):
import traceback

buffer = io.StringIO()

traceback.print_exception(*exc_info, file=buffer)

exc = exc_info[1]
tb = buffer.getvalue()

exc_list = [str(exc)]
tb_list = [tb]

# NOTE: parsing chained exceptions. See dvc/exceptions.py for more info
while hasattr(exc, "__cause__") and exc.__cause__:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, what about __context__?

Copy link
Author

@ghost ghost Jan 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Suor , __context__? We are not using __context__, and that should be the matter of another PR, I guess 😕

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python 3 automatically assigns __context__ when you raise an exception while handling exception. So if you don't use either from exc or from None you still can show the original cause, this is usually an error in an error handler, which we probably want to see.

exc_list.append(str(exc.__cause__))
if hasattr(exc, "cause_tb") and exc.cause_tb:
tb_list.insert(0, str(exc.cause_tb))
exc = exc.__cause__

return exc_list, tb_list
return exc_list

def _parse_exc(self, record):
tb_only = getattr(record, "tb_only", False)

if not record.exc_info:
return (None, "")

exc_list, tb_list = self._walk_exc(record.exc_info)
exc_list = self._walk_exc(record.exc_info)
tb = traceback.format_exception(*record.exc_info)

exception = None if tb_only else ": ".join(exc_list)

Expand All @@ -139,7 +129,7 @@ def _parse_exc(self, record):
red=colorama.Fore.RED,
nc=colorama.Fore.RESET,
line="-" * 60,
stack_trace="\n".join(tb_list),
stack_trace="".join(tb),
)
else:
stack_trace = ""
Expand Down