[LLVMdev] Minimum Python Version
gregory.szorc at gmail.com
Tue Dec 4 10:39:33 PST 2012
On 12/4/12 1:27 AM, Tobias Grosser wrote:
>> On Mon, Dec 3, 2012 at 7:01 PM, Tobias Grosser <tobias at grosser.es> wrote:
>>> Support for python 3 seems a good thing. What constructs in our code block
>>> the move to python 3?
>> I haven't looked in-depth at this, but just trying to import a couple
>> lit modules I get a lot of syntax errors like:
>>>>> import ShUtil.py
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> File "ShUtil.py", line 119
>> raise ValueError,"Fast path failure: %r != %r" % (res, reference)
>> SyntaxError: invalid syntax
> Yes, this is 2.x python syntax. To support 3.x you need to change it to:
> msg = "Fast path failure: %r != %r" % (res, reference)
> raise ValueError(msg)
> This code should now work for 3.x and 2.6. However, it also works
> without any
> problem on python 2.4. It is therefore a good example to show that
> python 3.x support does not always require removing support for older
> python version.
There is also the difference for handling exceptions. Python 2.4 and 2.5
only support "except Exception, e" syntax. Python 2.6 and 2.7 support
that and "except Exception as e." Python 3.0 and above only support
"except Exception as e." So, exception handling code that needs to work
on 2.4/2.5 through 3 needs to be something like:
e = sys.exc_info
As I said in the initial message in this thread, supporting Python 3 is
easier the closer the Python 2 release is to 3.0. There's the big ticket
language additions, which are nice. But what really sways me are all the
little bugs in the point releases, especially around Unicode handling.
e.g. this will fail in all 2.x releases except 2.7.3 (the newest):
from __future__ import unicode_literals
env = dict(os.environ)
env['FOO'] = 'bar'
output = subprocess.check_output([command], env=env)
It fails because something in the stdlib is expecting str, not unicode.
When you start supporting 2/3, you find these types of bugs all the time
when working with older Python releases. The remedy is to target the
newest and least buggy 2.x release possible.
More information about the llvm-dev