How do functions get access to builtins?

Steven D'Aprano steve+comp.lang.python at
Thu Jan 24 12:31:06 CET 2013

Rouslan Korneychuk wrote:

> I found the answer in Python's source code. When you execute a code
> object, PyFrame_New is called which gets 'bultins' from 'globals', but
> inside PyFrame_New (defined on line 596 of Objects/frameobject.c) is the
> following (line 613):
>    builtins = PyDict_GetItem(globals, builtin_object);
> Unlike PyObject_GetItem, PyDict_GetItem is specialized for dict objects.
> Your ChainedDict class uses ChainMaps's storage and leaves dict's
> storage empty, so PyDict_GetItem doesn't find anything.
> Interestingly: it looks like it could be fixed easily enough. Unless
> there are other places where globals is assumed to be a dict object, it
> would just be a matter of doing the same check and fallback in
> PyFrame_New that is done in LOAD_GLOBAL (technically, you could just use
> PyObject_GetItem; obviously, this is an optimization).

Thanks for the reply Rouslan.

Perhaps I should report this as a bug.


More information about the Python-list mailing list