Automatically detecting python 3 files in vim

I have different python plugins that use external commands like pydoc

. I would like to be able to set all the appropriate commands for my python 3 counterparts, which would require some form of python 3 auto-detection. I'm not sure if this can be done reliably as there are not many distinguishable factors between python 2/3. Ultimately, I need a function that looks like this:

function! IsPythonThree()
    ... code for detection goes here ...
    return result
endfunction

      

The only way I could be sure that this is being done is through some regex magic that I don't know how to use.

+3


source to share


1 answer


There is significant overlap between Python 2 and 3, especially when you factor in from __future__ import ...

. How do you want to handle such code? How is Python 2 or 3?

So, I don't think you can write a 100% reliability check.



One possible way is to execute the code with python2 using an option -3

. This will check for incompatibilities that cannot be fixed with 2to3

. If found it is definitely python 2 code.

Another is to execute the file with both python 2 and 3 and check if only one of them returns an error.

0


source







All Articles