]> granicus.if.org Git - python/commitdiff
Issue #13415: os.unsetenv() doesn't ignore errors anymore.
authorVictor Stinner <victor.stinner@haypocalc.com>
Tue, 22 Nov 2011 21:20:13 +0000 (22:20 +0100)
committerVictor Stinner <victor.stinner@haypocalc.com>
Tue, 22 Nov 2011 21:20:13 +0000 (22:20 +0100)
Lib/test/test_os.py
Misc/NEWS
Modules/posixmodule.c

index 6435b61fdb7a46ce53e54128e85b0786010531ef..4e4956e9537216714da83ee80ea2b769f083df34 100644 (file)
@@ -361,6 +361,15 @@ class EnvironTests(mapping_tests.BasicTestMappingProtocol):
                 value = popen.read().strip()
                 self.assertEqual(value, "World")
 
+    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):
     """Tests for os.walk()."""
 
index 0f4e04b81c9c74d23cdcbb0031c97dc0e281c8e8..1a92e2ad95fc41cf79f024ca22fe7685dfe88577 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -79,6 +79,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #13415: os.unsetenv() doesn't ignore errors anymore.
+
 - Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
   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
index 1040ec5b40480bd35f54705c412e82b5c3905113..6555f1f511ca7f2d396ed0795aa6c6b966b44f71 100644 (file)
@@ -6994,6 +6994,14 @@ posix_putenv(PyObject *self, PyObject *args)
 
     /* XXX This can leak memory -- not easy to fix :-( */
     len = strlen(s1) + strlen(s2) + 2;
+#ifdef MS_WINDOWS
+    if (_MAX_ENV < (len - 1)) {
+        PyErr_Format(PyExc_ValueError,
+                     "the environment variable is longer than %u bytes",
+                     _MAX_ENV);
+        return NULL;
+    }
+#endif
     /* len includes space for a trailing \0; the size arg to
        PyString_FromStringAndSize does not count that */
     newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
@@ -7036,11 +7044,14 @@ static PyObject *
 posix_unsetenv(PyObject *self, PyObject *args)
 {
     char *s1;
+    int err;
 
     if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
         return NULL;
 
-    unsetenv(s1);
+    err = unsetenv(s1);
+    if (err)
+        return posix_error();
 
     /* Remove the key from posix_putenv_garbage;
      * this will cause it to be collected.  This has to