Two curious errors when function globals are manipulated

eryk sun eryksun at gmail.com
Tue Jul 5 11:37:42 EDT 2016


On Tue, Jul 5, 2016 at 3:27 PM, eryk sun <eryksun at gmail.com> wrote:
> ChainMap implements the MutableMapping abstract base class. But
> CPython needs globals to be a dict. In the current implementation,
> LOAD_GLOBAL calls _PyDict_LoadGlobal, and STORE_GLOBAL calls
> PyDict_SetItem. They don't fall back on the abstract object APIs.

Sorry, I was replying from memory, but partly mixed up 2.x and 3.x. In
CPython 3.6, LOAD_GLOBAL does fall back on PyObject_GetItem, but
STORE_GLOBAL still does not. The latter is short enough to quote:

        TARGET(STORE_GLOBAL) {
            PyObject *name = GETITEM(names, oparg);
            PyObject *v = POP();
            int err;
            err = PyDict_SetItem(f->f_globals, name, v);
            Py_DECREF(v);
            if (err != 0)
                goto error;
            DISPATCH();
        }



More information about the Python-list mailing list