From 1aa0c4add4ee00800f8d66dec985ddb92d0ae8d2 Mon Sep 17 00:00:00 2001 From: Alexander Beedie Date: Wed, 11 Sep 2024 12:03:08 +0400 Subject: [PATCH] feat(python): `BytecodeParser` update for upcoming Python 3.13 --- py-polars/polars/_utils/udfs.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/py-polars/polars/_utils/udfs.py b/py-polars/polars/_utils/udfs.py index 6a94c5bf6b9f..dca868fb9f9f 100644 --- a/py-polars/polars/_utils/udfs.py +++ b/py-polars/polars/_utils/udfs.py @@ -604,10 +604,10 @@ def op(inst: Instruction) -> str: return "replace_strict" else: msg = ( - "unrecognized opname" - "\n\nPlease report a bug to https://github.com/pola-rs/polars/issues" - " with the content of function you were passing to `map` and the" - f" following instruction object:\n{inst!r}" + f"unexpected or unrecognised op name ({inst.opname})\n\n" + "Please report a bug to https://github.com/pola-rs/polars/issues " + "with the content of function you were passing to the `map` " + f"expressions and the following instruction object:\n{inst!r}" ) raise AssertionError(msg) @@ -751,7 +751,7 @@ def __init__( self._original_instructions = list(instructions) self._rewritten_instructions = self._rewrite( self._upgrade_instruction(inst) - for inst in self._original_instructions + for inst in self._unpack_superinstructions(self._original_instructions) if inst.opname not in self._ignored_ops ) @@ -1018,6 +1018,22 @@ def _rewrite_methods( return len(matching_instructions) + @staticmethod + def _unpack_superinstructions( + instructions: list[Instruction], + ) -> Iterator[Instruction]: + """Expand known 'superinstructions' into their component parts.""" + for inst in instructions: + if inst.opname == "LOAD_FAST_LOAD_FAST": + for idx in (0, 1): + yield inst._replace( + opname="LOAD_FAST", + argval=inst.argval[idx], + argrepr=inst.argval[idx], + ) + else: + yield inst + @staticmethod def _upgrade_instruction(inst: Instruction) -> Instruction: """Rewrite any older binary opcodes using py 3.11 'BINARY_OP' instead."""