Commit 60b385e8 authored by Victor Stinner's avatar Victor Stinner

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

parent a233df88
...@@ -423,6 +423,15 @@ class EnvironTests(mapping_tests.BasicTestMappingProtocol): ...@@ -423,6 +423,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()."""
......
...@@ -80,6 +80,8 @@ Core and Builtins ...@@ -80,6 +80,8 @@ Core and Builtins
Library Library
------- -------
- Issue #13415: os.unsetenv() doesn't ignore errors anymore.
- Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is - Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
raised when the wrapped raw file is non-blocking and the write would block. raised when the wrapped raw file is non-blocking and the write would block.
Previous code assumed that the raw write() would raise BlockingIOError, but Previous code assumed that the raw write() would raise BlockingIOError, but
......
...@@ -6106,6 +6106,12 @@ posix_putenv(PyObject *self, PyObject *args) ...@@ -6106,6 +6106,12 @@ posix_putenv(PyObject *self, PyObject *args)
PyBytes_FromStringAndSize does not count that */ PyBytes_FromStringAndSize does not count that */
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
len = wcslen(s1) + wcslen(s2) + 2; len = wcslen(s1) + wcslen(s2) + 2;
if (_MAX_ENV < (len - 1)) {
PyErr_Format(PyExc_ValueError,
"the environment variable is longer than %u characters",
_MAX_ENV);
goto error;
}
newstr = PyUnicode_FromUnicode(NULL, (int)len - 1); newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
#else #else
len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2; len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
...@@ -6177,42 +6183,30 @@ Delete an environment variable."); ...@@ -6177,42 +6183,30 @@ Delete an environment variable.");
static PyObject * static PyObject *
posix_unsetenv(PyObject *self, PyObject *args) posix_unsetenv(PyObject *self, PyObject *args)
{ {
#ifdef MS_WINDOWS
char *s1;
if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
return NULL;
#else
PyObject *os1; PyObject *os1;
char *s1; char *s1;
int err;
if (!PyArg_ParseTuple(args, "O&:unsetenv", if (!PyArg_ParseTuple(args, "O&:unsetenv",
PyUnicode_FSConverter, &os1)) PyUnicode_FSConverter, &os1))
return NULL; return NULL;
s1 = PyBytes_AsString(os1); s1 = PyBytes_AsString(os1);
#endif
unsetenv(s1); err = unsetenv(s1);
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, os1)) {
#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();
} }
#ifndef MS_WINDOWS
Py_DECREF(os1); 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