New submission from Jason R. Coombs: I'm writing a routine that captures exceptions and logs them to a database. In doing so, I encountered a situation that when parsing a Unicode file that has an IndentationError (SyntaxError), print_exc will fail when it tries to render the unicode line. Here's a script that replicates the failure.
# coding: utf-8 from __future__ import unicode_literals import io import sys import traceback PY3 = sys.version_info > (3,) print(sys.version) buffer = io.StringIO() if PY3 else io.BytesIO() try: args = str('<tokenize>'), 7, 2, ' // test' raise IndentationError('failed', args) except Exception: traceback.print_exc(file=buffer) And the output $ python2 test-unicode-tb.py 2.7.13 (default, Dec 24 2016, 21:20:02) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] Traceback (most recent call last): File "test-unicode-tb.py", line 19, in <module> traceback.print_exc(file=buffer) File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/traceback.py", line 233, in print_exc print_exception(etype, value, tb, limit, file) File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/traceback.py", line 128, in print_exception _print(file, line, '') File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/traceback.py", line 13, in _print file.write(str+terminator) TypeError: 'unicode' does not have the buffer interface The same test runs without error on Python 3. It's surprising to me that I'm the first person to encounter this issue. Is it possible I'm abusing the tokenize module and a unicode value shouldn't be present in the args for the IndentationError? ---------- messages: 289592 nosy: jason.coombs priority: normal severity: normal status: open title: TypeError in traceback.print_exc - unicode does not have the buffer interface versions: Python 2.7 _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue29809> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com