removing environment variables patch

Jarkko Torppa torppa at polykoira.megabaud.fi
Sun Dec 26 16:23:22 EST 1999


I did not find a way to remove environment variables from the
environment, here is a patch that adds unsetenv to posixmodule and
teaches os.py's environ to call it when it is available. This is
against cvs version (1.5.2+)

This patch is in publicdomain and as far as I am aware it does not
have any patent issues.

Index: configure.in
===================================================================
RCS file: /projects/cvsroot/python/dist/src/configure.in,v
retrieving revision 1.114
diff -u -r1.114 configure.in
--- configure.in	1999/12/20 21:27:22	1.114
+++ configure.in	1999/12/26 20:25:05
@@ -750,7 +750,7 @@
  flock fork fsync fdatasync fpathconf ftime ftruncate \
  getgroups getlogin getpeername getpgrp getpid getpwent gettimeofday getwd \
  kill link lstat mkfifo mktime nice pathconf pause plock pthread_init \
- putenv readlink \
+ putenv unsetenv readlink \
  select setgid setlocale setuid setsid setpgid setpgrp setvbuf \
  sigaction siginterrupt sigrelse strftime strptime symlink sysconf \
  tcgetpgrp tcsetpgrp tempnam timegm times tmpfile tmpnam tmpnam_r \
Index: config.h.in
===================================================================
RCS file: /projects/cvsroot/python/dist/src/config.h.in,v
retrieving revision 2.51
diff -u -r2.51 config.h.in
--- config.h.in	1999/12/20 21:25:59	2.51
+++ config.h.in	1999/12/26 20:25:06
@@ -341,6 +341,9 @@
 /* Define if you have the putenv function.  */
 #undef HAVE_PUTENV
 
+/* Define if you have the unsetenv function. */
+#undef HAVE_UNSETENV
+
 /* Define if you have the readlink function.  */
 #undef HAVE_READLINK
 
Index: Modules/posixmodule.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Modules/posixmodule.c,v
retrieving revision 2.121
diff -u -r2.121 posixmodule.c
--- posixmodule.c	1999/12/15 18:31:10	2.121
+++ posixmodule.c	1999/12/26 20:25:09
@@ -2962,6 +2962,28 @@
 }
 #endif /* putenv */
 
+#ifdef HAVE_UNSETENV
+static char posix_unsetenv__doc__[] =
+"unsetenv(key) -> None\n\
+Remove an environment variable.";
+static PyObject *
+posix_unsetenv(self, args)
+	PyObject *self;
+	PyObject *args;
+{
+	char *s1;
+
+	if(!PyArg_ParseTuple(args, "s:unsetenv", &s1)) {
+	   return NULL;
+	}
+
+ 	unsetenv(s1);
+	Py_INCREF(Py_None);
+	return Py_None;
+}
+#endif
+
+
 #ifdef HAVE_STRERROR
 static char posix_strerror__doc__[] =
 "strerror(code) -> string\n\
@@ -4523,6 +4545,9 @@
 #endif
 #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__},
Index: Lib/os.py
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Lib/os.py,v
retrieving revision 1.28
diff -u -r1.28 os.py
--- os.py	1999/11/02 20:44:07	1.28
+++ os.py	1999/12/26 20:25:10
@@ -235,6 +235,13 @@
                 exc, arg = error, (errno, msg)
     raise exc, arg
 
+def _exists(name):
+    try:
+        eval(name)
+        return 1
+    except NameError:
+        return 0
+
 # Change environ to automatically call putenv() if it exists
 try:
     # This will fail if there's no putenv
@@ -271,6 +278,10 @@
             def __setitem__(self, key, item):
                 putenv(key, item)
                 self.data[key] = item
+	    if _exists('unsetenv'):
+		def __delitem__(self, key):
+		    unsetenv(key)
+		    del self.data[key]
 
     environ = _Environ(environ)
 
@@ -279,13 +290,6 @@
 
     The optional second argument can specify an alternative default."""
     return environ.get(key, default)
-
-def _exists(name):
-    try:
-        eval(name)
-        return 1
-    except NameError:
-        return 0
 
 # Supply spawn*() (probably only for Unix)
 if _exists("fork") and not _exists("spawnv") and _exists("execv"):






More information about the Python-list mailing list