Skip to content

Commit 48cb9bb

Browse files
[3.14] gh-145783: Propagate errors raised in NEW_TYPE_COMMENT (GH-145784)
(cherry picked from commit e1c2246) Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
1 parent c3ea6c2 commit 48cb9bb

File tree

5 files changed

+538
-515
lines changed

5 files changed

+538
-515
lines changed

Lib/test/test_type_comments.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import ast
22
import sys
33
import unittest
4+
from test.support import import_helper
45

56

67
funcdef = """\
@@ -391,6 +392,13 @@ def check_both_ways(source):
391392
check_both_ways("pass # type: ignorewhatever\n")
392393
check_both_ways("pass # type: ignoreé\n")
393394

395+
def test_non_utf8_type_comment_with_ignore_cookie(self):
396+
_testcapi = import_helper.import_module('_testcapi')
397+
flags = 0x0800 | 0x1000 # PyCF_IGNORE_COOKIE | PyCF_TYPE_COMMENTS
398+
with self.assertRaises(UnicodeDecodeError):
399+
_testcapi.Py_CompileStringExFlags(
400+
b"a=1 # type: \x80", "<test>", 256, flags)
401+
394402
def test_func_type_input(self):
395403

396404
def parse_func_type_input(source):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an unlikely crash in the parser when certain errors were erroneously not
2+
propagated. Found by OSS Fuzz in :oss-fuzz:`491369109`.

Modules/_testcapimodule.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,18 @@ pycompilestring(PyObject* self, PyObject *obj) {
226226
return Py_CompileString(the_string, "<string>", Py_file_input);
227227
}
228228

229+
static PyObject*
230+
pycompilestringexflags(PyObject *self, PyObject *args) {
231+
const char *the_string, *filename;
232+
int start, flags;
233+
if (!PyArg_ParseTuple(args, "ysii", &the_string, &filename, &start, &flags)) {
234+
return NULL;
235+
}
236+
PyCompilerFlags cf = _PyCompilerFlags_INIT;
237+
cf.cf_flags = flags;
238+
return Py_CompileStringExFlags(the_string, filename, start, &cf, -1);
239+
}
240+
229241
static PyObject*
230242
test_lazy_hash_inheritance(PyObject* self, PyObject *Py_UNUSED(ignored))
231243
{
@@ -2620,6 +2632,7 @@ static PyMethodDef TestMethods[] = {
26202632
{"return_result_with_error", return_result_with_error, METH_NOARGS},
26212633
{"getitem_with_error", getitem_with_error, METH_VARARGS},
26222634
{"Py_CompileString", pycompilestring, METH_O},
2635+
{"Py_CompileStringExFlags", pycompilestringexflags, METH_VARARGS},
26232636
{"raise_SIGINT_then_send_None", raise_SIGINT_then_send_None, METH_VARARGS},
26242637
{"stack_pointer", stack_pointer, METH_NOARGS},
26252638
#ifdef W_STOPCODE

0 commit comments

Comments
 (0)