]> granicus.if.org Git - python/commitdiff
SF patch #460805 by Chris Gonnerman: Support for unsetenv()
authorGuido van Rossum <guido@python.org>
Fri, 19 Oct 2001 01:31:59 +0000 (01:31 +0000)
committerGuido van Rossum <guido@python.org>
Fri, 19 Oct 2001 01:31:59 +0000 (01:31 +0000)
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
Misc/NEWS
Modules/posixmodule.c
configure
configure.in
pyconfig.h.in

index 64caee6efae0186c1522a5b2adbe23a927b762be..9a7417bf6283054ace2a978759b1b2cceb21109b 100644 (file)
--- 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)
 
index 257546d84d87dc710c76dde80521511662d65b96..a03bb09df185e0223800bb7139a84dc6705f7cae 100644 (file)
--- 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
index 22f70b1692315f7a232bf4aa6dd589b202a64761..65e09c02581e3ca83ed7ee041a3f935cbc4c7048 100644 (file)
@@ -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
index d85057d42ea1fda909ac2af4cfec601c541f6148..4b71c3bd2a30654e7bd254c63fcea27610b1169b 100755 (executable)
--- 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
index a5785276fbe8684a6b9f4e8533722ddcd820b501..96ba400c0a21c8cbed72f6e13850792670e0f9de 100644 (file)
@@ -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
 
index 0edfcb2a1494c5a357684016bf36c0501295984b..7de66aff132f4dffeef907f9375f7ab9f6b9d70c 100644 (file)
 /* 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
 
 /* 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
 
 /* 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 <db_185.h> header file.  */
-#undef HAVE_DB_185_H
+/* Define if you have the <db.h> header file.  */
+#undef HAVE_DB_H
 
 /* Define if you have the <db1/ndbm.h> header file.  */
 #undef HAVE_DB1_NDBM_H
 
-/* Define if you have the <db.h> header file.  */
-#undef HAVE_DB_H
+/* Define if you have the <db_185.h> header file.  */
+#undef HAVE_DB_185_H
 
 /* Define if you have the <dirent.h> header file.  */
 #undef HAVE_DIRENT_H