[Python-Dev] Reading Python source file

Chris Angelico rosuav at gmail.com
Thu Nov 19 07:10:18 EST 2015


On Thu, Nov 19, 2015 at 10:51 PM, Serhiy Storchaka <storchaka at gmail.com> wrote:
> http://bugs.python.org/issue20115

Interestingly, the file linked in the last comment on that issue [1]
ties in with another part of this thread, regarding binary blobs in
Python scripts. It uses open(sys.argv[0],'rb') to find itself, and has
the binary data embedded as a comment. (Though the particular
technique strikes me as fragile; to prevent newlines from messing
things up, \n becomes #% and \r becomes #$, but I don't see any
protection against those sequences occurring naturally in the blob.
Given that it's a bz2 archive, I would expect any two-byte sequence to
be capable of occurring.) To be quite honest, I wouldn't mind if
Python objected to this kind of code. If I were writing it myself, I'd
use a triple-quoted string containing some kind of textualized version
- either the repr of the string, or some kind of base 64 or base 85
encoding. Binary data embedded literally will prevent non-ASCII file
encodings.

ChrisA

[1] http://ftp.waf.io/pub/release/waf-1.7.16


More information about the Python-Dev mailing list