Skip to content

Commit

Permalink
gh-125038: redundant GET_ITER and GET_AITER are removed from genexpr …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
efimov-mikhail committed Nov 4, 2024
1 parent bfc1d25 commit 9bbdf16
Show file tree
Hide file tree
Showing 17 changed files with 449 additions and 318 deletions.
8 changes: 8 additions & 0 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,14 @@ result back on the stack.
.. versionadded:: 3.5


.. opcode:: CHECK_ITERABLE (is_async)

Checks that ``STACK[-1]`` is iterable or async iterable, depending on
oparg value.

.. versionadded:: 3.14


.. opcode:: TO_BOOL

Implements ``STACK[-1] = bool(STACK[-1])``.
Expand Down
3 changes: 2 additions & 1 deletion Include/internal/pycore_magic_number.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ Known values:
Python 3.14a1 3607 (Add pseudo instructions JUMP_IF_TRUE/FALSE)
Python 3.14a1 3608 (Add support for slices)
Python 3.14a2 3609 (Add LOAD_SMALL_INT and LOAD_CONST_IMMORTAL instructions, remove RETURN_CONST)
Python 3.14a2 3610 (Add CHECK_ITERABLE)
Python 3.15 will start with 3650
Expand All @@ -274,7 +275,7 @@ PC/launcher.c must also be updated.
*/

#define PYC_MAGIC_NUMBER 3609
#define PYC_MAGIC_NUMBER 3610
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
(little-endian) and then appending b'\r\n'. */
#define PYC_MAGIC_NUMBER_TOKEN \
Expand Down
9 changes: 8 additions & 1 deletion Include/internal/pycore_opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Include/internal/pycore_uop_ids.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Include/internal/pycore_uop_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

125 changes: 63 additions & 62 deletions Include/opcode_ids.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

125 changes: 63 additions & 62 deletions Lib/_opcode_metadata.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Lib/test/test_asyncgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1798,6 +1798,12 @@ async def run():
res = self.loop.run_until_complete(run())
self.assertEqual(res, [i * 2 for i in range(1, 10)])

def test_async_gen_expression_incorrect(self):
err_msg = "'async for' requires an object with " \
"__aiter__ method, got int"
with self.assertRaisesRegex(TypeError, err_msg):
g = (x async for x in 42)

def test_asyncgen_nonstarted_hooks_are_cancellable(self):
# See https://bugs.python.org/issue38013
messages = []
Expand Down
Loading

0 comments on commit 9bbdf16

Please sign in to comment.