[issue20658] os.environ.clear() fails with empty keys (posix.unsetenv)

STINNER Victor report at bugs.python.org
Wed Feb 19 16:06:54 CET 2014


STINNER Victor added the comment:

In fact, there is also a clearenv() function which could be used by os.environ.clear().

"The clearenv() function clears the environment of all name-value pairs and sets the value of the external variable environ to NULL."

It looks like supported names depends a lot on the platform and platform version. Extract of Linux manual pages:

setenv:
---
BUGS:

POSIX.1-2001  specifies  that  if  name  contains an '=' character, then setenv() should fail with the error EINVAL; however, versions of glibc before 2.3.4 allowed an '=' sign in name.
---

clearenv:
---
CONFORMING TO

Various  UNIX variants (DG/UX, HP-UX, QNX, ...).  POSIX.9 (bindings for FORTRAN77).  POSIX.1-1996 did not accept clearenv() and putenv(3), but changed its mind and scheduled these functions for some later issue of this standard (cf. B.4.6.1).  However, POSIX.1-2001 adds only putenv(3),  and  rejected clearenv().
---

> In any case I think we should wrap unsetenv() in os.environ.clear() so that it should try to remove all environment variables even if some calls of unsetenv() fails.

os.environ.clear() may tries to remove as much keys as possible, but keep keys for which unsetenv raised an error and raise a global error in this case.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20658>
_______________________________________


More information about the Python-bugs-list mailing list