Convert Code From Python 2.x To 3.x
Solution 1:
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.
Solution 2:
Remove the comma after LexError. That works in both Python 2 and Python 3.
In Python 2 there was a rarely used syntax to raise exceptions like this:
raise ExceptionClass, "The message string"
This is the one used here, but for some reason, maybe since there is a parenthesis around the message string (according to Senthils tests, it's the line break in the parenthesis that does it), 2to3 misses the change into the much better:
raise ExceptionClass("The message string")
So it should look like this (in Python 2)
message = "%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:])
raise LexError(message)
Because formatting that message on the same line as the raise is fugly. :-) Then in addition func_code has been renamed, so in Python 3 there are more changes. But with the above change 2to3 should work correctly.
Solution 3:
What problem are you getting? 2to3 seems to translate it for me fine.
- 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:])
+ raise LexError("%s:%d: Rule '%s' returned an unknown token type '%s'" % (func.__code__.co_filename,func.__code__.co_firstlineno,func.__name__,newtok.type),lexdata[lexpos:])
Post a Comment for "Convert Code From Python 2.x To 3.x"