Limit character table?

I was looking for some corner cases for loading python files (2.7 on osx) as configuration files. I wanted to see what this behavior is if I looped through execfile. I was expecting a memory error or a big replacement, but I was pretty surprised when I got a different result.

I install the test script as follows:

'd' python script with:

#!/usr/bin/python
x = 0
execfile("d1")

'd1' python script with:

#!/usr/bin/python
x += 1
print "x = %d" % x
execfile("d2")

'd2' python script with:

#!/usr/bin/python
x += 1
print "x = %d" % x
execfile("d1")

Result:

$ ./d
x = 1
x = 2
x = 3
... removed for brevity ...
x = 997
x = 998
x = 999
Traceback (most recent call last):
  File "./d", line 5, in <module>
    execfile("d1")
  File "d1", line 5, in <module>
    execfile("d2")
  File "d2", line 5, in <module>
    execfile("d1")
... removed for brevity ...
  File "d1", line 5, in <module>
    execfile("d2")
  File "d2", line 5, in <module>
    execfile("d1")
  File "d1", line 5, in <module>
    execfile("d2")
KeyError: 'unknown symbol table entry'

I was just curious if there was anyone who could explain what was going on here? Why does it stop after execfile ~ 1000 times?

+4
source share
1 answer

From Python source code Objects/dictobject.c:

/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
 * that may occur (originally dicts supported only string keys, and exceptions
 * weren't possible).  So, while the original intent was that a NULL return
 * meant the key wasn't present, in reality it can mean that, or that an error
 * (suppressed) occurred while computing the key hash, or that some error
 * (suppressed) occurred when comparing keys in the dict internal probe
 * sequence.  A nasty example of the latter is when a Python-coded comparison
 * function hits a stack-depth error, which can cause this to return NULL
 * even if the key is present.
 */

, PyDict_GetItem() . ... Python/symtable.c,

v = PyDict_GetItem(st->st_blocks, k);
if (v) {
    assert(PySTEntry_Check(v));
    Py_INCREF(v);
}
else {
    PyErr_SetString(PyExc_KeyError,
                    "unknown symbol table entry");
}

, ( ), KeyError. , .

+7

All Articles