diff -r 79e5bb0d9b8e Lib/test/test_os.py --- a/Lib/test/test_os.py Tue Feb 18 09:37:43 2014 +0100 +++ b/Lib/test/test_os.py Tue Feb 18 12:51:16 2014 +0100 @@ -683,6 +683,10 @@ class EnvironTests(mapping_tests.BasicTe self.assertIs(cm.exception.args[0], missing) self.assertTrue(cm.exception.__suppress_context__) + def test_empty_key(self): + self.assertNotIn('', os.environ) + self.assertRaises(ValueError, os.environ.__setitem__, '', 'value') + class WalkTests(unittest.TestCase): """Tests for os.walk().""" diff -r 79e5bb0d9b8e Modules/posixmodule.c --- a/Modules/posixmodule.c Tue Feb 18 09:37:43 2014 +0100 +++ b/Modules/posixmodule.c Tue Feb 18 12:51:16 2014 +0100 @@ -8890,13 +8890,21 @@ posix_putenv(PyObject *self, PyObject *a PyObject *os1, *os2; char *s1, *s2; char *newenv; + Py_ssize_t len; if (!PyArg_ParseTuple(args, "O&O&:putenv", PyUnicode_FSConverter, &os1, PyUnicode_FSConverter, &os2)) return NULL; - s1 = PyBytes_AsString(os1); + + if (PyBytes_AsStringAndSize(os1, &s1, &len) < 0) + goto error; + if (len == 0) { + PyErr_SetString(PyExc_ValueError, "key is empty"); + goto error; + } + s2 = PyBytes_AsString(os2); newstr = PyBytes_FromFormat("%s=%s", s1, s2);