Sunday, October 19, 2008

Porting Python2.x script to Python3.0

Because Python3.0 (aka Python3000) has no backward compatibility, it is supposed to cause some trouble when porting existing code to Python3.0. According to PEP3000 (PEP stands for Python enhancement proposal) , there is a recommended procedure to port 2.x code to 3.0. This is a quote from PEP3000

  1. You should have excellent unit tests with close to full coverage.
  2. Port your project to Python 2.6.
  3. Turn on the Py3k warnings mode.
  4. Test and edit until no warnings remain.
  5. Use the 2to3 tool to convert this source code to 3.0 syntax. Do not manually edit the output!
  6. Test the converted source code under 3.0.
  7. If problems are found, make corrections to the 2.6 version of the source code and go back to step 3.
  8. When it's time to release, release separate 2.6 and 3.0 tarballs (or whatever archive form you use for releases).
I recommend you read PEP3000 for upcoming Python3.0 release. Unlike many PEPs, it is quite easy to read. It's not urgent since Python3.0 is not released yet (according to PEP361, Python3.0 is planned to be released on Dec. 03 2008) , and even after its first release, the safest way is not using Python3.0. But if you stick to 2.x for years, you'll be left out from the rest of the world in the meantime.

For Python experts: did you know there's one byte code assigned just for PRINT_NEWLINE? No wonder Guido wanted to renew Python interpreter. You can see the list of byte codes here. (and compare it with Python3.0's byte code instructions, you'll see lots of them are simplified or generalized with arguments.)

by the way, is anybody reading this blog regularly? I'm just curious.


Anonymous said...

I read it for the first time today.

hohehohe2 [at] said...

Hi Josh, thanks for visiting!

ger said...

Yeah I read it regularily.

hohehohe2 [at] said...

Hey ger, cheers! thanx the other day.
I'm interested in so many things so I'll write about them bit by bit.

Anonymous said...

Thanks for posting these, I'm going to be porting some older Python code over soon!

hohehohe2 [at] said...

you're welcome :)

captainhaha said...

I'm starting to read it regularly:)
it's good, very interested in your stuff you do. Just getting into python and 3d(for my job) and your blog is really cool to see whats possible.

hohehohe2 [at] said...

Thanks captainhaha,
yeah it's interesting with little stuff you can glue most things together. I'll explore those stuff further.