surprising behaviour of os.environ.clear

J

Joe P. Cool

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)
 
B

Benjamin

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

Tim Roberts

Benjamin said:
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.
 
S

s0suk3

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]'?
 
J

Joe P. Cool

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

Benjamin

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top