Two curious errors when function globals are manipulated

Steven D'Aprano steve at pearwood.info
Tue Jul 5 10:46:21 EDT 2016


I've come across two curious behaviours of a function using custom globals.
In both cases, I have a function where __globals__ is set to a ChainMap.

In the first, the builtin __import__ appears to both exist and not-exist at
the same time: I can see the __import__ builtin, but the `import` statement
fails with:

    ImportError: __import__ not found


In the second, I can use `global` to declare a name in the global scope,
apparently assign to it, but then the name doesn't exist.

Attached is a single demo script which demonstrates the problems, for Python
3.


I think both of these are bugs, or at least they are bugs if
function.__globals__ is allowed to be set to something other than the
module globals.

Have I missed something? Is there documentation that says I shouldn't
replace __globals__ with another dict?

Is there a bug in my code?

If not, I think these are both bugs. Does anyone disagree?



-- 
Steven




More information about the Python-list mailing list