Skip to content

Commit 65a3db2

Browse files
author
rocky
committed
Go over python_implementation value
1 parent 8777c18 commit 65a3db2

32 files changed

+259
-58
lines changed

xdis/cross_dis.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ def _try_compile(source: str, name: str) -> CodeType:
4444
return c
4545

4646

47-
def code_info(x, version_tuple: Tuple[int, ...], python_implementation: PythonImplementation) -> str:
47+
def code_info(
48+
x, version_tuple: Tuple[int, ...], python_implementation: PythonImplementation
49+
) -> str:
4850
"""Formatted details of methods, functions, or code."""
49-
return format_code_info(get_code_object(x), version_tuple, python_implementation=python_implementation)
51+
return format_code_info(
52+
get_code_object(x), version_tuple, python_implementation=python_implementation
53+
)
5054

5155

5256
def get_code_object(x):
@@ -259,7 +263,12 @@ def op_has_argument(opcode: int, opc) -> bool:
259263
"""
260264
Return True if `opcode` instruction has an operand.
261265
"""
262-
return opcode in opc.hasarg if hasattr(opc, "hasarg") else opcode >= opc.HAVE_ARGUMENT
266+
return (
267+
opcode in opc.hasarg
268+
if hasattr(opc, "hasarg")
269+
and opc.python_implementation is PythonImplementation.RustPython
270+
else opcode >= opc.HAVE_ARGUMENT
271+
)
263272

264273

