Skip to content

Commit

Permalink
feat(python): BytecodeParser update for upcoming Python 3.13
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-beedie committed Sep 11, 2024
1 parent 1a1796a commit daf123a
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions py-polars/polars/_utils/udfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
)

Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit daf123a

Please sign in to comment.