function local namespace question

Diez B. Roggisch deets at nospam.web.de
Thu Jul 9 08:54:25 EDT 2009


Dave Angel wrote:

> Paul LaFollette wrote:
>> Kind people,
>>
>> Using Python 3.1 under FreeBSD and WinXP.
>>
>> I've been tearing my hair out trying to solve this myself, but I need
>> to ask for help.   I want (for obscure reasons) to be able to log
>> transactions in the namespace(s) of a script.  Specifically I would
>> like to log creation of identifiers, changes in the binding of
>> identifiers ("assignment") and lookups.  This turns out to be pretty
>> easy in the global and local namespaces... I simply subclass dict,
>> override the appropriate operations to include the logging operations
>> I want, and then exec the code using my dictionaries as the global and
>> local namespaces.  All  of this  works just dandy until I try to
>> extend it to functions.
>>
>> I cannot figure out any way to get a hook into the local namespace of
>> a user defined function.  I have tried making a wrapper class that
>> grabs the function call and then uses exec to invoke
>> myfunction.__code__ with my own dictionaries.  This runs the (no
>> argument) function properly (losing the return value, but I can deal
>> with that) but never accesses the local logging dictionary that I
>> specify in the exec() call. Perhaps the local namespace of a function
>> is not a dict at all?
>>
>> Anyway, is there any way (however clumsy) to do what I want to do?

You might consider using the trace-functionality of python, exposed throug

 sys.settrace

It's very expensive of course to do that.

Diez



More information about the Python-list mailing list