views:

153

answers:

3

You may know the Windows compliance tool that helps people to know if their code us supported by any version of the MS OS.

I am looking something similar for Python.

I am writing a lib with Python 2.6 and I realized that it was not compatible with Python 2.5 due to the use of the with keyword.

I would like to know if there is a simple and automatic way to avoid this situation in the future.

I am also interested in something similar to know which OS are supported.

Thanks for your help

+7  A: 

In response to a previous question about this, I wrote pyqver. If you have any improvements, please feel free to fork and contribute!

Greg Hewgill
+1 for awesome hair color
lyrae
pyqver seems to be the tool I am looking for. Thank you very much.However, I've notice that my script was seen as compatible with 2.5 but it was not. from __future__ import with_statement was missing.The "except as" was also not detected as a compatibility issue with 2.5. I can imagine that it is complex to handle all cases. pyqver is a great tool and I will try to contribute and to improve it if I can
luc
The "except as" is noted as a bug in the documentation. I'll see if I can find out what happened with the with statement.
Greg Hewgill
When you noted the problem with the "with" statement, which version of Python were you using to run pyqver? It works as expected when run with 2.6, but with 2.5 the standard library "compiler" module doesn't recognise the "with" keyword. I recommend to run pyqver with 2.6 or later.
Greg Hewgill
I was running pyqver with python 2.6. It detected my code as valid for python 2.5. In fact my code was valid for that version but the "from future import with_statement" was missing.
luc
This approach may be useful for *triage* of a script/module/package that needs to be made usable on older Pythons, but there is no substitute for actual testing; many problems can't be discovered by static analysis (e.g. a in b where b is a dict). Notes on the tool itself: (1) doctest fails for spurious reasons (actual output is not in the simplistic (2, 5) format of the expected) (2) handling e.g. `from decimal import Decimal` is in the TODO
John Machin
@John: I noticed the broken doctest too and committed a fix.
Greg Hewgill
+5  A: 

I recommend you rather use automated tests than a code analysis tool.

Be aware that there are subtle behaviour changes in the Python standard library that your code may or may not depend upon. For example httplib: When uploading files, it is normal to give the data as a str. In Python 2.6 you can give stream objects instead (useful for >1GB files) if you nudge them correctly, but in Python 2.5 you will get an error.

A comprehensive set of unit tests and integration tests will be much more reliable because they test that your program actually works on Python version X.Y.

$ python2.6 tests/run_all.py
.................................
33 tests passed
[OK]

You're Python 2.6 compatible.

$ python2.4 tests/run_all.py
...........EEE.........EEE.......
27 tests passed, 6 errors
[FAIL]

You're not Python 2.4 compatible.

Deestan
+1 This is also a good idea. There are quite a range of more subtle things that a static analysis tool cannot detect. If you've got good coverage with your unit tests, this is an excellent method.
Greg Hewgill
Thansk for this remark which is very valuable. However I don't see a code analysis tool as a competitor for unit testing. It has different purposes and should be used together. Analysis tool can help detect potential problems more quickly than unit testing because it is focussed on compatibility issues. I am very happy to get a code analysis tool in my toolbox now but of course I keep running my tests for all supported versions
luc
A: 

Python 2.5 can still be saved, since it can use the with keyword:

from __future__ import with_statement
kaizer.se