Message79448
According to a recent thread on python-dev, Python APIs with the same
name as native APIs can be trusted to just pass through to the platform
API call
(<http://mail.python.org/pipermail/python-dev/2009-January/084899.html>).
This isn't the case for the environment APIs in the os module. For
example, os.getenv doesn't always return the actual value of a key in
the environment. Instead, it looks in the os.environ pseudo-dict to get
the answer. This can be wrong because os.environ is a *copy* of the
environment which any non-Python API will use, and as a copy it can get
out of sync with the real environment. One way it can get out of sync
is via os.environ.pop (at least on POSIX - os.environ has different
implementations on different platforms, I find the way an implementation
is selected to be rather confusing and I have not tracked down the
precise behavior for all platforms):
>>> import os
>>> os.environ.pop('PATH')
'/home/exarkun/.local/sbin:/home/exarkun/.local/bin:/home/exarkun/Projects/combinator_paths/bincache:/home/exarkun/.local/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games'
>>> os.getenv('PATH')
>>> os.system("echo $PATH")
/home/exarkun/.local/sbin:/home/exarkun/.local/bin:/home/exarkun/Projects/combinator_paths/bincache:/home/exarkun/.local/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games
0
>>>
Another way is via calls to the platform environment manipulation APIs
(setenv, unsetenv or putenv). These might be invoked by a third-party
library which is exposed to Python or via ctypes.
`environ´ itself is a C API, though it's a char**, whereas os.environ is
a Python dict. It's probably convenient for os.environ to remain as a
dict, since that's so much simpler to manipulate and inspect than the C
data structure used. However, it'd be good if were always in sync with
the real process environment. A good way to do this would probably be
to stop maintaining a copy of the environment and always pass through to
one of the platform APIs to satisfy a method call onto it. |
|
Date |
User |
Action |
Args |
2009-01-08 22:08:07 | exarkun | set | recipients:
+ exarkun |
2009-01-08 22:08:07 | exarkun | set | messageid: <1231452487.08.0.408278867366.issue4887@psf.upfronthosting.co.za> |
2009-01-08 22:08:06 | exarkun | link | issue4887 messages |
2009-01-08 22:08:04 | exarkun | create | |
|