关于SystemError: com_backpatch: 偏移量过大的深入分析
在Python中,当执行以下代码生成的内容时,会出现错误信息"SystemError: com_backpatch: offset too large"
。
f = open("test.py", "w")
f.write("def fn():\n a =1000\n")
for a in xrange(3000):
if a == 0:
f.write(" if a == "+str(a)+": \n print "+str(a)+"\n")
else:
f.write(" elif a == "+str(a)+": \n print "+str(a)+"\n")
f.close()
import test
很明显,如果长度声明超过了某个限制,就会出现这个错误。
有没有人能详细解释一下这个错误的原因呢?
3 个回答
0
仅供参考,这段脚本在我使用的一个Debian测试版的系统上运行得很好,这个系统是32位的用户空间和内核,使用的是Python 2.5.4版本。
$ ls -ln
total 4
-rw-r--r-- 1 1000 1000 270 2009-12-23 02:53 gentest.py
$ python gentest.py
$ ls -ln
total 216
-rw-r--r-- 1 1000 1000 270 2009-12-23 02:53 gentest.py
-rw-r--r-- 1 1000 1000 111799 2009-12-23 02:58 test.py
-rw-r--r-- 1 1000 1000 93299 2009-12-23 02:58 test.pyc
$ uname -srvmo
Linux 2.6.30-2-486 #1 Thu Dec 3 23:32:25 UTC 2009 i686 GNU/Linux
$ python --version
Python 2.5.4
0
看起来你遇到了Python解释器的限制。这个问题是因为从“if”开始到结束的距离太远了,可能是因为偏移量被限制在16位。你如果把“elif”改成“if”,这个问题就解决了。
你需要缩短“if/elif”链的长度。
希望这对你有帮助。
...richie
2
根据这个链接:http://www.cgl.ucsf.edu/pipermail/chimera-dev/2007/000404.html
Python的字节码编译器有一个限制,就是在跳转指令中,最大只能使用16位的偏移量。这意味着你在一个条件代码块中,不应该有超过64K字符的内容。
更多细节可以查看这里:http://www.mail-archive.com/python-list@python.org/msg72631.html