Skip to content

Commit d3bc0f9

Browse files
authored
Merge pull request #156 from 2elli/python-3.13
Python 3.11 - 3.13 Line number fixes
2 parents 79f745f + ac100f8 commit d3bc0f9

File tree

8 files changed

+454
-410
lines changed

8 files changed

+454
-410
lines changed

xdis/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
Code38,
4141
Code310,
4242
Code311,
43-
Code313,
4443
codeType2Portable,
4544
)
4645
from xdis.codetype.base import code_has_star_arg, code_has_star_star_arg, iscode
@@ -184,7 +183,6 @@
184183
"Code3",
185184
"Code310",
186185
"Code311",
187-
"Code313",
188186
"Code38",
189187
"code_has_star_star_arg",
190188
"code_has_star_arg",

xdis/bytecode.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ def get_logical_instruction_at_offset(
318318
# raw name index for LOAD_GLOBAL, LOAD_CONST, etc.
319319

320320
argval = arg
321+
322+
# create a localsplusnames table that resolves duplicates.
323+
localsplusnames = (varnames or tuple()) + tuple(name for name in (cells or tuple()) if name not in varnames)
324+
321325
if op in opc.CONST_OPS:
322326
argval, argrepr = _get_const_info(arg, constants)
323327
elif op in opc.NAME_OPS:
@@ -340,6 +344,12 @@ def get_logical_instruction_at_offset(
340344
elif op in opc.JREL_OPS:
341345
signed_arg = -arg if "JUMP_BACKWARD" in opname else arg
342346
argval = i + get_jump_val(signed_arg, opc.python_version)
347+
348+
# check cache instructions for python 3.13
349+
if opc.version_tuple >= (3, 13):
350+
if opc.opname[op] in ["POP_JUMP_IF_TRUE", "POP_JUMP_IF_FALSE", "POP_JUMP_IF_NONE", "POP_JUMP_IF_NOT_NONE", "JUMP_BACKWARD"]:
351+
argval += 2
352+
343353
# FOR_ITER has a cache instruction in 3.12
344354
if opc.version_tuple >= (3, 12) and opname == "FOR_ITER":
345355
argval += 2
@@ -351,28 +361,24 @@ def get_logical_instruction_at_offset(
351361
if opc.version_tuple >= (3, 13) and opname in ("LOAD_FAST_LOAD_FAST", "STORE_FAST_LOAD_FAST", "STORE_FAST_STORE_FAST"):
352362
arg1 = arg >> 4
353363
arg2 = arg & 15
354-
argval1, argrepr1 = _get_name_info(arg1, (varnames or tuple()) + (cells or tuple()))
355-
argval2, argrepr2 = _get_name_info(arg2, (varnames or tuple()) + (cells or tuple()))
364+
argval1, argrepr1 = _get_name_info(arg1, localsplusnames)
365+
argval2, argrepr2 = _get_name_info(arg2, localsplusnames)
356366
argval = argval1, argval2
357367
argrepr = argrepr1 + ", " + argrepr2
358368
elif opc.version_tuple >= (3, 11):
359-
argval, argrepr = _get_name_info(
360-
arg, (varnames or tuple()) + (cells or tuple())
361-
)
369+
argval, argrepr = _get_name_info(arg, localsplusnames)
362370
else:
363371
argval, argrepr = _get_name_info(arg, varnames)
364372
elif op in opc.FREE_OPS:
365373
if opc.version_tuple >= (3, 11):
366-
argval, argrepr = _get_name_info(
367-
arg, (varnames or tuple()) + (cells or tuple())
368-
)
374+
argval, argrepr = _get_name_info(arg, localsplusnames)
369375
else:
370376
argval, argrepr = _get_name_info(arg, cells)
371377
elif op in opc.COMPARE_OPS:
372-
if opc.python_version >= (3,13):
378+
if opc.python_version >= (3, 13):
373379
# The fifth-lowest bit of the oparg now indicates a forced conversion to bool.
374380
argval = (opc.cmp_op[arg >> 5])
375-
elif opc.python_version >= (3,12):
381+
elif opc.python_version >= (3, 12):
376382
argval = (opc.cmp_op[arg >> 4])
377383
else:
378384
argval = (opc.cmp_op[arg])

xdis/codetype/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from xdis.codetype.code38 import Code38
2929
from xdis.codetype.code310 import Code310
3030
from xdis.codetype.code311 import Code311, Code311FieldNames
31-
from xdis.codetype.code313 import Code313
31+
from xdis.codetype.code312 import Code312
3232
from xdis.version_info import PYTHON_VERSION_TRIPLE
3333

3434

@@ -101,7 +101,7 @@ def codeType2Portable(code, version_tuple=PYTHON_VERSION_TRIPLE):
101101
co_firstlineno=code.co_firstlineno,
102102
co_linetable=line_table,
103103
)
104-
elif version_tuple[:2] < (3,13):
104+
elif version_tuple[:2] == (3,11):
105105
return Code311(
106106
co_argcount=code.co_argcount,
107107
co_posonlyargcount=code.co_posonlyargcount,
@@ -122,8 +122,8 @@ def codeType2Portable(code, version_tuple=PYTHON_VERSION_TRIPLE):
122122
co_linetable=line_table,
123123
co_exceptiontable=code.co_exceptiontable,
124124
)
125-
else: # version tuple >= 3, 13
126-
return Code313(
125+
elif version_tuple[:2] >= (3,12):
126+
return Code312(
127127
co_argcount=code.co_argcount,
128128
co_posonlyargcount=code.co_posonlyargcount,
129129
co_kwonlyargcount=code.co_kwonlyargcount,
@@ -209,11 +209,11 @@ def portableCodeType(version_tuple=PYTHON_VERSION_TRIPLE):
209209
elif version_tuple[:2] == (3, 10):
210210
# 3.10
211211
return Code310
212-
elif version_tuple[:2] < (3,13):
212+
elif version_tuple[:2] == (3,11):
213213
# 3.11 ...
214214
return Code311
215-
else:
216-
return Code313
215+
elif version_tuple[:2] >= (3,12):
216+
return Code312
217217
elif version_tuple > (2, 0):
218218
# 2.0 .. 2.7
219219
return Code2

0 commit comments

Comments
 (0)