[Python-porting] Strange behavior of eval() in Python 3.1
Georg Brandl
g.brandl at gmx.net
Sun Aug 29 18:20:35 CEST 2010
Am 29.08.2010 17:16, schrieb Bo Peng:
>> Note that this specific issue only appears because you're using eval().
>> For static code, your access to "a" outside of the comprehension's scope
>> will be recognized and a proper closure generated.
>
> Thank you very much for your explanation and now I have a real problem
> to fix for my Python extension module. More specifically, I call
> "eval(expression, module_dict, user_dict)" to evaluate expressions in
> user_dict. However, to evaluate expressions such as "[a[x] for x in
> a]", I would need "eval(expression, user_dict, user_dict)" to make 'a'
> available in globals. But then I do not have access to names in
> module_dict, and simple expressions such as 'len(a)' would fail
> because I am doing all these from the C/C++ level and there is no
> __builtins__ in user_dict.
Surely you can do
namespace = module_dict.copy()
namespace.update(user_dict)
eval(..., namespace)
?
Georg
--
Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
Four shall be the number of spaces thou shalt indent, and the number of thy
indenting shall be four. Eight shalt thou not indent, nor either indent thou
two, excepting that thou then proceed to four. Tabs are right out.
More information about the Python-porting
mailing list