python source encoding and unicode
A post from Sam Ruby on encoding issues, quoting a post by Rick Jelliffe on the same issues, prompted an interesting response by Lenny Dommitser.
For those that didn’t follow the link, Lenny’s post includes an excellent example of the use of rot13 as coding for python ;-), and a complaint because base64 does not work. But the failure of base64 is documented, although the error is rather cryptic SyntaxError: encoding problem: with BOM IMO.
From PEP 263:
Any encoding which allows processing the first two lines in the way indicated above is allowed as source code encoding, this includes ASCII compatible encodings as well as certain multi-byte encodings such as Shift_JIS. It does not include encodings which use two or more bytes for all characters like e.g. UTF-16. The reason for this is to keep the encoding detection algorithm in the tokenizer simple.
rot13 maps “#” to itself, and so enables the code to be decoded and still functional, even if comments are a bit funny. OTOH base64 breaks on the couple of comment lines, which makes the interpreter bark. I guess the error message should be improved.
So today I got another useless piece of knowledge:
python -c """# coding: rot13 > cevag h'Uryyb, jbeyq' > """ Hello, world

Add your comment