Opened 5 years ago

Last modified 3 years ago

#8044 enhancement new

twisted.python.failure.Failure traceback printing is not unicode friendly

Reported by: posita Owned by:
Priority: normal Milestone: Python-3.x
Component: core Keywords:
Cc: Branch:
Author:

Description

To print a traceback to an in-memory buffer for inclusion in your own stuff, you currently have to do something like this:

buf = io.StringIO() if sys.version_info >= (3, 0) else io.BytesIO()
failure.printTraceback(buf, *args, **kw)
tb_str = buf.getvalue() if sys.version_info >= (3, 0) else buf.getvalue().decode('ascii') # or possibly 'latin_1'?

This is because tracebacks constructed in Python 2 aren't Unicode and tracebacks constructed in Python 3 are.

It should probably be the case that all tracebacks are Unicode, reducing the above (in either environment) to:

buf = io.StringIO()
failure.printTraceback(buf, *args, **kw)
tb_str = buf.getvalue()

#5948 is already marked as fixed, hence this issue.

Attachments (1)

unicode_exception_example.diff (917 bytes) - added by Pawel 5 years ago.

Download all attachments as: .zip

Change History (4)

comment:1 Changed 5 years ago by posita

Keywords: py3k added; py3 removed

comment:2 Changed 5 years ago by Pawel

This is actually also bug because currently you can't raise non-ascii in exception message in python 2, see attached (failing) unit test. If you have non-ascii in exception traceback failure.printTraceback() breaks in reflect._safeFormat().

It should probably be the case that all tracebacks are Unicode, reducing the above (in either environment) to:

hmm if all tracebacks will be unicode code that uses Python 2.7 and BytesIO will break because BytesIO does not support writing unicode (doing buf.write(u"łał") fails with TypeError: 'unicode' does not have the buffer interface). I suspect there are users around there who used BytesIO and they will suffer. Is that ok?

Last edited 5 years ago by Pawel (previous) (diff)

Changed 5 years ago by Pawel

comment:3 Changed 3 years ago by Craig Rodrigues

Keywords: py3k removed
Note: See TracTickets for help on using tickets.