Python 符号表的限制?
我一直在研究一些将python(osx上的2.7)文件作为配置文件加载的案例。我想看看循环运行execfile时的行为是什么。我预计会发生内存不足错误或大量交换,但当我得到不同的结果时,我感到相当惊讶 我设置了一个测试场景,如下所示: “d”python脚本,包含:Python 符号表的限制?,python,Python,我一直在研究一些将python(osx上的2.7)文件作为配置文件加载的案例。我想看看循环运行execfile时的行为是什么。我预计会发生内存不足错误或大量交换,但当我得到不同的结果时,我感到相当惊讶 我设置了一个测试场景,如下所示: “d”python脚本,包含: #!/usr/bin/python x = 0 execfile("d1") #!/usr/bin/python x += 1 print "x = %d" % x execfile("d2") #!/usr/bin/pytho
#!/usr/bin/python
x = 0
execfile("d1")
#!/usr/bin/python
x += 1
print "x = %d" % x
execfile("d2")
#!/usr/bin/python
x += 1
print "x = %d" % x
execfile("d1")
“d1”python脚本,包含:
#!/usr/bin/python
x = 0
execfile("d1")
#!/usr/bin/python
x += 1
print "x = %d" % x
execfile("d2")
#!/usr/bin/python
x += 1
print "x = %d" % x
execfile("d1")
“d2”python脚本,包含:
#!/usr/bin/python
x = 0
execfile("d1")
#!/usr/bin/python
x += 1
print "x = %d" % x
execfile("d2")
#!/usr/bin/python
x += 1
print "x = %d" % x
execfile("d1")
结果是:
$ ./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'
$/d
x=1
x=2
x=3
... 为简洁起见删除。。。
x=997
x=998
x=999
回溯(最近一次呼叫最后一次):
文件“/d”,第5行,在
执行文件(“d1”)
文件“d1”,第5行,在
执行文件(“d2”)
文件“d2”,第5行,在
执行文件(“d1”)
... 为简洁起见删除。。。
文件“d1”,第5行,在
执行文件(“d2”)
文件“d2”,第5行,在
执行文件(“d1”)
文件“d1”,第5行,在
执行文件(“d2”)
KeyError:“未知符号表项”
我只是好奇是否有人能解释这里发生了什么?为什么它在执行execfile~1000次后会停止?来自Python源代码,
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's hash, or that some error
* (suppressed) occurred when comparing keys in the dict's 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.
*/
查找符号时发生的任何错误(包括内存不足错误)都将转换为KeyError
。这可能是一个bug