[Python-Dev] Python-3.0, unicode, and os.environ

Toshio Kuratomi a.badger at gmail.com
Sat Dec 6 18:18:30 CET 2008


Bugbee, Larry wrote:
> There has been some discussion here that users should use the str or
> byte function variant based on what is relevant to their system, for
> example when getting a list of file names or opening a file.  That
> thought process really doesn't do much for those of us that write code
> that needs to run on any platform type, without alteration or the
> addition of complex if-statements and/or exceptions.
> 
> Whatever the resolution here, and those of you addressing this thorny
> issue have my admiration, the solution should be such that it gives
> consistent behavior regardless of platform type and doesn't require the
> programmer to know of all the minute details of each possible target
> platform.  
> 
I've been thinking about this and I can only see one option.  I don't
think that it really makes less work for the programmer, though -- it
just shifts the problem and makes it more apparent what your code is doing.

To avoid exceptions and if-then's in program code when accessing
filenames, environment variables, etc, you would need to access each of
these resources via the byte API.  Then, to avoid having to keep track
of what's a string and what's a byte in your other code, you probably
want to convert those bytes to strings.  This is where the burden gets
shifted.  You'll have your own routine(s) to do the conversion and have
to have exception handling code to deal with undecodable filenames.

Note 1: your particular app might be able to get away without doing the
conversion from bytes to string -- it depends on what you're planning on
doing with the filename/environment data.

Note 2: If there isn't a parallel API on all platforms, for instance,
Guido's proposal to not have os.environb on Windows, then you'll still
have to have a platform specific check. (Likely you should try to access
os.evironb in this instance and if it doesn't exist, use os.environ
instead... and remember that you need to either change os.environ's data
into str type or change os.environb's data into byte type.)

-Toshio

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-dev/attachments/20081206/0be3fe60/attachment.pgp>


More information about the Python-Dev mailing list