You are not logged in.

#1 2010-01-05 14:43:55

wuischke
Member
From: Suisse Romande
Registered: 2007-01-06
Posts: 630

Compatibility to both Python 2.5 and Python 3?

Hi everyone,

I'm currently writing a Python library and I'm a bit lost in regard to compatibility. Here are many Python programmers, I hope somebody can help me.

I use strings heavily for byte data and unicode strings for texts.  (code sample) Python 3 turns str into unicode strings and has a new type for byte data. The old unicode function doesn't exist anymore and causes an error.

What's the best way to achieve compatibility to both Python 2.5 (default in Debian and Ubuntu) and Python 3? Do I have to maintain two versions for the time being? Is there something like #ifdefs in C (this would fortunately only affect a small part of my code) I can use?

Thanks for your answers.

Offline

#2 2010-01-05 16:06:55

crankyadmin
Member
Registered: 2009-09-27
Posts: 117
Website

Re: Compatibility to both Python 2.5 and Python 3?

Use the python module 2to3

http://docs.python.org/library/2to3.html


:: Github :: My AUR :: Coreboot ::

Offline

#3 2010-01-05 16:26:45

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: Compatibility to both Python 2.5 and Python 3?

Yes, the Python devs recommend, if you're targetting both versions, that you write your code for Python 2.x and keep tweaking it until 2to3 converts it to Python3 without choking, and the resulting code runs under Python 3 without problems. But you always code in Python 2.x.

A different strategy, which you might combine with the previous, is to have some of your files check sys.version to decide whether to import myfoo3 or import myfoo2. You write Python 2.x-specific code in myfoo2.py and Python3-specific code in myfoo3.py.

Offline

#4 2010-01-05 18:57:34

wuischke
Member
From: Suisse Romande
Registered: 2007-01-06
Posts: 630

Re: Compatibility to both Python 2.5 and Python 3?

Ah, thank you for your answers, particularly at Profjim for explaining the developmental strategy.

I'll have to drop Python 2.5 compatibility in order to properly use 2to3. I guess I'll wait until the next Ubuntu LTS and Debian Squeeze and then go with Python 2.6 + 2to3.

Offline

#5 2010-01-05 23:06:41

baion baion
Member
From: Nicosia, Cyprus
Registered: 2009-06-13
Posts: 48

Re: Compatibility to both Python 2.5 and Python 3?

There is no way of having a source file that is both 2.x and 3.1 compatible, the two versions are incompatible. I suggest keeping seperate files for the two versions to avoid confusion. If you have to pick one version, 2.6 is your best bet, as it is the most widely used at the moment.
The sys.version method mentioned by Profjim sounds nice, I should try it somem time.

Offline

#6 2010-01-06 03:02:21

Nezmer
Member
Registered: 2008-10-24
Posts: 559
Website

Re: Compatibility to both Python 2.5 and Python 3?

I'm not a programmer or anything but I've seen waf scripts simply use:

if sys.hexversion<hexvalue: raise ImportError("your error message")  #to set minimum python 2 version

and

if sys.hexversion>hexvalue: #for python3 code

English is not my native language .

Offline

Board footer

Powered by FluxBB