views:

88

answers:

1

What is the Python API equivalent of PyErr_Print(), from the C interface?

I'm assuming a call in either the sys, or traceback modules, but can't find any functions therein that make calls to PyErr_Print().

Addendum

I'm after the Python call to get the same functionality as PyErr_PrintEx(), described as:

Print a standard traceback to sys.stderr and clear the error indicator.

That is I want to make the Python call that has this effect.

+2  A: 

There's no Python function that's exactly equivalent to PyErr_PrintEx (the real name of PyErr_Print;-), including for example setting sys.last_traceback and friends (which are only supposed to be set to help a post-mortem debugging from the interactive interpreter for exceptions which have not been caught). What exact combination of functionality are you looking for?

Alex Martelli
I just noticed the slight discrepancy, I'm after the printing and clearing bit.
Matt Joiner
Only way to "clear [[the error]] bit" in Python code is to catch the exception in an `except` clause of a `try/except` statement (sometimes this can be implicit, in the `__exit__` method of the context manager in a `with` statement).
Alex Martelli
ok, what about the printing part, what is the closest call? `print_exc`?
Matt Joiner
Yep, `traceback.print_exc` is the closest analog.
Alex Martelli