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

tback: make Traceback printing compatible with Python 3 #246

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
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
16 changes: 4 additions & 12 deletions kobo/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,21 +510,13 @@ def upload_task_log(self, file_obj, task_id, remote_file_name, append=True, mode
self._hub.worker.upload_task_log(task_id, remote_file_name, mode, chunk_start, chunk_len, chunk_checksum, encoded_chunk)


from six.moves.xmlrpc_client import Fault
import sys

from xmlrpc.client import Fault

# default implementation of Fault.__repr__ is:
# "<Fault %s: %s>" % (self.faultCode, repr(self.faultString))
# repr of string does not escape newlines ('\n') and produces very ugly output
# so using direct string is much nicer for users
# repr of string escapes everything (e.g. newlines) and produces a very ugly
# output so using it directly is much nicer for users
def fault_repr(self):
fault = self.faultString
if sys.version_info[0] > 2 and fault.startswith("b\'Traceback"):
# properly deserialize tracebacks
import ast
fault = ast.literal_eval(fault).decode("unicode-escape")

return "<Fault %s: %s>" % (self.faultCode, str(fault))
return "<Fault %s: %s>" % (self.faultCode, self.faultString)

Fault.__repr__ = fault_repr
6 changes: 3 additions & 3 deletions kobo/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ def new_func(*args, **kwargs):
import datetime
import kobo.shortcuts
import kobo.tback
date = datetime.datetime.strftime(datetime.datetime.now(), "%F %R:%S").encode()
data = b"--- TRACEBACK BEGIN: " + date + b" ---\n"
date = datetime.datetime.strftime(datetime.datetime.now(), "%F %R:%S")
data = "--- TRACEBACK BEGIN: " + date + " ---\n"
data += kobo.tback.Traceback().get_traceback()
data += b"--- TRACEBACK END: " + date + b" ---\n\n\n"
data += "--- TRACEBACK END: " + date + " ---\n\n\n"
kobo.shortcuts.save_to_file(log_file, data, append=True)
raise
return new_func
13 changes: 1 addition & 12 deletions kobo/tback.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,8 @@ def get_traceback(self):
obj_value_str = self._to_str(obj_value, "%.200r")
result.append("%20s = %s" % ("self." + self._to_str(obj_key), obj_value_str))
result.append("</LOCALS>")
s = u''

for i in result:
line = i.replace(r'\\n', '\n').replace(r'\n', '\n')

if type(i) == six.text_type:
s += i
else:
s += six.text_type(str(i), errors='replace')
s += '\n'

return s.encode('ascii', 'replace')
return '\n'.join(result)

def print_traceback(self):
"""Print a traceback string to stderr."""
Expand Down Expand Up @@ -270,7 +260,6 @@ def _hook(exctype, value, tb):
tback.show_locals = True
logger and logger.error(tback.get_traceback())
tback.print_traceback()
print()

_hook.__doc__ = sys.excepthook.__doc__
_hook.__name__ = sys.excepthook.__name__
Expand Down
2 changes: 1 addition & 1 deletion kobo/worker/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def run(self):
if self._logger:
msg = "\n".join([
"Fatal error in LoggingThread",
kobo.tback.Traceback().get_traceback().decode(),
kobo.tback.Traceback().get_traceback(),
])
self._logger.log_critical(msg)
raise
Expand Down
2 changes: 1 addition & 1 deletion kobo/worker/taskmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def run_task(self, task_info):
message = "ERROR: %s\n" % kobo.tback.get_exception()
message += "See traceback.log for details (admin only).\n"
hub.upload_task_log(BytesIO(message.encode()), task.task_id, "error.log")
hub.upload_task_log(BytesIO(kobo.tback.Traceback().get_traceback()), task.task_id, "traceback.log", mode=0o600)
hub.upload_task_log(BytesIO(kobo.tback.Traceback().get_traceback().encode()), task.task_id, "traceback.log", mode=0o600)
failed = True
finally:
thread.stop()
Expand Down
22 changes: 10 additions & 12 deletions tests/test_tback.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@


import re
import six
import unittest

from kobo.tback import get_traceback, Traceback
Expand All @@ -20,40 +19,39 @@ def test_text(self):
try:
raise Exception('Simple text')
except:
str_regexp = re.compile(r'Traceback \(most recent call last\):\n *File ".*test_tback.py", line .+, in test_text\n *raise Exception\(\'Simple text\'\)\n *Exception: Simple text', re.M)
bytes_regexp = re.compile(rb'Traceback \(most recent call last\):\n *File ".*test_tback.py", line .+, in test_text\n *raise Exception\(\'Simple text\'\)\n *Exception: Simple text', re.M)
regexp = re.compile(r'Traceback \(most recent call last\):\n *File ".*test_tback.py", line .+, in test_text\n *raise Exception\(\'Simple text\'\)\n *Exception: Simple text', re.M)

self.assertRegex(get_traceback(), str_regexp)
self.assertRegex(get_traceback(), regexp)
tb = Traceback(show_traceback = True, show_code = False, show_locals = False, show_environ = False, show_modules = False)
self.assertRegex(tb.get_traceback(), bytes_regexp)
self.assertRegex(tb.get_traceback(), regexp)

def test_Traceback(self):
try:
raise Exception('Simple text')
except:
tb = Traceback(show_traceback = False, show_code = False, show_locals = False, show_environ = False, show_modules = False)
self.assertEqual(b'', tb.get_traceback())
self.assertEqual('', tb.get_traceback())
tb.show_code = True
self.assertRegex(tb.get_traceback(), re.compile(rb'<CODE>.*--> *\d+ *raise Exception.*<\/CODE>$', re.M | re.S))
self.assertRegex(tb.get_traceback(), re.compile(r'<CODE>.*--> *\d+ *raise Exception.*<\/CODE>$', re.M | re.S))
tb.show_code = False
tb.show_locals = True
self.assertRegex(tb.get_traceback(), re.compile(rb'<LOCALS>.*tb = .*<\/LOCALS>$', re.M | re.S))
self.assertRegex(tb.get_traceback(), re.compile(r'<LOCALS>.*tb = .*<\/LOCALS>$', re.M | re.S))
tb.show_locals = False
tb.show_environ = True
self.assertRegex(tb.get_traceback(), re.compile(rb'<ENVIRON>.*<\/ENVIRON>\n<GLOBALS>.*</GLOBALS>$', re.M | re.S))
self.assertRegex(tb.get_traceback(), re.compile(r'<ENVIRON>.*<\/ENVIRON>\n<GLOBALS>.*</GLOBALS>$', re.M | re.S))
tb.show_environ = False
tb.show_modules = True
self.assertRegex(tb.get_traceback(), re.compile(rb'<MODULES>.*<\/MODULES>$', re.M | re.S))
self.assertRegex(tb.get_traceback(), re.compile(r'<MODULES>.*<\/MODULES>$', re.M | re.S))

def test_encoding(self):
try:
a = ''.join([chr(i) for i in range(256)])
b = u''.join([unichr(i) for i in range(65536)])
b = b''.join([chr(i).encode() for i in range(65536)])
raise Exception()
except:
tb = Traceback(show_code = False, show_traceback = False)
output = tb.get_traceback()
self.assertIsInstance(output, six.binary_type)
self.assertIsInstance(output, str)

def test_uninitialized_variables(self):
class Foo(object):
Expand Down
Loading