1

I have a database of scripts that are in Python 2.7 format. Upon loading in my Python 3 application, I want to convert them to Python 3 format without writing each script to a file. I tried the following:

from lib2to3 import refactor
def convert_2to3(py_script):
    avail_fixes = refactor.get_fixers_from_package('lib2to3.fixes')
    py_converter = refactor.RefactoringTool(avail_fixes)
    ast = py_converter.refactor_string(py_script, '<script>')
    return str(ast)

However sometimes this fails; for instance if py_script is just "pass". The error is cryptic:

lib2to3.pgen2.parse.ParseError: bad input: type=0, value='', context=('\n', (2, 0))

It works if py_script is "", or if it is a multiline string. Any idea what might be causing the simple case to fail?

Oliver
  • 27,510
  • 9
  • 72
  • 103

1 Answers1

1

passing in "pass\n" seems to make it work. In fact, any single line python without the "\n" at the end causes the parser to fail, e.g. "1+1". It's probably due to python's specific sensitivity to spacing / indentation, similar to the care needed in formatting code strings when using a code.InteractiveInterpreter instance to runsource, but I didn't dig deeper.

bc19
  • 61
  • 5
  • I did eventually find that out but forgot I had posted this. You are right, the \n is the issue, it must be present even on last line. I've marked yours as the answer. – Oliver Dec 31 '15 at 14:13
  • Cool - that's a useful little utility function you wrote for in-memory transformations. – bc19 Dec 31 '15 at 23:31