Skip to content

Commit 10ec42d

Browse files
Support negative jumps in 3.10
Adds support for negative relative jump args introduced in Python 3.10. Ports corresponding fix from dis python/cpython#31285
1 parent e513da5 commit 10ec42d

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

code_data/blocks.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from __future__ import annotations
77

8+
import ctypes
89
import dis
910
import sys
1011
from dataclasses import dataclass, field
@@ -61,7 +62,6 @@ def bytes_to_blocks(b: bytes) -> Blocks:
6162
# Compute a sorted list of target, to map each one to a bloc offset
6263
targets = sorted(targets_set)
6364
del targets_set
64-
6565
# Then, iterate through each instruction to update the jump to point to the
6666
# block offset, instead of the bytecode offset
6767
block: list[Instruction]
@@ -232,6 +232,9 @@ def _parse_bytes(b: bytes) -> Iterable[tuple[int, int, int, int, int]]:
232232
n_args += 1
233233
if opcode == dis.EXTENDED_ARG:
234234
arg = arg << 8
235+
# https://github.com/python/cpython/pull/31285
236+
if arg > _c_int_upper_limit:
237+
arg -= _c_int_length
235238
else:
236239
first_offset = i - ((n_args - 1) * 2)
237240
next_offset = i + 2
@@ -248,3 +251,11 @@ def _instrsize(arg: int) -> int:
248251
From https://github.com/python/cpython/blob/b2e5794870eb4728ddfaafc0f79a40299576434f/Python/wordcode_helpers.h#L11-L20
249252
"""
250253
return 1 if arg <= 0xFF else 2 if arg <= 0xFFFF else 3 if arg <= 0xFFFFFF else 4
254+
255+
256+
# The number of bits in a signed int
257+
_c_int_bit_size = ctypes.sizeof(ctypes.c_int()) * 8
258+
# The maximum value that can be stored in a signed int
259+
_c_int_upper_limit = (2 ** (_c_int_bit_size - 1)) - 1
260+
# The number of values that can be stored in a signed int
261+
_c_int_length = 2 ** _c_int_bit_size

code_data/line_table.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def from_line_table(line_table: LineTable) -> bytes:
2424
class NewLineTable:
2525
"""
2626
PEP 626 line number table.
27+
https://www.python.org/dev/peps/pep-0626/
2728
"""
2829

2930
bytes_: bytes = field(default=b"")
@@ -47,7 +48,7 @@ def from_bytes(cls, bytes: bytes) -> OldLineTable:
4748
"""
4849
items = []
4950
for i in range(0, len(bytes), 2):
50-
items.append(LineTableItem(bytes[i], bytes[i + 1]))
51+
items.append(LineTableItem(bytes[i + 1], bytes[i]))
5152
return OldLineTable(items)
5253

5354
def to_bytes(self) -> bytes:

0 commit comments

Comments
 (0)