Hooking Mechanism when Entering and Leaving a Try Block

Sven R. Kunze srkunze at mail.de
Tue Aug 11 23:47:43 CEST 2015


Hi everybody,

is there something like a hook that a Python module could register to in 
order to 'trace' the entering and leaving of arbitrary try blocks?


What is this good for?

I am the maintainer of https://pypi.python.org/pypi/xfork . A package 
for converting a classic sequential program to a parallel one.
In order to make sure exception handling basically works the same way as 
in the sequential implementation, it would be great if I could hook into 
try blocks.


Consider this:

try:
     return 25+fork(b)
except:
     return 11
finally:
     print('finished')


25+fork(b) is a ResultProxy. That kind of object is basically wraps up 
operators and evaluate them lazily.

In order to preserve sequential/classic exception handling, I would like 
to evaluate all open/not-already-evaluated result proxies as soon as I 
enter or leave a try: block.


Why when leaving try:?
Result proxies might raise exceptions once they evaluate. So, result 
proxies evaluated outside of their origin try: block, might get caught 
somewhere else by the wrong exception handler.

Why when entering try:?
Result proxies might raise exceptions once they evaluate. So, result 
proxies from before the try: might raise exceptions which in turn might 
be caught inside the try: by the wrong exception handler.

Best,
Sven


PS: I already looked into the source of CPython and discovered these places:

https://github.com/python/cpython/blob/d95bb1adefc7/Python/symtable.c#L947symtable_enter_block
https://github.com/python/cpython/blob/d95bb1adefc7/Python/symtable.c#L931 
symtable_exit_block

Unfortunately, I cannot find anything specific to exception handling in 
the source.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150811/fa38ab42/attachment.html>


More information about the Python-list mailing list