Index: Modules/main.c =================================================================== --- Modules/main.c (révision 74452) +++ Modules/main.c (copie de travail) @@ -543,18 +543,22 @@ } if (command) { + char *commandStr; PyObject *commandObj = PyUnicode_FromWideChar( command, wcslen(command)); free(command); - if (commandObj != NULL) { - sts = PyRun_SimpleStringFlags( - _PyUnicode_AsString(commandObj), &cf) != 0; + if (commandObj != NULL) + commandStr = _PyUnicode_AsString(commandObj); + else + commandStr = NULL; + if (commandStr != NULL) { + sts = PyRun_SimpleStringFlags(commandStr, &cf) != 0; + Py_DECREF(commandObj); } else { PyErr_Print(); sts = 1; } - Py_DECREF(commandObj); } else if (module) { sts = RunModule(module, 1); } Index: Lib/test/test_sys.py =================================================================== --- Lib/test/test_sys.py (révision 74452) +++ Lib/test/test_sys.py (copie de travail) @@ -431,6 +431,15 @@ out = p.stdout.read().strip() self.assertEqual(out, b'?') + def test_main_invalid_unicode(self): + p = subprocess.Popen([sys.executable, "-c", b'print("\x80")'], + stderr=subprocess.PIPE) + p.wait() + self.assertEqual(p.returncode, 1) + err = p.stderr.read().strip() + self.assertEqual(err, b"UnicodeEncodeError: " + b"'utf-8' codec can't encode character '\\udc80' in " + b"position 7: surrogates not allowed") class SizeofTest(unittest.TestCase):