Skip to content

Commit 6a36163

Browse files
[3.13] 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 b012f68 commit 6a36163

File tree

5 files changed

+522
-499
lines changed

5 files changed

+522
-499
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
@@ -331,6 +331,18 @@ pycompilestring(PyObject* self, PyObject *obj) {
331331
return Py_CompileString(the_string, "<string>", Py_file_input);
332332
}
333333

334+
static PyObject*
335+
pycompilestringexflags(PyObject *self, PyObject *args) {
336+
const char *the_string, *filename;
337+
int start, flags;
338+
if (!PyArg_ParseTuple(args, "ysii", &the_string, &filename, &start, &flags)) {
339+
return NULL;
340+
}
341+
PyCompilerFlags cf = _PyCompilerFlags_INIT;
342+
cf.cf_flags = flags;
343+
return Py_CompileStringExFlags(the_string, filename, start, &cf, -1);
344+
}
345+
334346
static PyObject*
335347
test_lazy_hash_inheritance(PyObject* self, PyObject *Py_UNUSED(ignored))
336348
{
@@ -3552,6 +3564,7 @@ static PyMethodDef TestMethods[] = {
35523564
{"return_result_with_error", return_result_with_error, METH_NOARGS},
35533565
{"getitem_with_error", getitem_with_error, METH_VARARGS},
35543566
{"Py_CompileString", pycompilestring, METH_O},
3567+
{"Py_CompileStringExFlags", pycompilestringexflags, METH_VARARGS},
35553568
{"dict_get_version", dict_get_version, METH_VARARGS},
35563569
{"raise_SIGINT_then_send_None", raise_SIGINT_then_send_None, METH_VARARGS},
35573570
{"stack_pointer", stack_pointer, METH_NOARGS},

0 commit comments

Comments
 (0)