Skip to main content
 首页 » 编程设计

python之将代码从 Python 2.x 转换为 3.x

2025年05月04日11webabcd

这是我的 previous question 的跟进,我正在使用 Senthil Kumaran 建议的 2to3 工具

它似乎工作得很好,但它没有选择这部分:

raise LexError,("%s:%d: Rule '%s' returned an unknown token type '%s'" % ( 
    func.func_code.co_filename, func.func_code.co_firstlineno, 
    func.__name__, newtok.type),lexdata[lexpos:]) 

这在 3.2 中应该是什么样子?

编辑:下面答案的变化很好,2to3 现在似乎工作正常。但是在 setup.py 构建中,我现在收到以下错误,请参阅我的新 question .

请您参考如下方法:

函数的func_code属性已经重命名为__code__,试试吧

func.__code__.co_filename, func.__code__.co_firstlineno, 

作为代码段的第二行。