Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > surprising behaviour of os.environ.clear

Reply
Thread Tools

surprising behaviour of os.environ.clear

 
 
Joe P. Cool
Guest
Posts: n/a
 
      06-27-2008
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
 
Reply With Quote
 
 
 
 
Benjamin
Guest
Posts: n/a
 
      06-28-2008
On Jun 27, 4:05*pm, "Joe P. Cool" <(E-Mail Removed)> wrote:
> 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


 
Reply With Quote
 
 
 
 
Tim Roberts
Guest
Posts: n/a
 
      06-28-2008
Benjamin <(E-Mail Removed)> wrote:
>
>On Jun 27, 4:05*pm, "Joe P. Cool" <(E-Mail Removed)> wrote:
>> 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, http://www.velocityreviews.com/forums/(E-Mail Removed)
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
s0suk3@gmail.com
Guest
Posts: n/a
 
      06-28-2008
On Jun 27, 4:05 pm, "Joe P. Cool" <(E-Mail Removed)> wrote:
> 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]'?

 
Reply With Quote
 
Joe P. Cool
Guest
Posts: n/a
 
      06-28-2008
On 28 Jun., 08:54, (E-Mail Removed) 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.
 
Reply With Quote
 
Joe P. Cool
Guest
Posts: n/a
 
      06-28-2008
On 28 Jun., 04:05, Benjamin <(E-Mail Removed)> wrote:
> On Jun 27, 4:05 pm, "Joe P. Cool" <(E-Mail Removed)> wrote:


> 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
 
Reply With Quote
 
Joe P. Cool
Guest
Posts: n/a
 
      06-28-2008
On 28 Jun., 23:06, "Joe P. Cool" <(E-Mail Removed)> wrote:
> On 28 Jun., 04:05, Benjamin <(E-Mail Removed)> wrote:
>
> > On Jun 27, 4:05 pm, "Joe P. Cool" <(E-Mail Removed)> wrote:
> > 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.
 
Reply With Quote
 
Benjamin
Guest
Posts: n/a
 
      06-29-2008
On Jun 28, 1:23*am, Tim Roberts <(E-Mail Removed)> wrote:
> Benjamin <(E-Mail Removed)> wrote:
>
> >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, (E-Mail Removed)
> Providenza & Boekelheide, Inc.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
surprising behaviour of global dictionaries Michele Simionato Python 6 10-09-2012 05:45 PM
List of lists surprising behaviour candide Python 15 06-18-2010 04:47 PM
It is very surprising .... janek C++ 3 02-21-2005 06:44 PM
Help with Datagrid (Surprising No Data!!) naija naija ASP .Net 2 10-02-2004 04:10 AM
Surprising(?) behaviour of the comma operator Frank Schmitt Ruby 3 12-09-2003 09:43 AM



Advertisments