By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,760 Members | 1,098 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,760 IT Pros & Developers. It's quick & easy.

surprising behaviour of os.environ.clear

P: n/a
If I call os.environ.clear in a python program child processes still
see the deleted entries. But when I iterate over the keys like so

names = os.environ.keys
for k in names:
del os.environ[k]

then the entries are also deleted for the child processes. Where is
the difference? Is this a bug?
(Observed in Python 2.5.2)

--
Joe
Jun 27 '08 #1
Share this Question
Share on Google+
7 Replies

P: n/a
On Jun 27, 4:05*pm, "Joe P. Cool" <joe.p.c...@googlemail.comwrote:
If I call os.environ.clear in a python program child processes still
see the deleted entries. But when I iterate over the keys like so

names = *os.environ.keys
for k in names:
* * del *os.environ[k]

then the entries are also deleted for the child processes. Where is
the difference? Is this a bug?
(Observed in Python 2.5.2)
This is because of how os.environ is implement with a UserDict
subclass. You should report this at bugs.python.org.
>
--
Joe
Jun 28 '08 #2

P: n/a
Benjamin <mu**************@gmail.comwrote:
>
On Jun 27, 4:05*pm, "Joe P. Cool" <joe.p.c...@googlemail.comwrote:
>If I call os.environ.clear in a python program child processes still
see the deleted entries. But when I iterate over the keys like so

names = *os.environ.keys
for k in names:
* * del *os.environ[k]

then the entries are also deleted for the child processes. Where is
the difference? Is this a bug?
(Observed in Python 2.5.2)

This is because of how os.environ is implement with a UserDict
subclass.
Why? I mean, I can see that it happens, but I don't understand why being a
UserDict causes this.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 28 '08 #3

P: n/a
On Jun 27, 4:05 pm, "Joe P. Cool" <joe.p.c...@googlemail.comwrote:
If I call os.environ.clear in a python program child processes still
see the deleted entries. But when I iterate over the keys like so

names = os.environ.keys
for k in names:
del os.environ[k]

then the entries are also deleted for the child processes. Where is
the difference? Is this a bug?
(Observed in Python 2.5.2)
For one thing, the expression 'os.environ.keys' will yield a method
object (not a list, as you're probably expecting), but iterating over
a method as you did should produce an exception. If you want to get
the list of environment vars, you have to call the method, like
'os.environ.keys()'.

Also, aren't changes to environment vars supposed to be visible to
child processes anyway? Which one are you suggesting that behaves the
wrong way, 'os.environ.clear()' or 'del os.environ[key]'?

Jun 28 '08 #4

P: n/a
On 28 Jun., 08:54, s0s...@gmail.com wrote:
For one thing, the expression 'os.environ.keys' will yield a method
object (not a list, as you're probably expecting), but iterating over
a method as you did should produce an exception. If you want to get
the list of environment vars, you have to call the method, like
'os.environ.keys()'.
You are right but it's just a typo in this message, sorry. My real
code is correct.
Also, aren't changes to environment vars supposed to be visible to
child processes anyway?
Yes. Both the clear method and the del method change os.environ.
os.environ IS the environment in a python program.

Which one are you suggesting that behaves the
wrong way, 'os.environ.clear()' or 'del os.environ[key]'?
The former. If a key is not in os.environ, it shouldn't exist for
child
processes.
Jun 28 '08 #5

P: n/a
On 28 Jun., 04:05, Benjamin <musiccomposit...@gmail.comwrote:
On Jun 27, 4:05 pm, "Joe P. Cool" <joe.p.c...@googlemail.comwrote:
This is because of how os.environ is implement with a UserDict
subclass. You should report this at bugs.python.org.
issue 3227: os.environ.clear has no effect on child processes

--
Joe
Jun 28 '08 #6

P: n/a
On 28 Jun., 23:06, "Joe P. Cool" <joe.p.c...@googlemail.comwrote:
On 28 Jun., 04:05, Benjamin <musiccomposit...@gmail.comwrote:
On Jun 27, 4:05 pm, "Joe P. Cool" <joe.p.c...@googlemail.comwrote:
This is because of how os.environ is implement with a UserDict
subclass. You should report this at bugs.python.org.

issue 3227: os.environ.clear has no effect on child processes.
According to Benjamin Peterson this has been fixed in the upcoming
Python 2.6.
Jun 28 '08 #7

P: n/a
On Jun 28, 1:23*am, Tim Roberts <t...@probo.comwrote:
Benjamin <musiccomposit...@gmail.comwrote:
This is because of how os.environ is implement with a UserDict
subclass.

Why? *I mean, I can see that it happens, but I don't understand why being a
UserDict causes this.
The contents of a UserDict is stored in UserDict.data. When
UserDict.clear is called, that contents is simply cleared. environ
needs to override this is to unset env variable and then update the
actual dict.
--
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
Jun 29 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.