Commit 65170954 authored by Victor Stinner's avatar Victor Stinner

(Merge 3.2) Issue #13415: os.unsetenv() doesn't ignore errors anymore.

parents 02686751 60b385e8
...@@ -447,6 +447,15 @@ class EnvironTests(mapping_tests.BasicTestMappingProtocol): ...@@ -447,6 +447,15 @@ class EnvironTests(mapping_tests.BasicTestMappingProtocol):
value_str = value.decode(sys.getfilesystemencoding(), 'surrogateescape') value_str = value.decode(sys.getfilesystemencoding(), 'surrogateescape')
self.assertEqual(os.environ['bytes'], value_str) self.assertEqual(os.environ['bytes'], value_str)
def test_unset_error(self):
if sys.platform == "win32":
# an environment variable is limited to 32,767 characters
key = 'x' * 50000
else:
# "=" is not allowed in a variable name
key = 'key='
self.assertRaises(OSError, os.environ.__delitem__, key)
class WalkTests(unittest.TestCase): class WalkTests(unittest.TestCase):
"""Tests for os.walk().""" """Tests for os.walk()."""
......
...@@ -387,6 +387,8 @@ Core and Builtins ...@@ -387,6 +387,8 @@ Core and Builtins
Library Library
------- -------
- Issue #13415: os.unsetenv() doesn't ignore errors anymore.
- Issue #13245: sched.scheduler class constructor's timefunc and - Issue #13245: sched.scheduler class constructor's timefunc and
delayfunct parameters are now optional. delayfunct parameters are now optional.
scheduler.enter and scheduler.enterabs methods gained a new kwargs parameter. scheduler.enter and scheduler.enterabs methods gained a new kwargs parameter.
......
...@@ -7749,61 +7749,28 @@ static PyObject *posix_putenv_garbage; ...@@ -7749,61 +7749,28 @@ static PyObject *posix_putenv_garbage;
static PyObject * static PyObject *
posix_putenv(PyObject *self, PyObject *args) posix_putenv(PyObject *self, PyObject *args)
{ {
PyObject *newstr = NULL;
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
PyObject *s1, *s2;
wchar_t *newenv;
#else
PyObject *os1, *os2; PyObject *os1, *os2;
char *s1, *s2; wchar_t *newenv;
char *newenv;
size_t len;
#endif
PyObject *newstr = NULL;
#ifdef MS_WINDOWS
if (!PyArg_ParseTuple(args, if (!PyArg_ParseTuple(args,
"UU:putenv", "UU:putenv",
&s1, &s2)) &os1, &os2))
return NULL; return NULL;
#else
if (!PyArg_ParseTuple(args,
"O&O&:putenv",
PyUnicode_FSConverter, &os1,
PyUnicode_FSConverter, &os2))
return NULL;
s1 = PyBytes_AsString(os1);
s2 = PyBytes_AsString(os2);
#endif
#if defined(PYOS_OS2) newstr = PyUnicode_FromFormat("%U=%U", os1, os2);
if (stricmp(s1, "BEGINLIBPATH") == 0) {
APIRET rc;
rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
if (rc != NO_ERROR) {
os2_error(rc);
goto error;
}
} else if (stricmp(s1, "ENDLIBPATH") == 0) {
APIRET rc;
rc = DosSetExtLIBPATH(s2, END_LIBPATH);
if (rc != NO_ERROR) {
os2_error(rc);
goto error;
}
} else {
#endif
/* XXX This can leak memory -- not easy to fix :-( */
/* len includes space for a trailing \0; the size arg to
PyBytes_FromStringAndSize does not count that */
#ifdef MS_WINDOWS
newstr = PyUnicode_FromFormat("%U=%U", s1, s2);
if (newstr == NULL) { if (newstr == NULL) {
PyErr_NoMemory(); PyErr_NoMemory();
goto error; goto error;
} }
if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) {
PyErr_Format(PyExc_ValueError,
"the environment variable is longer than %u characters",
_MAX_ENV);
goto error;
}
newenv = PyUnicode_AsUnicode(newstr); newenv = PyUnicode_AsUnicode(newstr);
if (newenv == NULL) if (newenv == NULL)
goto error; goto error;
...@@ -7812,15 +7779,25 @@ posix_putenv(PyObject *self, PyObject *args) ...@@ -7812,15 +7779,25 @@ posix_putenv(PyObject *self, PyObject *args)
goto error; goto error;
} }
#else #else
len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2; PyObject *os1, *os2;
newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1); char *s1, *s2;
char *newenv;
if (!PyArg_ParseTuple(args,
"O&O&:putenv",
PyUnicode_FSConverter, &os1,
PyUnicode_FSConverter, &os2))
return NULL;
s1 = PyBytes_AsString(os1);
s2 = PyBytes_AsString(os2);
newstr = PyBytes_FromFormat("%s=%s", s1, s2);
if (newstr == NULL) { if (newstr == NULL) {
PyErr_NoMemory(); PyErr_NoMemory();
goto error; goto error;
} }
newenv = PyBytes_AS_STRING(newstr); newenv = PyBytes_AS_STRING(newstr);
PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
if (putenv(newenv)) { if (putenv(newenv)) {
posix_error(); posix_error();
goto error; goto error;
...@@ -7831,13 +7808,7 @@ posix_putenv(PyObject *self, PyObject *args) ...@@ -7831,13 +7808,7 @@ posix_putenv(PyObject *self, PyObject *args)
* this will cause previous value to be collected. This has to * this will cause previous value to be collected. This has to
* happen after the real putenv() call because the old value * happen after the real putenv() call because the old value
* was still accessible until then. */ * was still accessible until then. */
if (PyDict_SetItem(posix_putenv_garbage, if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) {
#ifdef MS_WINDOWS
PyTuple_GET_ITEM(args, 0),
#else
os1,
#endif
newstr)) {
/* really not much we can do; just leak */ /* really not much we can do; just leak */
PyErr_Clear(); PyErr_Clear();
} }
...@@ -7845,10 +7816,6 @@ posix_putenv(PyObject *self, PyObject *args) ...@@ -7845,10 +7816,6 @@ posix_putenv(PyObject *self, PyObject *args)
Py_DECREF(newstr); Py_DECREF(newstr);
} }
#if defined(PYOS_OS2)
}
#endif
#ifndef MS_WINDOWS #ifndef MS_WINDOWS
Py_DECREF(os1); Py_DECREF(os1);
Py_DECREF(os2); Py_DECREF(os2);
...@@ -7873,42 +7840,27 @@ Delete an environment variable."); ...@@ -7873,42 +7840,27 @@ Delete an environment variable.");
static PyObject * static PyObject *
posix_unsetenv(PyObject *self, PyObject *args) posix_unsetenv(PyObject *self, PyObject *args)
{ {
#ifdef MS_WINDOWS PyObject *name;
char *s1; int err;
if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
return NULL;
#else
PyObject *os1;
char *s1;
if (!PyArg_ParseTuple(args, "O&:unsetenv", if (!PyArg_ParseTuple(args, "O&:unsetenv",
PyUnicode_FSConverter, &os1)) PyUnicode_FSConverter, &name))
return NULL; return NULL;
s1 = PyBytes_AsString(os1);
#endif
unsetenv(s1); err = unsetenv(PyBytes_AS_STRING(name));
if (err)
return posix_error();
/* Remove the key from posix_putenv_garbage; /* Remove the key from posix_putenv_garbage;
* this will cause it to be collected. This has to * this will cause it to be collected. This has to
* happen after the real unsetenv() call because the * happen after the real unsetenv() call because the
* old value was still accessible until then. * old value was still accessible until then.
*/ */
if (PyDict_DelItem(posix_putenv_garbage, if (PyDict_DelItem(posix_putenv_garbage, name)) {
#ifdef MS_WINDOWS
PyTuple_GET_ITEM(args, 0)
#else
os1
#endif
)) {
/* really not much we can do; just leak */ /* really not much we can do; just leak */
PyErr_Clear(); PyErr_Clear();
} }
Py_DECREF(name);
#ifndef MS_WINDOWS
Py_DECREF(os1);
#endif
Py_RETURN_NONE; Py_RETURN_NONE;
} }
#endif /* unsetenv */ #endif /* unsetenv */
......
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