Commit a039a169 authored by Guido van Rossum's avatar Guido van Rossum

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?)
parent f5f25aed
...@@ -354,6 +354,14 @@ except NameError: ...@@ -354,6 +354,14 @@ except NameError:
else: else:
import UserDict 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": if name == "riscos":
# On RISC OS, all env access goes through getenv and putenv # On RISC OS, all env access goes through getenv and putenv
from riscosenviron import _Environ from riscosenviron import _Environ
...@@ -370,8 +378,15 @@ else: ...@@ -370,8 +378,15 @@ else:
self.data[key.upper()] = item self.data[key.upper()] = item
def __getitem__(self, key): def __getitem__(self, key):
return self.data[key.upper()] return self.data[key.upper()]
def __delitem__(self, key): try:
del self.data[key.upper()] 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): def has_key(self, key):
return self.data.has_key(key.upper()) return self.data.has_key(key.upper())
def get(self, key, failobj=None): def get(self, key, failobj=None):
...@@ -391,6 +406,15 @@ else: ...@@ -391,6 +406,15 @@ else:
def update(self, dict): def update(self, dict):
for k, v in dict.items(): for k, v in dict.items():
self[k] = v self[k] = v
try:
unsetenv
except NameError:
pass
else:
def __delitem__(self, key):
unsetenv(key)
del self.data[key]
environ = _Environ(environ) environ = _Environ(environ)
......
...@@ -46,14 +46,15 @@ Extension modules ...@@ -46,14 +46,15 @@ Extension modules
- binascii has now two quopri support functions, a2b_qp and b2a_qp. - binascii has now two quopri support functions, a2b_qp and b2a_qp.
- readline now supports setting the startup_hook and the pre_event_hook. - readline now supports setting the startup_hook and the
pre_event_hook, and adds the add_history() function.
- os and posix supports chroot() and setgroups() where available. The
stat(), fstat(), statvfs() and fstatvfs() functions now return - os and posix supports chroot(), setgroups() and unsetenv() where
"pseudo-sequences" -- the various fields can now be accessed as available. The stat(), fstat(), statvfs() and fstatvfs() functions
attributes (e.g. os.stat("/").st_mtime) but for backwards now return "pseudo-sequences" -- the various fields can now be
compatibility they also behave as a fixed-length sequence. Some accessed as attributes (e.g. os.stat("/").st_mtime) but for
platform-specific fields (e.g. st_rdev) are only accessible as backwards compatibility they also behave as a fixed-length sequence.
Some platform-specific fields (e.g. st_rdev) are only accessible as
attributes. attributes.
- time: localtime(), gmtime() and strptime() now return a - time: localtime(), gmtime() and strptime() now return a
......
...@@ -4083,6 +4083,37 @@ posix_putenv(PyObject *self, PyObject *args) ...@@ -4083,6 +4083,37 @@ posix_putenv(PyObject *self, PyObject *args)
} }
#endif /* putenv */ #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 #ifdef HAVE_STRERROR
static char posix_strerror__doc__[] = static char posix_strerror__doc__[] =
"strerror(code) -> string\n\ "strerror(code) -> string\n\
...@@ -5667,6 +5698,9 @@ static PyMethodDef posix_methods[] = { ...@@ -5667,6 +5698,9 @@ static PyMethodDef posix_methods[] = {
#ifdef HAVE_PUTENV #ifdef HAVE_PUTENV
{"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
#endif #endif
#ifdef HAVE_UNSETENV
{"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
#endif
#ifdef HAVE_STRERROR #ifdef HAVE_STRERROR
{"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
#endif #endif
......
#! /bin/sh #! /bin/sh
# From configure.in Revision: 1.273 # From configure.in Revision: 1.274
# Guess values for system-dependent variables and create Makefiles. # Guess values for system-dependent variables and create Makefiles.
# Generated automatically using autoconf version 2.13 # Generated automatically using autoconf version 2.13
...@@ -4824,7 +4824,7 @@ for ac_func in alarm chown chroot clock confstr ctermid ctermid_r execv \ ...@@ -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 \ setlocale setregid setreuid setsid setpgid setuid setvbuf snprintf \
sigaction siginterrupt sigrelse strftime strptime symlink sysconf \ sigaction siginterrupt sigrelse strftime strptime symlink sysconf \
tcgetpgrp tcsetpgrp tempnam timegm times tmpfile tmpnam tmpnam_r \ tcgetpgrp tcsetpgrp tempnam timegm times tmpfile tmpnam tmpnam_r \
truncate uname waitpid _getpty getpriority truncate uname unsetenv waitpid _getpty getpriority
do do
echo $ac_n "checking for $ac_func""... $ac_c" 1>&6 echo $ac_n "checking for $ac_func""... $ac_c" 1>&6
echo "configure:4831: checking for $ac_func" >&5 echo "configure:4831: checking for $ac_func" >&5
......
...@@ -1411,7 +1411,7 @@ AC_CHECK_FUNCS(alarm chown chroot clock confstr ctermid ctermid_r execv \ ...@@ -1411,7 +1411,7 @@ AC_CHECK_FUNCS(alarm chown chroot clock confstr ctermid ctermid_r execv \
setlocale setregid setreuid setsid setpgid setuid setvbuf snprintf \ setlocale setregid setreuid setsid setpgid setuid setvbuf snprintf \
sigaction siginterrupt sigrelse strftime strptime symlink sysconf \ sigaction siginterrupt sigrelse strftime strptime symlink sysconf \
tcgetpgrp tcsetpgrp tempnam timegm times tmpfile tmpnam tmpnam_r \ 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 # check for openpty and forkpty
......
...@@ -321,6 +321,9 @@ ...@@ -321,6 +321,9 @@
/* The number of bytes in a wchar_t. */ /* The number of bytes in a wchar_t. */
#undef SIZEOF_WCHAR_T #undef SIZEOF_WCHAR_T
/* Define if you have the _getpty function. */
#undef HAVE__GETPTY
/* Define if you have the alarm function. */ /* Define if you have the alarm function. */
#undef HAVE_ALARM #undef HAVE_ALARM
...@@ -423,9 +426,6 @@ ...@@ -423,9 +426,6 @@
/* Define if you have the getpriority function. */ /* Define if you have the getpriority function. */
#undef HAVE_GETPRIORITY #undef HAVE_GETPRIORITY
/* Define if you have the _getpty function. */
#undef HAVE__GETPTY
/* Define if you have the getpwent function. */ /* Define if you have the getpwent function. */
#undef HAVE_GETPWENT #undef HAVE_GETPWENT
...@@ -597,17 +597,20 @@ ...@@ -597,17 +597,20 @@
/* Define if you have the uname function. */ /* Define if you have the uname function. */
#undef HAVE_UNAME #undef HAVE_UNAME
/* Define if you have the unsetenv function. */
#undef HAVE_UNSETENV
/* Define if you have the waitpid function. */ /* Define if you have the waitpid function. */
#undef HAVE_WAITPID #undef HAVE_WAITPID
/* Define if you have the <db_185.h> header file. */ /* Define if you have the <db.h> header file. */
#undef HAVE_DB_185_H #undef HAVE_DB_H
/* Define if you have the <db1/ndbm.h> header file. */ /* Define if you have the <db1/ndbm.h> header file. */
#undef HAVE_DB1_NDBM_H #undef HAVE_DB1_NDBM_H
/* Define if you have the <db.h> header file. */ /* Define if you have the <db_185.h> header file. */
#undef HAVE_DB_H #undef HAVE_DB_185_H
/* Define if you have the <dirent.h> header file. */ /* Define if you have the <dirent.h> header file. */
#undef HAVE_DIRENT_H #undef HAVE_DIRENT_H
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment