Skip to content

Commit 4801375

Browse files
committed
gh-155151: Check recursion limits in specialized Python calls
1 parent ee521e8 commit 4801375

5 files changed

Lines changed: 56 additions & 2 deletions

File tree

Include/internal/pycore_opcode_metadata.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_call.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,26 @@ def recurse_kw(a=0):
11521152
with self.assertRaises(RecursionError):
11531153
recurse_kw()
11541154

1155+
def test_recursion_with_star_args(self):
1156+
# GH-155151: CALL_EX_PY forgot to check the recursion limit.
1157+
def recurse_star_args(depth):
1158+
if depth:
1159+
recurse_star_args(*(depth - 1,))
1160+
with set_recursion_limit(50):
1161+
with self.assertRaises(RecursionError):
1162+
recurse_star_args(100)
1163+
1164+
def test_recursion_with_bound_method_kwargs(self):
1165+
# GH-155151: CALL_KW_BOUND_METHOD forgot to check the recursion limit.
1166+
class Recurse:
1167+
def recurse(self, depth):
1168+
if depth:
1169+
recurse_bound_method(depth=depth - 1)
1170+
recurse_bound_method = Recurse().recurse
1171+
with set_recursion_limit(50):
1172+
with self.assertRaises(RecursionError):
1173+
recurse_bound_method(100)
1174+
11551175

11561176
class TestFunctionWithManyArgs(unittest.TestCase):
11571177
def test_function_with_many_args(self):

Modules/_testinternalcapi/test_cases.c.h

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/bytecodes.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5542,6 +5542,7 @@ dummy_func(
55425542
_CHECK_METHOD_VERSION_KW +
55435543
_EXPAND_METHOD_KW +
55445544
flush + // so that self is in the argument array
5545+
_CHECK_RECURSION_REMAINING +
55455546
_PY_FRAME_KW +
55465547
_SAVE_RETURN_OFFSET +
55475548
_PUSH_FRAME;
@@ -5762,6 +5763,7 @@ dummy_func(
57625763
_CHECK_PEP_523 +
57635764
_MAKE_CALLARGS_A_TUPLE +
57645765
_CHECK_IS_PY_CALLABLE_EX +
5766+
_CHECK_RECURSION_REMAINING +
57655767
_PY_FRAME_EX +
57665768
_SAVE_RETURN_OFFSET +
57675769
_PUSH_FRAME;

Python/generated_cases.c.h

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)