Raising exceptions during call back

Donn Cave donn at oz.net
Tue May 30 01:29:59 EDT 2000


Quoth "Makhno" <mak at imakhno.freeserve.co.uk>:
[quoting Martin von Loewis]
|> When you get an exception in a C module, immediately release all
|> resources, and return NULL as well. That way, the entire stack will
|> wind up, and Python will print the full traceback.
|
| I wanted to avoid this because the error can occur in many places and it's
| non-trivial to change my code to return NULL everytime, espcially as my
| module is scripting-language independant. And I was uncertain if it'd
| actually work propelly anyway given how traceback.print_stack() had worked.
|
|> Alternatively, if you want to print the backtrace in the module, use
|> PyErr_Print().
|
| Yes, I found this worked as it should. Followed by exit(1) it does what I
| need.

If you return 0 right away, like the man said, you'll get your full
trace complete with line number and expression where the error occurred.

If you exit after printing an error, then obviously there's no way for
the program to recover from an error.  It's fine if you like it that
way and this module is for you.  For general use by others, it's much
better to play along with the exception system, or at least provide
conspicuous disclaimers that this module doesn't conform to the normal
Python behavior.

	Donn Cave, donn at oz.net



More information about the Python-list mailing list