V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2019-08-07 18:19:13 +08:00 · 1430 次点击
    这是一个创建于 1717 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码在刚刚开始时就跑不起来了,源代码在这里,就两个文件 __main__.pypyvm2.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 条回复    2019-08-08 10:21:40 +08:00
    ampedee
        1
    ampedee  
       2019-08-07 18:51:34 +08:00 via Android
    3.3 还是 3.5 变过一次字节码的数量,切到 3.3 之前的版本试试吧
    jiushengyuan
        2
    jiushengyuan  
    OP
       2019-08-07 18:57:19 +08:00 via Android
    @ampedee 意思是 3 到 3.3 的版本吗?我直接用 2 了。
    jiushengyuan
        3
    jiushengyuan  
    OP
       2019-08-08 10:21:40 +08:00 via Android
    @ampedee 用 3.3 可以跑起来,谢谢(,,・ω・,,)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1093 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 18:38 · PVG 02:38 · LAX 11:38 · JFK 14:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.