surprising behaviour of os.environ.clear

Discussion in 'Python' started by Joe P. Cool, Jun 27, 2008.

  1. Joe P. Cool

    Joe P. Cool Guest

    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
    Joe P. Cool, Jun 27, 2008
    #1
    1. Advertising

  2. Joe P. Cool

    Benjamin Guest

    On Jun 27, 4:05 pm, "Joe P. Cool" <> 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
    Benjamin, Jun 28, 2008
    #2
    1. Advertising

  3. Joe P. Cool

    Tim Roberts Guest

    Benjamin <> wrote:
    >
    >On Jun 27, 4:05 pm, "Joe P. Cool" <> 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,
    Providenza & Boekelheide, Inc.
    Tim Roberts, Jun 28, 2008
    #3
  4. Joe P. Cool

    Guest

    On Jun 27, 4:05 pm, "Joe P. Cool" <> 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]'?
    , Jun 28, 2008
    #4
  5. Joe P. Cool

    Joe P. Cool Guest

    On 28 Jun., 08:54, 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.
    Joe P. Cool, Jun 28, 2008
    #5
  6. Joe P. Cool

    Joe P. Cool Guest

    On 28 Jun., 04:05, Benjamin <> wrote:
    > On Jun 27, 4:05 pm, "Joe P. Cool" <> 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
    Joe P. Cool, Jun 28, 2008
    #6
  7. Joe P. Cool

    Joe P. Cool Guest

    On 28 Jun., 23:06, "Joe P. Cool" <> wrote:
    > On 28 Jun., 04:05, Benjamin <> wrote:
    >
    > > On Jun 27, 4:05 pm, "Joe P. Cool" <> 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.
    Joe P. Cool, Jun 28, 2008
    #7
  8. Joe P. Cool

    Benjamin Guest

    On Jun 28, 1:23 am, Tim Roberts <> wrote:
    > Benjamin <> 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,
    > Providenza & Boekelheide, Inc.
    Benjamin, Jun 29, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    716
    Peter Otten
    Feb 27, 2007
  2. gert
    Replies:
    0
    Views:
    467
  3. candide
    Replies:
    15
    Views:
    523
    Stephen Hansen
    Jun 18, 2010
  4. Frank Schmitt
    Replies:
    3
    Views:
    167
    Frank Schmitt
    Dec 9, 2003
  5. Michele Simionato

    surprising behaviour of global dictionaries

    Michele Simionato, Oct 9, 2012, in forum: Python
    Replies:
    6
    Views:
    175
    Peter Otten
    Oct 9, 2012
Loading...

Share This Page