PEP263 (Specifying encoding) and bytecode strings

Terry Reedy tjreedy at udel.edu
Mon May 5 12:14:44 EDT 2003


I am a little puzzled by some of the questions and comments in this
thread.  Am I missing something?

"Tony Meyer" <ta-meyer at ihug.co.nz> wrote in message
news:mailman.1052120948.3150.python-list at python.org...
> > > Is there some way to specify that all strings are
> > > bytecodes, and not encoded characters?

The value of a Python string object *is* a sequence of bytecodes.
Character encoding is in the eye of the interpreter/user of a string.

> > Couldn't resourcepackage just insert a suitable encoding
> > "declaration", which is after all a comment and thus
> > innocuous for any previous release of Python -- or else emit
> > non-ascii chars as escape sequences?

My impression from other recent threads is that an encoding
declaration only applies to literal strings in source code; is only
requested if there are non-ascii (high bit set) bytes in some such
literal; and is only really needed if there is a conversion to another
string encoding or to a unicode string.  If there are no such
conversions, then I should think that any dummy declaration would do
as long as the declared 'encoding' is defined for all bytes.

> I probably phrased my question poorly: what, then, is the correct
> encoding for the output of zlib.compress()?  I know IANA has a list
[1]
> of encodings, but it's not really clear which is the right one.

I think you are asking for *the* 'correct' fake declaration.  If there
is not yet a way to say encoding = None or encoding = bytes, then any
one that works should be ok.

> [zlib.compress() returns a string of '\xXX's, where XX is from 00 to
> FF.]

Imitating the Python interpretation of source code so as to see '\xXX'
as one byte rather than a quoted string of four Ascii chars, correct.
So?  This should only matter if you are putting compress() output or
decompress() input into source code, such as for testing each function
separately.  Don't things like

file('tem.z', 'w').write(zlib.compress('Mary had a little lamb'))

work ok, without a warning?

Terry J. Reedy








More information about the Python-list mailing list