Skip to content

GH-104405: Add missing PEP 523 checks #104406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 12, 2023
Merged
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
88 changes: 70 additions & 18 deletions Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1748,28 +1748,80 @@ class Subclass(BaseException, self.module.StateAccessType):

class Test_Pep523API(unittest.TestCase):

def do_test(self, func):
calls = []
def do_test(self, func, names):
actual_calls = []
start = SUFFICIENT_TO_DEOPT_AND_SPECIALIZE
count = start + SUFFICIENT_TO_DEOPT_AND_SPECIALIZE
for i in range(count):
if i == start:
_testinternalcapi.set_eval_frame_record(calls)
func()
_testinternalcapi.set_eval_frame_default()
self.assertEqual(len(calls), SUFFICIENT_TO_DEOPT_AND_SPECIALIZE)
for name in calls:
self.assertEqual(name, func.__name__)

def test_pep523_with_specialization_simple(self):
def func1():
pass
self.do_test(func1)
try:
for i in range(count):
if i == start:
_testinternalcapi.set_eval_frame_record(actual_calls)
func()
finally:
_testinternalcapi.set_eval_frame_default()
expected_calls = names * SUFFICIENT_TO_DEOPT_AND_SPECIALIZE
self.assertEqual(len(expected_calls), len(actual_calls))
for expected, actual in zip(expected_calls, actual_calls, strict=True):
self.assertEqual(expected, actual)

def test_inlined_binary_subscr(self):
class C:
def __getitem__(self, other):
return None
def func():
C()[42]
names = ["func", "__getitem__"]
self.do_test(func, names)

def test_pep523_with_specialization_with_default(self):
def func2(x=None):
def test_inlined_call(self):
def inner(x=42):
pass
def func():
inner()
inner(42)
names = ["func", "inner", "inner"]
self.do_test(func, names)

def test_inlined_call_function_ex(self):
def inner(x):
pass
self.do_test(func2)
def func():
inner(*[42])
names = ["func", "inner"]
self.do_test(func, names)

def test_inlined_for_iter(self):
def gen():
yield 42
def func():
for _ in gen():
pass
names = ["func", "gen", "gen", "gen"]
self.do_test(func, names)

def test_inlined_load_attr(self):
class C:
@property
def a(self):
return 42
class D:
def __getattribute__(self, name):
return 42
def func():
C().a
D().a
names = ["func", "a", "__getattribute__"]
self.do_test(func, names)

def test_inlined_send(self):
def inner():
yield 42
def outer():
yield from inner()
def func():
list(outer())
names = ["func", "outer", "outer", "inner", "inner", "outer", "inner"]
self.do_test(func, names)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix an issue where some :term:`bytecode` instructions could ignore
:pep:`523` when "inlining" calls.
8 changes: 6 additions & 2 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ dummy_func(
}

inst(BINARY_SUBSCR_GETITEM, (unused/1, container, sub -- unused)) {
DEOPT_IF(tstate->interp->eval_frame, BINARY_SUBSCR);
PyTypeObject *tp = Py_TYPE(container);
DEOPT_IF(!PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE), BINARY_SUBSCR);
PyHeapTypeObject *ht = (PyHeapTypeObject *)tp;
Expand Down Expand Up @@ -830,8 +831,9 @@ dummy_func(
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
#endif /* ENABLE_SPECIALIZATION */
assert(frame != &entry_frame);
if ((Py_TYPE(receiver) == &PyGen_Type ||
Py_TYPE(receiver) == &PyCoro_Type) && ((PyGenObject *)receiver)->gi_frame_state < FRAME_EXECUTING)
if ((tstate->interp->eval_frame == NULL) &&
(Py_TYPE(receiver) == &PyGen_Type || Py_TYPE(receiver) == &PyCoro_Type) &&
((PyGenObject *)receiver)->gi_frame_state < FRAME_EXECUTING)
{
PyGenObject *gen = (PyGenObject *)receiver;
_PyInterpreterFrame *gen_frame = (_PyInterpreterFrame *)gen->gi_iframe;
Expand Down Expand Up @@ -867,6 +869,7 @@ dummy_func(
}

inst(SEND_GEN, (unused/1, receiver, v -- receiver, unused)) {
DEOPT_IF(tstate->interp->eval_frame, SEND);
PyGenObject *gen = (PyGenObject *)receiver;
DEOPT_IF(Py_TYPE(gen) != &PyGen_Type &&
Py_TYPE(gen) != &PyCoro_Type, SEND);
Expand Down Expand Up @@ -2331,6 +2334,7 @@ dummy_func(
}

inst(FOR_ITER_GEN, (unused/1, iter -- iter, unused)) {
DEOPT_IF(tstate->interp->eval_frame, FOR_ITER);
PyGenObject *gen = (PyGenObject *)iter;
DEOPT_IF(Py_TYPE(gen) != &PyGen_Type, FOR_ITER);
DEOPT_IF(gen->gi_frame_state >= FRAME_EXECUTING, FOR_ITER);
Expand Down
1 change: 1 addition & 0 deletions Python/ceval_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@

#define DISPATCH_INLINED(NEW_FRAME) \
do { \
assert(tstate->interp->eval_frame == NULL); \
_PyFrame_SetStackPointer(frame, stack_pointer); \
frame->prev_instr = next_instr - 1; \
(NEW_FRAME)->previous = frame; \
Expand Down
Loading