6

I don't understand the below behavior.

  • How does locals() result in a new reference?
  • Why doesn't gc.collect remove it? I didn't assign the result of locals() anywhere.

x

import gc

from sys import getrefcount

def trivial(x): return x

def demo(x):
    print getrefcount(x)
    x = trivial(x)
    print getrefcount(x)
    locals()
    print getrefcount(x)
    gc.collect()
    print getrefcount(x)


demo(object())

The output is:

$ python demo.py 
3
3
4
4
2
  • My guess is that the local variable dict of a function call is created the first time it's needed and then cached. I don't know the Python internals well enough to know if Python actually does this, though. Mar 8, 2014 at 0:20
  • @user2357112: Your guess is correct, proven in my answer.
    – bukzor
    Mar 8, 2014 at 19:58

3 Answers 3

6

This has to do with 'fast locals' which are stored as a matched pair of tuples for fast integer indexing (one for names f->f_code->co_varnames, one for values f->f_localsplus). When locals() is called, the fast-locals are converted into a standard dict and tacked onto the frame structure. The relevant bits of the cpython code are below.

This is the implementing function for locals(). It does little more than call PyEval_GetLocals.

static PyObject *
builtin_locals(PyObject *self)
{
    PyObject *d;

    d = PyEval_GetLocals();
    Py_XINCREF(d);
    return d;
}   

In turn PyEval_GetLocals does little more than call PyFrame_FastToLocals.

PyObject *
PyEval_GetLocals(void)
{   
    PyFrameObject *current_frame = PyEval_GetFrame();
    if (current_frame == NULL)
        return NULL;
    PyFrame_FastToLocals(current_frame);
    return current_frame->f_locals;
}

This is the bit that allocates a plain-old dictionary for the frame's local variables and stuffs any "fast" variables into it. Since the new dict is tacked onto the frame structure (as f->f_locals), any "fast" variables get an extra reference upon a call to locals().

void
PyFrame_FastToLocals(PyFrameObject *f)
{
    /* Merge fast locals into f->f_locals */
    PyObject *locals, *map;
    PyObject **fast;
    PyObject *error_type, *error_value, *error_traceback;
    PyCodeObject *co;
    Py_ssize_t j;
    int ncells, nfreevars;
    if (f == NULL)
        return;
    locals = f->f_locals;
    if (locals == NULL) {
        /* This is the dict that holds the new, additional reference! */
        locals = f->f_locals = PyDict_New();  
        if (locals == NULL) {
            PyErr_Clear(); /* Can't report it :-( */
            return;
        }
    }
    co = f->f_code;
    map = co->co_varnames;
    if (!PyTuple_Check(map))
        return;
    PyErr_Fetch(&error_type, &error_value, &error_traceback);
    fast = f->f_localsplus;
    j = PyTuple_GET_SIZE(map);
    if (j > co->co_nlocals)
        j = co->co_nlocals;
    if (co->co_nlocals)
        map_to_dict(map, j, locals, fast, 0);
    ncells = PyTuple_GET_SIZE(co->co_cellvars);
    nfreevars = PyTuple_GET_SIZE(co->co_freevars);
    if (ncells || nfreevars) {
        map_to_dict(co->co_cellvars, ncells,
                    locals, fast + co->co_nlocals, 1);
        /* If the namespace is unoptimized, then one of the
           following cases applies:
           1. It does not contain free variables, because it
              uses import * or is a top-level namespace.
           2. It is a class namespace.
           We don't want to accidentally copy free variables
           into the locals dict used by the class.
        */
        if (co->co_flags & CO_OPTIMIZED) {
            map_to_dict(co->co_freevars, nfreevars,
                        locals, fast + co->co_nlocals + ncells, 1);
        }
    }
    PyErr_Restore(error_type, error_value, error_traceback);
}
2
  • Nice! Where'd you find these functions? I've had a hard time figuring out where most of the built-in functions are implemented. Mar 8, 2014 at 22:41
  • @user2357112 My manager is a cpython guru and pasted them to me =X That said, bltinmodule.c implements __builtins__ and frameobject.c implements python's frame object.
    – bukzor
    Mar 9, 2014 at 0:24
2

I added a few prints to your demo code :

#! /usr/bin/python

import gc

from sys import getrefcount

def trivial(x): return x

def demo(x):
    print getrefcount(x)
    x = trivial(x)
    print getrefcount(x)
    print id(locals())
    print getrefcount(x)
    print gc.collect(), "collected"
    print id(locals())
    print getrefcount(x)


demo(object())

The output is then (on my machine):

3
3
12168320
4
0 collected
12168320
4

locals() actually creates a dict containing a ref on x, thus the ref inc. gc.collect() does not collect the locals dict, you can see it by printing the id, it's the same object returned twice, it is somehow memoized for this frame, thus not collected.

-1

This is because locals() creates an actual dictionary and puts x inside it hence, increasing the reference count of x, this dictionary is probably cached.

So I changed the code by adding two lines

import gc

from sys import getrefcount

def trivial(x): return x

def demo(x):
   print getrefcount(x)
   x = trivial(x)
   print getrefcount(x)
   print "Before Locals ",  gc.get_referrers(x)
   locals()
   print "After Locals ",  gc.get_referrers(x)
   print getrefcount(x)
   gc.collect()
   print getrefcount(x)
   print "After garbage collect", gc.get_referrers(x)

demo(object())   

and here is the output of the code

3
3
Before Locals  [<frame object at 0x1f1ee30>]
After Locals  [<frame object at 0x1f1ee30>, {'x': <object object at 0x7f323f56a0c0>}]
4
4
After garbage collect [<frame object at 0x1f1ee30>, {'x': <object object at 0x7f323f56a0c0>}]

Seems like it is caching the dict value even after garbage collection for future calls to locals().

4
  • 2
    The OP knows that locals does this. The issue is that the dict and its reference would be expected to vanish, since no explicit reference to the dict is stored. Mar 8, 2014 at 0:21
  • Also, locals() doesn't say it creates a dict. Since the local namespace is just a mapping from names to values, I'd expect a dict to already exist. The fact that editing the return value of locals() edits the local namespace as well matches this intuition.
    – bukzor
    Mar 8, 2014 at 0:30
  • 3
    Ned Batcheler, coverage.py's maintainter, wrote about locals "The locals() function is trickier than it appears at first glance. The returned value is a dictionary which is a copy of the local symbol table. This is why changing the dict might not actually change the local variables."
    – ddelemeny
    Mar 8, 2014 at 0:48
  • 3
    @bukzor: what you expect and what CPython actually does are two different things ;-p. The fact that local variables are not stored in a dict in CPython is, and I think I'm quoting Tim Peters, one of the most important optimizations in it. Mar 8, 2014 at 1:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.