[New-bugs-announce] [issue35103] format_exception() doesn't work with PyErr_Fetch

Tomáš Heran report at bugs.python.org
Mon Oct 29 04:59:54 EDT 2018


New submission from Tomáš Heran <tomas.heran at oracle.com>:

When embedding Python into a C/C++ based programs, one of the ways to access the formatted traceback is/was to get the type, value and tb by calling the C API PyErr_Fetch() and then call native Python traceback.format_exception() function to get a list of strings representing the tracback.

This worked fine in 2.7.

Doing the same in 3.5 doesn't work because:

1. PyErr_Fetch()'s middle output argument 'value' is not an instance of Exception (or a subclass), but rather (what seems to me) a __str__ or __repr__ (i.e. the type is str) of said raised exception. 

An aside - this seems to be the case both in 2.7 and 3.5, but differs (in both 2.7 and 3.5, I believe) from what sys.exc_info() returns, as there the middle output 'value' is of an Exception (or a subclass) type.

2. In the overhauled traceback module in 3.x, functions like format_exception() now use an internal class TracebackException which expects the exc_value (which it gets from format_exception()'s value argument) to be an instance of Exception (or a subclass), thus feeding traceback.format_exception() whatever I got from PyErr_Fetch() ends up throwing another exception.

As a temporary workaround, I can use traceback.format_tb() which luckily doesn't use the TracebackException class and works fine in 3.5.

----------
messages: 328796
nosy: tomasheran
priority: normal
severity: normal
status: open
title: format_exception() doesn't work with PyErr_Fetch
versions: Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35103>
_______________________________________


More information about the New-bugs-announce mailing list