Search code examples
pythonpython-3.xpython-2to3

2to3 conversion for strings


I have written a small Qt-based text editor for Python code that I embed in my application. Now I am going to switch to Python 3 and I want to help the users of my app with converting their code. I know that 2to3 can do most of the conversion for files. However, I need an on-the-fly conversion, without touching the files on disk. I mean something like:

py3_code_str = convert2to3(py2_code_str)

Does anybody know how it can be achieved with 2to3 or lib2to3?


Solution

  • Unfortunately, lib2to3 doesn't have stable interface, meaning it might change drastically.

    Though, in case you don't really care about that, it might be possible (with minor changes) to use from lib2to3.refactor import RefactoringTool and continue on with its refactor_string method.