Search code examples
pythonpython-2to3

Using 2to3 on in-memory scripts


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?


Solution

  • 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.