How to use self-inspection to check for try-block

elmar at cmbi.ru.nl elmar at cmbi.ru.nl
Fri Mar 20 04:32:12 EDT 2009


Hi everyone,

is there a sufficiently easy possibility for a Python function to find
out whether it has been called from a try-block or not?

try:
  print "Calling foo"
  foo()
except:
  print "Got exception"

In the example above, foo() should be able to 'see' that it was called
from a try block, allowing it to behave differently.

Can this information be obtained from the traceback/frame/code
objects, or is that too difficult?

Many thanks for your help,
Elmar



More information about the Python-list mailing list