gh-145783: Propagate errors raised in NEW_TYPE_COMMENT (#145784)

This commit is contained in:
Stan Ulbrych 2026-03-13 11:05:20 +00:00 committed by GitHub
parent 6d1e9ceed3
commit e1c224624a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 559 additions and 536 deletions

View file

@ -1,6 +1,7 @@
import ast
import sys
import unittest
from test.support import import_helper
funcdef = """\
@ -391,6 +392,13 @@ def check_both_ways(source):
check_both_ways("pass # type: ignorewhatever\n")
check_both_ways("pass # type: ignoreé\n")
def test_non_utf8_type_comment_with_ignore_cookie(self):
_testcapi = import_helper.import_module('_testcapi')
flags = 0x0800 | 0x1000 # PyCF_IGNORE_COOKIE | PyCF_TYPE_COMMENTS
with self.assertRaises(UnicodeDecodeError):
_testcapi.Py_CompileStringExFlags(
b"a=1 # type: \x80", "<test>", 256, flags)
def test_func_type_input(self):
def parse_func_type_input(source):

View file

@ -0,0 +1,2 @@
Fix an unlikely crash in the parser when certain errors were erroneously not
propagated. Found by OSS Fuzz in :oss-fuzz:`491369109`.

View file

@ -226,6 +226,18 @@ pycompilestring(PyObject* self, PyObject *obj) {
return Py_CompileString(the_string, "<string>", Py_file_input);
}
static PyObject*
pycompilestringexflags(PyObject *self, PyObject *args) {
const char *the_string, *filename;
int start, flags;
if (!PyArg_ParseTuple(args, "ysii", &the_string, &filename, &start, &flags)) {
return NULL;
}
PyCompilerFlags cf = _PyCompilerFlags_INIT;
cf.cf_flags = flags;
return Py_CompileStringExFlags(the_string, filename, start, &cf, -1);
}
static PyObject*
test_lazy_hash_inheritance(PyObject* self, PyObject *Py_UNUSED(ignored))
{
@ -2659,6 +2671,7 @@ static PyMethodDef TestMethods[] = {
{"return_result_with_error", return_result_with_error, METH_NOARGS},
{"getitem_with_error", getitem_with_error, METH_VARARGS},
{"Py_CompileString", pycompilestring, METH_O},
{"Py_CompileStringExFlags", pycompilestringexflags, METH_VARARGS},
{"raise_SIGINT_then_send_None", raise_SIGINT_then_send_None, METH_VARARGS},
{"stack_pointer", stack_pointer, METH_NOARGS},
#ifdef W_STOPCODE

1070
Parser/parser.c generated

File diff suppressed because it is too large Load diff

View file

@ -739,7 +739,7 @@ def join_conditions(self, keyword: str, node: Any) -> None:
def emit_action(self, node: Alt, cleanup_code: str | None = None) -> None:
self.print(f"_res = {node.action};")
self.print("if (_res == NULL && PyErr_Occurred()) {")
self.print("if ((_res == NULL || p->error_indicator) && PyErr_Occurred()) {")
with self.indent():
self.print("p->error_indicator = 1;")
if cleanup_code: