mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Issue #6697: Fix a crash if code of "python -c code" contains surrogates
This commit is contained in:
parent
f155f1f4ce
commit
6baded49d0
2 changed files with 26 additions and 4 deletions
|
@ -449,6 +449,24 @@ class SysModuleTest(unittest.TestCase):
|
||||||
|
|
||||||
self.assertRaises(TypeError, sys.intern, S("abc"))
|
self.assertRaises(TypeError, sys.intern, S("abc"))
|
||||||
|
|
||||||
|
def test_main_invalid_unicode(self):
|
||||||
|
import locale
|
||||||
|
non_decodable = b"\xff"
|
||||||
|
encoding = locale.getpreferredencoding()
|
||||||
|
try:
|
||||||
|
non_decodable.decode(encoding)
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
self.skipTest('%r is decodable with encoding %s'
|
||||||
|
% (non_decodable, encoding))
|
||||||
|
code = b'print("' + non_decodable + b'")'
|
||||||
|
p = subprocess.Popen([sys.executable, "-c", code], stderr=subprocess.PIPE)
|
||||||
|
stdout, stderr = p.communicate()
|
||||||
|
self.assertEqual(p.returncode, 1)
|
||||||
|
self.assert_(stderr.startswith(b"UnicodeEncodeError: "
|
||||||
|
b"'utf-8' codec can't encode character '\\udcff' in "
|
||||||
|
b"position 7: surrogates not allowed"), stderr)
|
||||||
|
|
||||||
def test_sys_flags(self):
|
def test_sys_flags(self):
|
||||||
self.assertTrue(sys.flags)
|
self.assertTrue(sys.flags)
|
||||||
|
|
|
@ -563,18 +563,22 @@ Py_Main(int argc, wchar_t **argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (command) {
|
if (command) {
|
||||||
|
char *commandStr;
|
||||||
PyObject *commandObj = PyUnicode_FromWideChar(
|
PyObject *commandObj = PyUnicode_FromWideChar(
|
||||||
command, wcslen(command));
|
command, wcslen(command));
|
||||||
free(command);
|
free(command);
|
||||||
if (commandObj != NULL) {
|
if (commandObj != NULL)
|
||||||
sts = PyRun_SimpleStringFlags(
|
commandStr = _PyUnicode_AsString(commandObj);
|
||||||
_PyUnicode_AsString(commandObj), &cf) != 0;
|
else
|
||||||
|
commandStr = NULL;
|
||||||
|
if (commandStr != NULL) {
|
||||||
|
sts = PyRun_SimpleStringFlags(commandStr, &cf) != 0;
|
||||||
|
Py_DECREF(commandObj);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
PyErr_Print();
|
PyErr_Print();
|
||||||
sts = 1;
|
sts = 1;
|
||||||
}
|
}
|
||||||
Py_DECREF(commandObj);
|
|
||||||
} else if (module) {
|
} else if (module) {
|
||||||
sts = RunModule(module, 1);
|
sts = RunModule(module, 1);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue