[Python-Dev] Nested scopes core dump

Michael Hudson mwh21@cam.ac.uk
19 Mar 2001 23:14:08 +0000


Ka-Ping Yee <ping@lfw.org> writes:

> I just tried this:
> 
>     Python 2.1b1 (#15, Mar 16 2001, 04:31:43) 
>     [GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
>     Type "copyright", "credits" or "license" for more information.
>     >>> from __future__ import nested_scopes
>     >>> def f(x):
>     ...     x = x + 1
>     ...     a = x + 3
>     ...     b = x + 5
>     ...     def g(y):
>     ...         def h(z):
>     ...             return a, b, x, y, z
>     ...         return h
>     ...     return g
>     ...
>     Fatal Python error: non-string found in code slot
>     Aborted (core dumped)

Here, look at this:

static int
symtable_freevar_offsets(PyObject *freevars, int offset)
{
      PyObject *name, *v;
      int pos;

      /* The cell vars are the first elements of the closure,
         followed by the free vars.  Update the offsets in
         c_freevars to account for number of cellvars. */  
      pos = 0;
      while (PyDict_Next(freevars, &pos, &name, &v)) {
              int i = PyInt_AS_LONG(v) + offset;
              PyObject *o = PyInt_FromLong(i);
              if (o == NULL)
                      return -1;
              if (PyDict_SetItem(freevars, name, o) < 0) {
                      Py_DECREF(o);
                      return -1;
              }
              Py_DECREF(o);
      }
      return 0;
}

this modifies the dictionary you're iterating over.  This is, as they
say, a Bad Idea[*].

https://sourceforge.net/tracker/index.php?func=detail&aid=409864&group_id=5470&atid=305470

is a minimal-effort/impact fix.  I don't know the new compile.c well
enough to really judge the best fix.

Cheers,
M.

[*] I thought that if you used the same keys when you were iterating
    over a dict you were safe.  It seems not, at least as far as I
    could tell with mounds of debugging printf's.
-- 
  (Of course SML does have its weaknesses, but by comparison, a
  discussion of C++'s strengths and flaws always sounds like an
  argument about whether one should face north or east when one
  is sacrificing one's goat to the rain god.)         -- Thant Tessman