Python Gotcha with Octal Numbers

John Machin sjmachin at lexicon.net
Wed Feb 13 16:30:34 EST 2002


"John W. Baxter" <jwbaxter at spamcop.net> wrote in message news:<130220020751360934%jwbaxter at spamcop.net>...
> In article <mailman.1013611523.15053.python-list at python.org>, Simon
> Brunning <SBrunning at trisystems.co.uk> wrote:
> 
> > Probably to late to do anything about it now - think of all the code which
> > would break!

Yes, unfortunately there is a lot of octal in the library. Some of it
has the reasonable excuse of copying existing Unix practice or
mechanical translation of existing C code:

stat.py:    return mode & 0170000
stat.py:S_IFDIR  = 0040000

However this sort of thing boggles me:

codecs.py:BOM64_BE = '\000\000\376\377'
codecs.py:#       corresponds to Unicode U+0000FEFF in UCS-4

Why not '\x00\x00\xfe\xff'? There is not a whiff of octal in the whole
Unicode book. Why translate hex into octal?

> 
> Did the elves at AT&T pick up the "leading 0 means octal" from
> something earlier, or did they invent this stupidity themselves?

They were captured by aliens and implanted with concepts like using
octal notation on machines whose word-size was not a multiple of 3,
NUL-terminated strings, signed characters, unprintable characters in
file names, backspace not being honoured during a login attempt, etc.
All the (many) good ideas were their own.

> 
> Either way, we're stuck with it.

Unfortunately.

I've been semi-bitten just in the last week; grabbed a table of data
that included month numbers with leading zeroes and pasted it into a
script -- the 08 and 09 caused a syntax error which puzzled me for a
minute or two (I haven't needed to think about octal much since I
worked on an ICL 1905 (24-bit words)). Fortunately no harm done.

Next step: see if Pychecker has an octal warning feature and if not
pester the maintainer to provide one.

> 
>    --John (could the zero-less Romans have been right after all?)



More information about the Python-list mailing list