推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
jiushengyuan
V2EX  ›  Python

求助, Python 解释器 byterun 的代码,运行报错

  •  
  •   jiushengyuan · Aug 7, 2019 · 2550 views
    This topic created in 2608 days ago, the information mentioned may be changed or developed.

    代码在刚刚开始时就跑不起来了,源代码在这里,就两个文件 __main__.py 和 pyvm2.py 。

    以下是我写到文件里,作为要编译的源代码

    def f():
        print(1)
    

    先用 python3.7.2 跑,在报错信息是:

    File "e:\PythonCode\500lines\interpreter\pyvm2.py", line 181, in parse_byte_and_args
    arg = f.code_obj.co_consts[arg_val]
    IndexError: tuple index out of range

    调试能看到 arg_val 是 25600,f.code_obj.co_consts 是长度为 3 的列表,所以 index out of range。但是不懂为什么。

    网上很少讨论,搜达一处说可能是 python3 的字节码有变。

    不确定这是否是问题原因,我用 python2.7.16 跑,还没 python3 执行的多呢。

    报错信息是:

    File "e:\PythonCode\500lines\interpreter\pyvm2.py", line 175, in parse_byte_and_args
    byte_name = dis.opname[byteCode]
    TypeError: list indices must be integers, not str

    调试看到,byteCode 是 'd'

    不知道如何解决了,大佬们帮忙看看鸭


    FYI:在遇到上述问题前,另外有一些报错

    对于 python3, with open(filename, 'rU') as f: 报错,可换成 with open(filename, 'r', newline='') as f

    对于 python2,main_mod.__builtins__ = sys.modules['builtins'] 报错,可换成 main_mod.__builtins__ = sys.modules['__builtin__']

    3 replies  •  2019-08-08 10:21:40 +08:00
    ampedee
        1
    ampedee  
       Aug 7, 2019 via Android
    3.3 还是 3.5 变过一次字节码的数量,切到 3.3 之前的版本试试吧
    jiushengyuan
        2
    jiushengyuan  
    OP
       Aug 7, 2019 via Android
    @ampedee 意思是 3 到 3.3 的版本吗?我直接用 2 了。
    jiushengyuan
        3
    jiushengyuan  
    OP
       Aug 8, 2019 via Android
    @ampedee 用 3.3 可以跑起来,谢谢(,,・ω・,,)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2586 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 15:16 · PVG 23:16 · LAX 08:16 · JFK 11:16
    ♥ Do have faith in what you're doing.