Ben Roberts added the comment:

Attached is a patch that adds a 'global' kwarg to the Timeit constructor, which 
does pretty much what it says on the tin: specifies a global namespace that 
exec() will use.

I originally had a 'locals' arg as well (to mirror the signature of eval/exec), 
but realized that the local vars I was passing to exec were not available to 
the inner function.  Reason: the timeit module compiles/execs a *closure*, and 
closed-over variables and exec() simply do not play nicely.  Possible 
workarounds were to munge locals() into the globals() dict, or to somehow 
inject the variables in the locals dict into the closure.  I found neither of 
these options superior to simply not including a locals argument, for reasons 
of Least Surprise and maintainability.

Patch includes some basic tests and documentation.  I am particularly 
uncomfortable with writing docs so those very likely need some polish.

----------
keywords: +patch
Added file: http://bugs.python.org/file36409/timeit_global_arg.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue2527>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to