This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author terry.reedy
Recipients BreamoreBoy, docs@python, draghuram, jaraco, loewis, pythonmeister, robert.ancell, terry.reedy
Date 2013-06-18.20:28:37
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1371587318.16.0.00558218722651.issue1159@psf.upfronthosting.co.za>
In-reply-to
Content
The problem is the 'putenv' and 'getenv' appear to be parallel, and seem to be documented as being parallel
"Set the environment variable named key to the string value."
"Return the value of the environment variable key if it exists,"
but they are not. Getenv actually looks up the key in the internal os.environ copy while putenv puts to the actual external environment.

This tripped-up someone today on python-list, who did putenv(key, val); getenv(key) and wondered why putenv seemed to have no effect.

I think the solution for this should be to document the asymmetry by adding ' in os.environ' after 'key' in the getenv doc.

--
putenv should not also update os.environ because one can already do that with "os.environ[key] = value" (as recommended) and because the latter uses putenv *if available* to also update the external environment *if possible*. Note that at the time this system was designed, not all systems supported putenv (and perhaps not 'true' getenv either).

getenv(key) is not the same as os.environ[key] because the former has an optional 'default' parameter that defaults to None. So aside from back-compatibility, I do not think the behavior of existing code should change.

A new parameter might be possible. To implement it, one would have to augment the underlying, undocumented, C-coded (for CPython) os-specific module --  posix, nt, os2, ce -- to define a new os-specific getenv function that parallels the os-specific putenv function.

Adding os.environ.update (or .synchronize) to resynchronize os.environ with the external environment would also require a new os-specific function. Currently, the original os.environ is imported as a *data* attribute of the os-specific module.

However, neither of these changes are needed for python code that used os.environ as intended. I don't think we should necessarily cater to badly written C libraries that modify the enviroment in a way that cannot be easily intercepted or controlled. So after making a doc change, I would be inclined to close this pending a python-ideas discussion that supported a new feature.
History
Date User Action Args
2013-06-18 20:28:38terry.reedysetrecipients: + terry.reedy, loewis, jaraco, draghuram, pythonmeister, robert.ancell, docs@python, BreamoreBoy
2013-06-18 20:28:38terry.reedysetmessageid: <1371587318.16.0.00558218722651.issue1159@psf.upfronthosting.co.za>
2013-06-18 20:28:38terry.reedylinkissue1159 messages
2013-06-18 20:28:37terry.reedycreate