470,636 Members | 1,423 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,636 developers. It's quick & easy.

surprising behaviour of os.environ.clear

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
7 2277
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
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
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
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
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
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
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.

Similar topics

reply views Thread by mahongquan | last post: by
reply views Thread by jtauber | last post: by
7 posts views Thread by codergem | last post: by
45 posts views Thread by charles.lobo | last post: by
9 posts views Thread by boris.smirnov | last post: by
1 post views Thread by Rahul | last post: by
3 posts views Thread by Tim Chase | last post: by
1 post views Thread by Korara | last post: by
???
reply views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.