Saturday 15 January 2011

Tracking a Python function's global accesses -



Tracking a Python function's global accesses -

i able track global variables python function accesses. first effort involved creating subclass of dict logged calls __getitem__ , __setitem__, , constructing new function globals.

this sorta-kinda works in python 3 (although still haven't gotten builtins work properly). however, looks like doesn't work @ in python 2, because cpython calls pydict_setitem() straight on globals instead of pyobject_setitem().

what's best way of doing same thing in python 2? have inspect bytecode load_global instructions, or there improve way?

edit: 1 disadvantage of load_global approach don't observe whether function accesses global variables--for instance, if global foo accessed in 1 branch, approach detailed above can tell if branch executed, whereas bytecode-reading won't. disadvantage won't able grab variable accesses through globals()[bar]. these minor purposes, nice solve them.

python python-2.7 cpython

No comments:

Post a Comment