From c524d952da928f745203876f2e67f651c2bc7246 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Fri, 19 Oct 2001 01:31:59 +0000 Subject: SF patch #460805 by Chris Gonnerman: Support for unsetenv() This adds unsetenv to posix, and uses it in the __delitem__ method of os.environ. (XXX Should we change the preferred name for putenv to setenv, for consistency?) --- Lib/os.py | 28 ++++++++++++++++++++++++++-- Misc/NEWS | 17 +++++++++-------- Modules/posixmodule.c | 34 ++++++++++++++++++++++++++++++++++ configure | 4 ++-- configure.in | 2 +- pyconfig.h.in | 17 ++++++++++------- 6 files changed, 82 insertions(+), 20 deletions(-) diff --git a/Lib/os.py b/Lib/os.py index 64caee6..9a7417b 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -354,6 +354,14 @@ except NameError: else: import UserDict + # Fake unsetenv() for Windows + # not sure about os2 and dos here but + # I'm guessing they are the same. + + if name in ('os2', 'nt', 'dos'): + def unsetenv(key): + putenv(key, "") + if name == "riscos": # On RISC OS, all env access goes through getenv and putenv from riscosenviron import _Environ @@ -370,8 +378,15 @@ else: self.data[key.upper()] = item def __getitem__(self, key): return self.data[key.upper()] - def __delitem__(self, key): - del self.data[key.upper()] + try: + unsetenv + except NameError: + def __delitem__(self, key): + del self.data[key.upper()] + else: + def __delitem__(self, key): + unsetenv(key) + del self.data[key.upper()] def has_key(self, key): return self.data.has_key(key.upper()) def get(self, key, failobj=None): @@ -391,6 +406,15 @@ else: def update(self, dict): for k, v in dict.items(): self[k] = v + try: + unsetenv + except NameError: + pass + else: + def __delitem__(self, key): + unsetenv(key) + del self.data[key] + environ = _Environ(environ) diff --git a/Misc/NEWS b/Misc/NEWS index 257546d..a03bb09 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -46,14 +46,15 @@ Extension modules - binascii has now two quopri support functions, a2b_qp and b2a_qp. -- readline now supports setting the startup_hook and the pre_event_hook. - -- os and posix supports chroot() and setgroups() where available. The - stat(), fstat(), statvfs() and fstatvfs() functions now return - "pseudo-sequences" -- the various fields can now be accessed as - attributes (e.g. os.stat("/").st_mtime) but for backwards - compatibility they also behave as a fixed-length sequence. Some - platform-specific fields (e.g. st_rdev) are only accessible as +- readline now supports setting the startup_hook and the + pre_event_hook, and adds the add_history() function. + +- os and posix supports chroot(), setgroups() and unsetenv() where + available. The stat(), fstat(), statvfs() and fstatvfs() functions + now return "pseudo-sequences" -- the various fields can now be + accessed as attributes (e.g. os.stat("/").st_mtime) but for + backwards compatibility they also behave as a fixed-length sequence. + Some platform-specific fields (e.g. st_rdev) are only accessible as attributes. - time: localtime(), gmtime() and strptime() now return a diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 22f70b1..65e09c0 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -4083,6 +4083,37 @@ posix_putenv(PyObject *self, PyObject *args) } #endif /* putenv */ +#ifdef HAVE_UNSETENV +static char posix_unsetenv__doc__[] = +"unsetenv(key) -> None\n\ +Delete an environment variable."; + +static PyObject * +posix_unsetenv(PyObject *self, PyObject *args) +{ + char *s1; + + if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) + return NULL; + + unsetenv(s1); + + /* Remove the key from posix_putenv_garbage; + * this will cause it to be collected. This has to + * happen after the real unsetenv() call because the + * old value was still accessible until then. + */ + if (PyDict_DelItem(posix_putenv_garbage, + PyTuple_GET_ITEM(args, 0))) { + /* really not much we can do; just leak */ + PyErr_Clear(); + } + + Py_INCREF(Py_None); + return Py_None; +} +#endif /* unsetenv */ + #ifdef HAVE_STRERROR static char posix_strerror__doc__[] = "strerror(code) -> string\n\ @@ -5667,6 +5698,9 @@ static PyMethodDef posix_methods[] = { #ifdef HAVE_PUTENV {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, #endif +#ifdef HAVE_UNSETENV + {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, +#endif #ifdef HAVE_STRERROR {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, #endif diff --git a/configure b/configure index d85057d..4b71c3b 100755 --- a/configure +++ b/configure @@ -1,6 +1,6 @@ #! /bin/sh -# From configure.in Revision: 1.273 +# From configure.in Revision: 1.274 # Guess values for system-dependent variables and create Makefiles. # Generated automatically using autoconf version 2.13 @@ -4824,7 +4824,7 @@ for ac_func in alarm chown chroot clock confstr ctermid ctermid_r execv \ setlocale setregid setreuid setsid setpgid setuid setvbuf snprintf \ sigaction siginterrupt sigrelse strftime strptime symlink sysconf \ tcgetpgrp tcsetpgrp tempnam timegm times tmpfile tmpnam tmpnam_r \ - truncate uname waitpid _getpty getpriority + truncate uname unsetenv waitpid _getpty getpriority do echo $ac_n "checking for $ac_func""... $ac_c" 1>&6 echo "configure:4831: checking for $ac_func" >&5 diff --git a/configure.in b/configure.in index a578527..96ba400 100644 --- a/configure.in +++ b/configure.in @@ -1411,7 +1411,7 @@ AC_CHECK_FUNCS(alarm chown chroot clock confstr ctermid ctermid_r execv \ setlocale setregid setreuid setsid setpgid setuid setvbuf snprintf \ sigaction siginterrupt sigrelse strftime strptime symlink sysconf \ tcgetpgrp tcsetpgrp tempnam timegm times tmpfile tmpnam tmpnam_r \ - truncate uname waitpid _getpty getpriority) + truncate uname unsetenv waitpid _getpty getpriority) # check for openpty and forkpty diff --git a/pyconfig.h.in b/pyconfig.h.in index 0edfcb2..7de66af 100644 --- a/pyconfig.h.in +++ b/pyconfig.h.in @@ -321,6 +321,9 @@ /* The number of bytes in a wchar_t. */ #undef SIZEOF_WCHAR_T +/* Define if you have the _getpty function. */ +#undef HAVE__GETPTY + /* Define if you have the alarm function. */ #undef HAVE_ALARM @@ -423,9 +426,6 @@ /* Define if you have the getpriority function. */ #undef HAVE_GETPRIORITY -/* Define if you have the _getpty function. */ -#undef HAVE__GETPTY - /* Define if you have the getpwent function. */ #undef HAVE_GETPWENT @@ -597,17 +597,20 @@ /* Define if you have the uname function. */ #undef HAVE_UNAME +/* Define if you have the unsetenv function. */ +#undef HAVE_UNSETENV + /* Define if you have the waitpid function. */ #undef HAVE_WAITPID -/* Define if you have the header file. */ -#undef HAVE_DB_185_H +/* Define if you have the header file. */ +#undef HAVE_DB_H /* Define if you have the header file. */ #undef HAVE_DB1_NDBM_H -/* Define if you have the header file. */ -#undef HAVE_DB_H +/* Define if you have the header file. */ +#undef HAVE_DB_185_H /* Define if you have the header file. */ #undef HAVE_DIRENT_H -- cgit v0.12