June 17, 2011

Python 3

I've been ignoring Python 3K.

I code under Linux and have been perfectly happy with the 2.x versions that most distros include by default. But Python 3 nags at me like a late property tax of uncertain size.

A while back someone filed a bug that Miniboa didn't work with Python 3. "Get off my lawn", I thought to myself.

To switch gears for a second -- I've found that deleting big chunks of code (murder your darlings) is usually a sign that you're on the right track. I deleted the unfinished Telnet, co-routine, and character-at-a-time code from Netboa. Knowing that the codebase will never be this slim again, I thought it was a good time to take a shot at Python 3 compatibility and issued an 'apt-get install python3'.

Like the five stages of denial, I bet there's a sequence that Python coders go through to port their code to 3K that starts with "keep adding parenthesis", takes them mindless cutting and pasting from googles of error messages, and finishes with them sobbing over byte encoding. Python 3 has two types of strings; bytes and unicode. Oddly, byte sequences are very like Python 2 strings minus any formatting options but with all kinds of gotchas. Take this weirdness for example:
>>> '\x20\x20'[0] == '\x20'
>>> b'\x20\x20'[0] == b'\x20'
Following the advice of Armin Ronacher, I switched most everything to byte sequences as those play nice with sockets and the keys needed for handshaking. After a bit of import hackery, Netboa seems to work under both 3+ and 2.6+. Due to a change in exceptions using the "as" keyword, I lost 2.5 compatibility.

No comments: