How to parse Python 2.x with Python 3.x ast module?

I recently wrote a Sublime Text 3 plugin that parses Python code and provides some statistics.

Nothing complicated, except that I noticed that Sublime Text 3 uses Python 3.x and, apparently, the ast module expects to see Python 3.x as well. I looked through the documentation for the ast module, but could not find anything that allowed me to specify the version.

Obviously, this causes problems when analyzing perfectly valid Python 2.7 code.

Is there a way to indicate or somehow detect / guess the Python version? Or allow the parser to be more flexible?

+6
source share
1 answer

No, the ast module can only process Python code for the version of Python that it comes with. This is because under the hood, the same parser is used to compile Python code into byte code; the entire ast is module gives you the AST tree broker. This analyzer does not have backward compatibility mode.

You will need to implement your own parser if you want to parse various source codes from Python versions. This certainly means the path that Jedi (Python autocomplete library).

Looking at the SublimeCodeIntel packaging of CodeIntel2 , I see that this package bases its Python and Python 3 parsers on SilverCity , which has Python bindings. Perhaps this project will be a suitable starting point for your own.

+6
source

Source: https://habr.com/ru/post/977476/


All Articles