Skip to content

Commit 9bbdf16

Browse files
gh-125038: redundant GET_ITER and GET_AITER are removed from genexpr code
Adds CHECK_ITERABLE instruction. It checks that TOS is iterable or async iterable. Redundant GET_ITER and GET_AITER instructions are removed from generator expression code.
1 parent bfc1d25 commit 9bbdf16

17 files changed

+449
-318
lines changed

Doc/library/dis.rst

+8
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,14 @@ result back on the stack.
670670
.. versionadded:: 3.5
671671

672672

673+
.. opcode:: CHECK_ITERABLE (is_async)
674+
675+
Checks that ``STACK[-1]`` is iterable or async iterable, depending on
676+
oparg value.
677+
678+
.. versionadded:: 3.14
679+
680+
673681
.. opcode:: TO_BOOL
674682

675683
Implements ``STACK[-1] = bool(STACK[-1])``.

Include/internal/pycore_magic_number.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ Known values:
262262
Python 3.14a1 3607 (Add pseudo instructions JUMP_IF_TRUE/FALSE)
263263
Python 3.14a1 3608 (Add support for slices)
264264
Python 3.14a2 3609 (Add LOAD_SMALL_INT and LOAD_CONST_IMMORTAL instructions, remove RETURN_CONST)
265+
Python 3.14a2 3610 (Add CHECK_ITERABLE)
265266
266267
Python 3.15 will start with 3650
267268
@@ -274,7 +275,7 @@ PC/launcher.c must also be updated.
274275
275276
*/
276277

277-
#define PYC_MAGIC_NUMBER 3609
278+
#define PYC_MAGIC_NUMBER 3610
278279
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
279280
(little-endian) and then appending b'\r\n'. */
280281
#define PYC_MAGIC_NUMBER_TOKEN \

Include/internal/pycore_opcode_metadata.h

+8-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_ids.h

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_metadata.h

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/opcode_ids.h

+63-62
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/_opcode_metadata.py

+63-62
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_asyncgen.py

+6
Original file line numberDiff line numberDiff line change
@@ -1798,6 +1798,12 @@ async def run():
17981798
res = self.loop.run_until_complete(run())
17991799
self.assertEqual(res, [i * 2 for i in range(1, 10)])
18001800

1801+
def test_async_gen_expression_incorrect(self):
1802+
err_msg = "'async for' requires an object with " \
1803+
"__aiter__ method, got int"
1804+
with self.assertRaisesRegex(TypeError, err_msg):
1805+
g = (x async for x in 42)
1806+
18011807
def test_asyncgen_nonstarted_hooks_are_cancellable(self):
18021808
# See https://bugs.python.org/issue38013
18031809
messages = []

0 commit comments

Comments
 (0)