[Python-Dev] Accessing globals without dict lookup

Skip Montanaro skip@pobox.com
Mon, 11 Feb 2002 08:39:35 -0600


    Ping> All right -- i have attempted to diagram a slightly more
    Ping> interesting example, using my interpretation of Guido's scheme.

Very nice.  One case I would like to see covered is that of a global that is
deleted.  Something like:


    import eggs

    i = -2
    max = 3
    j = 4

    def foo(n):
        y = abs(i) + max
        return eggs.ham(y + n)

    del j

I presume there would still be an entry in spam's module dict with a NULL
objptr.

The whole think makes sense to me if it avoids the possible two
PyDict_GetItem calls in the LOAD_GLOBAL opcode.  As I understand it, if
accessed inside a function, LOAD_GLOBAL could be implemented something like
this:

    case LOAD_GLOBAL:
        cell = func_cells[oparg];
        if (cell.objptr) x = cell->objptr;
        else x = cell->cellptr->objptr;
        if (x == NULL) {
            ... error recovery ...
            break;
        }
        Py_INCREF(x);
        continue;

This looks a lot better to me (no complex function calls).

What happens in the module's top-level code where there is presumably no
func_cells array?  Do we simply have two different opcodes, one for use at
the global level and one for use in functions?

Skip