1

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.

Community
  • 1
  • 1
Remko
  • 7,214
  • 2
  • 32
  • 52

3 Answers3

11

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.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
3

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.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • The suggested changes are ok, 2to3 picks it up now but I get an error in the build (after the 2to3 changes). I edited my question. – Remko Mar 31 '11 at 09:18
  • @Remko: Which made it a different question. You should remove the edit and make a new question instead. Except that the new question is a duplicate. :) http://stackoverflow.com/search?q=Attempted+relative+import+in+non-package – Lennart Regebro Mar 31 '11 at 09:43
  • Created new question, if it is a duplicate please point me since I found several questions with the same error but wasn't able to get a solution from them. – Remko Mar 31 '11 at 09:48
  • Accepted this answer as it was the most complete – Remko Mar 31 '11 at 09:48
1

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:])
Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131