265274
def pretty_flags(flags, python_implementation=PYTHON_IMPLEMENTATION) -> str:
@@ -269,7 +278,10 @@ def pretty_flags(flags, python_implementation=PYTHON_IMPLEMENTATION) -> str:
269278
for i in range(32):
270279
flag = 1 << i
271280
if flags & flag:
272-
if python_implementation == PythonImplementation.PyPy and flag in PYPY_COMPILER_FLAG_NAMES:
281+
if (
282+
python_implementation == PythonImplementation.PyPy
283+
and flag in PYPY_COMPILER_FLAG_NAMES
284+
):
273285
names.append(PYPY_COMPILER_FLAG_NAMES.get(flag, hex(flag)))
274286
else:
275287
names.append(COMPILER_FLAG_NAMES.get(flag, hex(flag)))
@@ -320,7 +332,9 @@ def format_code_info(
320332
pass
321333

322334
if version_tuple >= (1, 3):
323-
lines.append("# Flags: %s" % pretty_flags(co.co_flags, python_implementation))
335+
lines.append(
336+
"# Flags: %s" % pretty_flags(co.co_flags, python_implementation)
337+
)
324338

325339
if version_tuple >= (1, 5):
326340
lines.append("# First Line: %s" % co.co_firstlineno)
@@ -373,7 +387,9 @@ def format_exception_table(bytecode, version_tuple) -> str:
373387
for entry in bytecode.exception_entries:
374388
lasti = " lasti" if entry.lasti else ""
375389
end = entry.end - 2
376-
lines.append(f" {entry.start} to {end} -> {entry.target} [{entry.depth}]{lasti}")
390+
lines.append(
391+
f" {entry.start} to {end} -> {entry.target} [{entry.depth}]{lasti}"
392+
)
377393
return "\n".join(lines)
378394

379395

@@ -414,7 +430,11 @@ def unpack_opargs_bytecode(code, opc):
414430
offset += 1
415431
if op_has_argument(op, opc):
416432
arg = code2num(code, offset) | extended_arg
417-
extended_arg = extended_arg_val(opc, arg) if hasattr(opc, "EXTENDED_ARG") and op == opc.EXTENDED_ARG else 0
433+
extended_arg = (
434+
extended_arg_val(opc, arg)
435+
if hasattr(opc, "EXTENDED_ARG") and op == opc.EXTENDED_ARG
436+
else 0
437+
)
418438
offset += 2
419439
else:
420440
arg = None
@@ -474,7 +494,7 @@ def xstack_effect(opcode, opc, oparg: int = 0, jump=None):
474494
if opname == "BUILD_MAP" and version_tuple >= (3, 5):
475495
return 1 - (2 * oparg)
476496
if opname in ("UNPACK_SEQUENCE",):
477-
return oparg - 1
497+
return oparg - 1
478498
elif opname in ("UNPACK_EX"):
479499
return (oparg & 0xFF) + (oparg >> 8)
480500
elif opname == "BUILD_INTERPOLATION":

xdis/opcodes/opcode_10.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# (C) Copyright 2019-2023 by Rocky Bernstein
1+
# (C) Copyright 2019-2023, 2025 by Rocky Bernstein
22
#
33
# This program is free software; you can redistribute it and/or
44
# modify it under the terms of the GNU General Public License
@@ -16,16 +16,16 @@
1616
"""
1717
CPython 1.0 bytecode opcodes
1818
19-
This is used in bytecode disassembly. This is similar to the
20-
opcodes in Python's dis.py library.
19+
This is like Python 1.0's dis.py with some classification
20+
of stack usage and information for formatting instructions.
2121
"""
2222

2323
import xdis.opcodes.opcode_11 as opcode_11
2424

2525
# This is used from outside this module
2626
from xdis.cross_dis import findlabels # noqa
2727
from xdis.opcodes.base import ( # Although these aren't used here, they are exported; noqa
28-
cpython_implementation as python_implementation,
28+
cpython_implementation,
2929
finalize_opcodes,
3030
init_opdata,
3131
name_op,
@@ -35,6 +35,7 @@
3535
from xdis.opcodes.opcode_11 import opcode_arg_fmt11, opcode_extended_fmt11
3636

3737
version_tuple = (1, 0)
38+
python_implementation = cpython_implementation
3839

3940
loc = locals()
4041
init_opdata(loc, opcode_11, version_tuple)

xdis/opcodes/opcode_11.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
# This is used from outside this module
2626
from xdis.cross_dis import findlabels
2727
from xdis.opcodes.base import ( # Although these aren't used here, they are exported; noqa
28-
cpython_implementation as python_implementation,
28+
cpython_implementation,
2929
finalize_opcodes,
3030
init_opdata,
3131
update_pj2,
3232
)
3333
from xdis.opcodes.opcode_12 import opcode_arg_fmt12, opcode_extended_fmt12
3434

3535
version_tuple = (1, 1) # 1.2 is the same
36+
python_implementation = cpython_implementation
3637

3738
loc = locals()
3839
init_opdata(loc, opcode_12, version_tuple)

xdis/opcodes/opcode_12.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# This is used from outside this module
2626
from xdis.cross_dis import findlabels # noqa
2727
from xdis.opcodes.base import ( # Although these aren't used here, they are exported; noqa
28-
cpython_implementation as python_implementation,
28+
cpython_implementation,
2929
finalize_opcodes,
3030
init_opdata,
3131
name_op,
@@ -36,6 +36,7 @@
3636
from xdis.opcodes.opcode_13 import opcode_arg_fmt13, opcode_extended_fmt13
3737

3838
version_tuple = (1, 2)
39+
python_implementation = cpython_implementation
3940

4041
loc = locals()
4142
init_opdata(loc, opcode_13, version_tuple)

xdis/opcodes/opcode_13.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
# This is used from outside this module
2626
from xdis.cross_dis import findlabels # noqa
27-
from xdis.opcodes.base import ( # Although these aren't used here, they are exported; noqa
28-
cpython_implementation as python_implementation,
27+
from xdis.opcodes.base import (
28+
cpython_implementation,
2929
def_op,
3030
finalize_opcodes,
3131
init_opdata,
@@ -35,6 +35,7 @@
3535
from xdis.opcodes.opcode_1x import opcode_extended_fmt_base1x, update_arg_fmt_base1x
3636

3737
version_tuple = (1, 3)
38+
python_implementation = cpython_implementation
3839

3940
loc = locals()
4041
init_opdata(loc, opcode_14, version_tuple)

xdis/opcodes/opcode_14.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
# This is used from outside this module
2626
from xdis.cross_dis import findlabels # noqa
2727
from xdis.opcodes.base import ( # Although these aren't used here, they are exported
28+
cpython_implementation,
2829
def_op,
2930
finalize_opcodes,
3031
init_opdata,
@@ -35,7 +36,7 @@
3536
from xdis.opcodes.opcode_1x import opcode_extended_fmt_base1x, update_arg_fmt_base1x
3637

3738
version_tuple = (1, 4)
38-
python_implementation = "CPython"
39+
python_implementation = cpython_implementation
3940

4041
loc = locals()
4142
init_opdata(loc, opcode_15, version_tuple)

xdis/opcodes/opcode_15.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,22 @@
1616
"""
1717
CPython 1.5 bytecode opcodes
1818
19-
This is a like Python 1.5's opcode.py with some classification
19+
This is a like Python 1.5's dis.py with some classification
2020
of stack usage and information for formatting instructions.
2121
of stack usage.
2222
"""
2323

2424
import xdis.opcodes.opcode_1x as opcode_1x
2525
from xdis.opcodes.base import ( # Although these aren't used here, they are exported; noqa
26-
cpython_implementation as python_implementation,
26+
cpython_implementation,
2727
finalize_opcodes,
2828
init_opdata,
2929
update_pj2,
3030
)
3131
from xdis.opcodes.opcode_1x import opcode_extended_fmt_base1x, update_arg_fmt_base1x
3232

3333
version_tuple = (1, 5)
34+
python_implementation = cpython_implementation
3435

3536
loc = locals()
3637

xdis/opcodes/opcode_16.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
"""
1717
CPython 1.6 bytecode opcodes
1818
19-
This is used in bytecode disassembly. This is similar to the
20-
opcodes in Python's dis.py library.
19+
This is a like Python 1.6's dis.py with some classification
20+
of stack usage and information for formatting instructions.
21+
of stack usage.
2122
"""
2223

2324
import xdis.opcodes.opcode_15 as opcode_15
@@ -26,14 +27,15 @@
2627
from xdis.cross_dis import findlabels, findlinestarts # noqa
2728
from xdis.opcodes.base import ( # noqa
2829
call_op,
29-
cpython_implementation as python_implementation,
30+
cpython_implementation,
3031
finalize_opcodes,
3132
init_opdata,
3233
update_pj2,
3334
)
3435
from xdis.opcodes.opcode_2x import opcode_extended_fmt_base2x, update_arg_fmt_base2x
3536

3637
version_tuple = (1, 6)
38+
python_implementation = cpython_implementation
3739

3840
loc = locals()
3941

xdis/opcodes/opcode_20.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import xdis.opcodes.opcode_21 as opcode_21
2323
from xdis.opcodes.base import ( # noqa
24-
cpython_implementation as python_implementation,
24+
cpython_implementation,
2525
finalize_opcodes,
2626
init_opdata,
2727
rm_op,
@@ -30,6 +30,7 @@
3030
from xdis.opcodes.opcode_2x import opcode_extended_fmt_base2x, update_arg_fmt_base2x
3131

3232
version_tuple = (2, 0)
33+
python_implementation = cpython_implementation
3334

3435
loc = locals()
3536
init_opdata(loc, opcode_21, version_tuple)

xdis/opcodes/opcode_21.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import xdis.opcodes.opcode_22 as opcode_22
2323
from xdis.opcodes.base import ( # noqa
24-
cpython_implementation as python_implementation,
24+
cpython_implementation,
2525
finalize_opcodes,
2626
init_opdata,
2727
rm_op,
@@ -30,6 +30,7 @@
3030
from xdis.opcodes.opcode_2x import opcode_extended_fmt_base2x, update_arg_fmt_base2x
3131

3232
version_tuple = (2, 1)
33+
python_implementation = cpython_implementation
3334

3435
loc = locals()
3536
init_opdata(loc, opcode_22, version_tuple)

0 commit comments

Comments
 (0)