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?