Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

convert code from Python 2.x to 3.x

This is a followup on my previous question, I am using the 2to3 tool as suggested by Senthil Kumaran

It seems to work well but it doesn't pick up this part:

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:])

What should this look like in 3.2 ?

EDIT: the changes from the answer below are good, 2to3 now seems to work ok. Howevery in the setup.py build I now get the error below, see my new question.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The func_code attribute of functions has been renamed to __code__, so try

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

as the second line of your code snippet.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...