Lenny Domnitser’s

⇙ Jump to content


This is a static archive of the domnit.org blog,
which Lenny Domnitser wrote between 2006 and 2009.

Encoded Python

PEP 263:

This PEP proposes to introduce a syntax to declare the encoding of
a Python source file. The encoding information is then used by the
Python parser to interpret the file using the given encoding.

I wonder if this will work:

#!/usr/bin/env python
# coding: rot-13

vzcbeg flf

qrs uryyb(jbeyq='World', svyr=flf.fgqbhg):
    cevag >> svyr, 'Hello', jbeyq

PNGPU = h'''Havpbqr fgevatf ner nyfb ebg-13,
ohg olgr fgevatf ner whfg olgrf, fb ner abg
rapbqrq nybat jvgu gur bgure fbhepr pbqr.'''

cevag PNGPU

Yes, you can write Python in rot-13, with one catch: strings are just bytes, so they don’t get encoded. Unicode strings are encoded with the rest of it.

That’s sufficiently murky. Next order of business is to write even more obscure code:

#!/usr/bin/env python
# coding: base64

Rats! Even though 'whatever'.encode('base64') is perfectly valid Python, this didn’t fly. Given what I saw from the rot-13 example, I was interested to see how strings would work with base64 Python. Alas, even this simplest test, just the Python pass statement, a no-op, was a syntax error.

Competent, my ass.