Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -2691,6 +2691,7 @@ def f(x: *b)
import unittest

from test import support
from test.support.script_helper import assert_python_ok

class SyntaxWarningTest(unittest.TestCase):
def check_warning(self, code, errtext, filename="<testcase>", mode="exec"):
Expand Down Expand Up @@ -3024,6 +3025,22 @@ class A:
class B[{name}]: pass
""", "<testcase>", mode="exec")

@support.nomemtest
def test_disallowed_type_param_names_oom(self):
# gh-152682: Don't crash on OOM when formatting the SyntaxError message
# in symtable_visit_type_param_bound_or_default.
code = textwrap.dedent("""\
import _testcapi
_testcapi.set_nomemory(0)
try:
compile("class A[__classdict__]: pass", "<string>", "exec")
except MemoryError:
pass
else:
raise RuntimeError('MemoryError not raised')
""")
assert_python_ok("-c", code)

@support.cpython_only
def test_nested_named_except_blocks(self):
code = ""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix NULL pointer dereference in :func:`compile` when a reserved name (e.g.
``__classdict__``) is used as a type parameter name and memory allocation
fails while formatting the error message.
3 changes: 3 additions & 0 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -2608,6 +2608,9 @@ symtable_visit_type_param_bound_or_default(

PyObject *error_msg = PyUnicode_FromFormat("reserved name '%U' cannot be "
"used for type parameter", name);
if (error_msg == NULL) {
return 0;
}
PyErr_SetObject(PyExc_SyntaxError, error_msg);
Py_DECREF(error_msg);
SET_ERROR_LOCATION(st->st_filename, LOCATION(tp));
Expand Down
Loading