diff --git a/Doc/library/dis.rst b/Doc/library/dis.rst index 4d1740fe468a0a8..1b5f0c92faf02c7 100644 --- a/Doc/library/dis.rst +++ b/Doc/library/dis.rst @@ -851,11 +851,16 @@ not have to be) the original ``STACK[-2]``. .. opcode:: GET_ANEXT - Implement ``STACK.append(get_awaitable(STACK[-1].__anext__()))`` to the stack. + Implement ``STACK.append(get_awaitable(STACK[-1].__anext__()))`` to the stack, + then push ``NULL`` and ``None``, ready for :opcode:`SEND`. See ``GET_AWAITABLE`` for details about ``get_awaitable``. .. versionadded:: 3.5 + .. versionchanged:: next + Also pushes ``NULL`` and ``None``, which were previously pushed by + separate instructions. + .. opcode:: END_ASYNC_FOR diff --git a/Include/internal/pycore_magic_number.h b/Include/internal/pycore_magic_number.h index 8413528c0a26839..041256df56b4938 100644 --- a/Include/internal/pycore_magic_number.h +++ b/Include/internal/pycore_magic_number.h @@ -305,6 +305,7 @@ Known values: Python 3.16a1 3705 (Add INTRINSIC_ADD_CONDITIONAL_ANNOTATION) Python 3.16a1 3706 (Add INTRINSIC_MATCH_CLASS_ISINSTANCE) Python 3.16a1 3707 (GET_AWAITABLE also pushes NULL and None for SEND) + Python 3.16a1 3708 (GET_ANEXT also pushes NULL and None for SEND) Python 3.17 will start with 3750 @@ -314,7 +315,7 @@ Known values: */ -#define PYC_MAGIC_NUMBER 3707 +#define PYC_MAGIC_NUMBER 3708 /* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes (little-endian) and then appending b'\r\n'. */ #define PYC_MAGIC_NUMBER_TOKEN \ diff --git a/Include/internal/pycore_opcode_metadata.h b/Include/internal/pycore_opcode_metadata.h index ca1004816d8b00d..f1e6f1beae644a5 100644 --- a/Include/internal/pycore_opcode_metadata.h +++ b/Include/internal/pycore_opcode_metadata.h @@ -714,7 +714,7 @@ int _PyOpcode_num_pushed(int opcode, int oparg) { case GET_AITER: return 1; case GET_ANEXT: - return 2; + return 4; case GET_AWAITABLE: return 3; case GET_ITER: diff --git a/Include/internal/pycore_uop_ids.h b/Include/internal/pycore_uop_ids.h index 8006830801916a9..31a8e60259f4baf 100644 --- a/Include/internal/pycore_uop_ids.h +++ b/Include/internal/pycore_uop_ids.h @@ -689,7 +689,7 @@ enum { _FOR_ITER_VIRTUAL_r23, _FOR_ITER_VIRTUAL_TIER_TWO_r23, _GET_AITER_r11, - _GET_ANEXT_r12, + _GET_ANEXT_r13, _GET_AWAITABLE_r13, _GET_ITER_r12, _GET_ITER_TRAD_r12, diff --git a/Include/internal/pycore_uop_metadata.h b/Include/internal/pycore_uop_metadata.h index e746fd7c3b79b0b..a264ad3f7848b76 100644 --- a/Include/internal/pycore_uop_metadata.h +++ b/Include/internal/pycore_uop_metadata.h @@ -1630,7 +1630,7 @@ const _PyUopCachingInfo _PyUop_Caching[MAX_UOP_ID+1] = { .best = { 1, 1, 1, 1 }, .entries = { { -1, -1, -1 }, - { 2, 1, _GET_ANEXT_r12 }, + { 3, 1, _GET_ANEXT_r13 }, { -1, -1, -1 }, { -1, -1, -1 }, }, @@ -4283,7 +4283,7 @@ const uint16_t _PyUop_Uncached[MAX_UOP_REGS_ID+1] = { [_MAKE_HEAP_SAFE_r33] = _MAKE_HEAP_SAFE, [_RETURN_VALUE_r11] = _RETURN_VALUE, [_GET_AITER_r11] = _GET_AITER, - [_GET_ANEXT_r12] = _GET_ANEXT, + [_GET_ANEXT_r13] = _GET_ANEXT, [_GET_AWAITABLE_r13] = _GET_AWAITABLE, [_SEND_GEN_FRAME_r33] = _SEND_GEN_FRAME, [_GUARD_TOS_IS_NONE_r01] = _GUARD_TOS_IS_NONE, @@ -5236,7 +5236,7 @@ const char *const _PyOpcode_uop_name[MAX_UOP_REGS_ID+1] = { [_GET_AITER] = "_GET_AITER", [_GET_AITER_r11] = "_GET_AITER_r11", [_GET_ANEXT] = "_GET_ANEXT", - [_GET_ANEXT_r12] = "_GET_ANEXT_r12", + [_GET_ANEXT_r13] = "_GET_ANEXT_r13", [_GET_AWAITABLE] = "_GET_AWAITABLE", [_GET_AWAITABLE_r13] = "_GET_AWAITABLE_r13", [_GET_ITER] = "_GET_ITER", diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-12-14-46-10.gh-issue-161875.oOjR9w.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-12-14-46-10.gh-issue-161875.oOjR9w.rst new file mode 100644 index 000000000000000..9afa682ea8b6ffb --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-12-14-46-10.gh-issue-161875.oOjR9w.rst @@ -0,0 +1,4 @@ +The :opcode:`GET_ANEXT` instruction now also pushes the ``NULL`` and +``None`` that start the :opcode:`SEND` loop, so each :keyword:`async for` +iteration no longer needs separate :opcode:`PUSH_NULL` and ``LOAD_CONST None`` +instructions. diff --git a/Modules/_testinternalcapi/test_cases.c.h b/Modules/_testinternalcapi/test_cases.c.h index 457592374d73d1c..8bb3f857437792a 100644 --- a/Modules/_testinternalcapi/test_cases.c.h +++ b/Modules/_testinternalcapi/test_cases.c.h @@ -6799,6 +6799,8 @@ INSTRUCTION_STATS(GET_ANEXT); _PyStackRef aiter; _PyStackRef awaitable; + _PyStackRef null; + _PyStackRef none; aiter = stack_pointer[-1]; _PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_StackPointerValidate(frame); @@ -6808,8 +6810,12 @@ JUMP_TO_LABEL(error); } awaitable = PyStackRef_FromPyObjectSteal(awaitable_o); + null = PyStackRef_NULL; + none = PyStackRef_None; stack_pointer[0] = awaitable; - stack_pointer += 1; + stack_pointer[1] = null; + stack_pointer[2] = none; + stack_pointer += 3; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); DISPATCH(); } diff --git a/Python/bytecodes.c b/Python/bytecodes.c index c914da85cc65777..c6f1f61f3e08fe1 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1641,12 +1641,14 @@ dummy_func( iter = PyStackRef_FromPyObjectSteal(iter_o); } - inst(GET_ANEXT, (aiter -- aiter, awaitable)) { + inst(GET_ANEXT, (aiter -- aiter, awaitable, null, none)) { PyObject *awaitable_o = _PyEval_GetANext(PyStackRef_AsPyObjectBorrow(aiter)); if (awaitable_o == NULL) { ERROR_NO_POP(); } awaitable = PyStackRef_FromPyObjectSteal(awaitable_o); + null = PyStackRef_NULL; + none = PyStackRef_None; } inst(GET_AWAITABLE, (iterable -- iter, null, none)) { diff --git a/Python/codegen.c b/Python/codegen.c index ac5e7904c053a00..ae60caf195f23ca 100644 --- a/Python/codegen.c +++ b/Python/codegen.c @@ -2223,8 +2223,6 @@ codegen_async_for(compiler *c, stmt_ty s) /* SETUP_FINALLY to guard the __anext__ call */ ADDOP_JUMP(c, loc, SETUP_FINALLY, except); ADDOP(c, loc, GET_ANEXT); - ADDOP(c, loc, PUSH_NULL); - ADDOP_LOAD_CONST(c, loc, Py_None); USE_LABEL(c, send); ADD_YIELD_FROM(c, loc, 1); ADDOP(c, loc, POP_BLOCK); /* for SETUP_FINALLY */ @@ -4841,8 +4839,6 @@ codegen_async_comprehension_generator(compiler *c, location loc, ADDOP_JUMP(c, loc, SETUP_FINALLY, except); ADDOP(c, loc, GET_ANEXT); - ADDOP(c, loc, PUSH_NULL); - ADDOP_LOAD_CONST(c, loc, Py_None); USE_LABEL(c, send); ADD_YIELD_FROM(c, loc, 1); ADDOP(c, loc, POP_BLOCK); diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index 02689598a649e8b..5df8120e7424b93 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -8877,11 +8877,13 @@ break; } - case _GET_ANEXT_r12: { + case _GET_ANEXT_r13: { CHECK_CURRENT_CACHED_VALUES(1); ASSERT_WITHIN_STACK_BOUNDS_IGNORING_CACHE(__FILE__, __LINE__); _PyStackRef aiter; _PyStackRef awaitable; + _PyStackRef null; + _PyStackRef none; _PyStackRef _stack_item_0 = _tos_cache0; aiter = _stack_item_0; stack_pointer[0] = aiter; @@ -8896,12 +8898,12 @@ JUMP_TO_ERROR(); } awaitable = PyStackRef_FromPyObjectSteal(awaitable_o); - _tos_cache1 = awaitable; - _tos_cache0 = aiter; - _tos_cache2 = PyStackRef_ZERO_BITS; - SET_CURRENT_CACHED_VALUES(2); - stack_pointer += -1; - ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); + null = PyStackRef_NULL; + none = PyStackRef_None; + _tos_cache2 = none; + _tos_cache1 = null; + _tos_cache0 = awaitable; + SET_CURRENT_CACHED_VALUES(3); ASSERT_WITHIN_STACK_BOUNDS_IGNORING_CACHE(__FILE__, __LINE__); break; } diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 05a68ead027b743..3af7863e80d2c6a 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -6799,6 +6799,8 @@ INSTRUCTION_STATS(GET_ANEXT); _PyStackRef aiter; _PyStackRef awaitable; + _PyStackRef null; + _PyStackRef none; aiter = stack_pointer[-1]; _PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_StackPointerValidate(frame); @@ -6808,8 +6810,12 @@ JUMP_TO_LABEL(error); } awaitable = PyStackRef_FromPyObjectSteal(awaitable_o); + null = PyStackRef_NULL; + none = PyStackRef_None; stack_pointer[0] = awaitable; - stack_pointer += 1; + stack_pointer[1] = null; + stack_pointer[2] = none; + stack_pointer += 3; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); DISPATCH(); } diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index b1be62bbe0613e5..6648b72e66e281d 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -2763,13 +2763,15 @@ dummy_func(void) { } } - op(_GET_ANEXT, (aiter -- aiter, awaitable)) { + op(_GET_ANEXT, (aiter -- aiter, awaitable, null, none)) { if (sym_matches_type(aiter, &PyAsyncGen_Type)) { awaitable = sym_new_type(ctx, &_PyAsyncGenASend_Type); } else { awaitable = sym_new_not_null(ctx); } + null = PyJitRef_Borrow(sym_new_null(ctx)); + none = PyJitRef_Borrow(sym_new_const(ctx, Py_None)); } op(_GET_AWAITABLE, (iterable -- iter, null, none)) { diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index 6c5fb641c564e9f..c86121fcfc0e79a 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -1872,6 +1872,8 @@ case _GET_ANEXT: { JitOptRef aiter; JitOptRef awaitable; + JitOptRef null; + JitOptRef none; aiter = stack_pointer[-1]; if (sym_matches_type(aiter, &PyAsyncGen_Type)) { awaitable = sym_new_type(ctx, &_PyAsyncGenASend_Type); @@ -1879,9 +1881,13 @@ else { awaitable = sym_new_not_null(ctx); } - CHECK_STACK_BOUNDS(1); + null = PyJitRef_Borrow(sym_new_null(ctx)); + none = PyJitRef_Borrow(sym_new_const(ctx, Py_None)); + CHECK_STACK_BOUNDS(3); stack_pointer[0] = awaitable; - stack_pointer += 1; + stack_pointer[1] = null; + stack_pointer[2] = none; + stack_pointer += 3; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); break; }