Skip to content

Commit

Permalink
Merge pull request #135 from rocky/3-10-linetable
Browse files Browse the repository at this point in the history
Revise for 3.10 linetable...
  • Loading branch information
rocky authored Jun 15, 2024
2 parents 481be4c + 5509b0d commit ce90dac
Show file tree
Hide file tree
Showing 8 changed files with 379 additions and 65 deletions.
1 change: 1 addition & 0 deletions pytest/test_codetype.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def test_codeType2Portable():
# Python 2.6-
five_code = five.func_code

xdis.codetype.portableCodeType()
cc = xdis.codetype.codeType2Portable(five_code)
assert xdis.codetype.portableCodeType() == type(cc)
new_code = cc.to_native()
Expand Down
13 changes: 12 additions & 1 deletion xdis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,16 @@
offset2line,
op_has_argument,
)
from xdis.codetype import Code2, Code3, Code13, Code15, Code38, codeType2Portable
from xdis.codetype import (
Code2,
Code3,
Code13,
Code15,
Code38,
Code310,
Code311,
codeType2Portable,
)
from xdis.codetype.base import code_has_star_arg, code_has_star_star_arg, iscode
from xdis.cross_dis import (
code_info,
Expand Down Expand Up @@ -170,6 +179,8 @@
"Code15",
"Code2",
"Code3",
"Code310",
"Code311",
"Code38",
"code_has_star_star_arg",
"code_has_star_arg",
Expand Down
45 changes: 35 additions & 10 deletions xdis/codetype/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) Copyright 2020-2021, 2023 by Rocky Bernstein
# (C) Copyright 2020-2021, 2023-2024 by Rocky Bernstein
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -26,6 +26,7 @@
from xdis.codetype.code20 import Code2
from xdis.codetype.code30 import Code3
from xdis.codetype.code38 import Code38
from xdis.codetype.code310 import Code310
from xdis.codetype.code311 import Code311, Code311FieldNames
from xdis.version_info import PYTHON_VERSION_TRIPLE

Expand All @@ -40,6 +41,8 @@ def codeType2Portable(code, version_tuple=PYTHON_VERSION_TRIPLE):
raise TypeError(
f"parameter expected to be a types.CodeType type; is {type(code)} instead"
)
line_table_field = "co_lnotab" if hasattr(code, "co_lnotab") else "co_linetable"
line_table = getattr(code, line_table_field)
if version_tuple >= (3, 0):
if version_tuple < (3, 8):
return Code3(
Expand All @@ -55,11 +58,11 @@ def codeType2Portable(code, version_tuple=PYTHON_VERSION_TRIPLE):
code.co_filename,
code.co_name,
code.co_firstlineno,
code.co_lnotab, # noqa
line_table,
code.co_freevars,
code.co_cellvars,
)
elif version_tuple < (3, 11):
elif version_tuple < (3, 10):
return Code38(
co_argcount=code.co_argcount,
co_posonlyargcount=code.co_posonlyargcount,
Expand All @@ -76,7 +79,26 @@ def codeType2Portable(code, version_tuple=PYTHON_VERSION_TRIPLE):
co_filename=code.co_filename,
co_name=code.co_name,
co_firstlineno=code.co_firstlineno,
co_lnotab=code.co_lnotab,
co_lnotab=line_table,
)
elif version_tuple[:2] == (3, 10):
return Code310(
co_argcount=code.co_argcount,
co_posonlyargcount=code.co_posonlyargcount,
co_kwonlyargcount=code.co_kwonlyargcount,
co_nlocals=code.co_nlocals,
co_stacksize=code.co_stacksize,
co_flags=code.co_flags,
co_code=code.co_code,
co_consts=code.co_consts,
co_names=code.co_names,
co_varnames=code.co_varnames,
co_freevars=code.co_freevars,
co_cellvars=code.co_cellvars,
co_filename=code.co_filename,
co_name=code.co_name,
co_firstlineno=code.co_firstlineno,
co_linetable=line_table,
)
else: # version tuple >= 3, 11
return Code311(
Expand All @@ -96,7 +118,7 @@ def codeType2Portable(code, version_tuple=PYTHON_VERSION_TRIPLE):
co_name=code.co_name,
co_qualname=code.co_qualname,
co_firstlineno=code.co_firstlineno,
co_linetable=code.co_lnotab,
co_linetable=line_table,
co_exceptiontable=code.co_exceptiontable,
)
elif version_tuple > (2, 0):
Expand All @@ -113,7 +135,7 @@ def codeType2Portable(code, version_tuple=PYTHON_VERSION_TRIPLE):
co_filename=code.co_filename,
co_name=code.co_name,
co_firstlineno=code.co_firstlineno,
co_lnotab=code.co_lnotab,
co_lnotab=line_table,
co_freevars=code.co_freevars, # not in 1.x
co_cellvars=code.co_cellvars, # not in 1.x
)
Expand Down Expand Up @@ -145,7 +167,7 @@ def codeType2Portable(code, version_tuple=PYTHON_VERSION_TRIPLE):
code.co_filename,
code.co_name,
code.co_firstlineno, # Not in 1.0..1.4
code.co_lnotab, # Not in 1.0..1.4
line_table, # Not in 1.0..1.4
)


Expand All @@ -159,9 +181,12 @@ def portableCodeType(version_tuple=PYTHON_VERSION_TRIPLE):
if version_tuple < (3, 8):
# 3.0 .. 3.7
return Code3
elif version_tuple < (3, 11):
# 3.8 ... 3.10
elif version_tuple < (3, 10):
# 3.8 ... 3.9
return Code38
elif version_tuple[:2] == (3, 10):
# 3.10
return Code310
else:
# 3.11 ...
return Code311
Expand Down Expand Up @@ -222,7 +247,7 @@ def to_portable(
co_name=co_name,
co_qualname=co_qualname,
co_firstlineno=co_firstlineno,
co_lnotab=co_lnotab,
co_linetable=co_lnotab,
co_freevars=co_freevars,
co_cellvars=co_cellvars,
co_exceptiontable=co_exceptiontable,
Expand Down
26 changes: 13 additions & 13 deletions xdis/codetype/code20.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) Copyright 2017-2021, 2023 by Rocky Bernstein
# (C) Copyright 2017-2021, 2023-2024 by Rocky Bernstein
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -64,18 +64,18 @@ def __init__(
# Keyword argument parameters in the call below is more robust.
# Since things change around, robustness is good.
super(Code2, self).__init__(
co_argcount = co_argcount,
co_nlocals = co_nlocals,
co_stacksize = co_stacksize,
co_flags = co_flags,
co_code = co_code,
co_consts = co_consts,
co_names = co_names,
co_varnames = co_varnames,
co_filename = co_filename,
co_name = co_name,
co_firstlineno = co_firstlineno,
co_lnotab = co_lnotab,
co_argcount=co_argcount,
co_nlocals=co_nlocals,
co_stacksize=co_stacksize,
co_flags=co_flags,
co_code=co_code,
co_consts=co_consts,
co_names=co_names,
co_varnames=co_varnames,
co_filename=co_filename,
co_name=co_name,
co_firstlineno=co_firstlineno,
co_lnotab=co_lnotab,
)
self.co_freevars = co_freevars
self.co_cellvars = co_cellvars
Expand Down
10 changes: 6 additions & 4 deletions xdis/codetype/code30.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@


class Code3(Code2):
"""Class for a Python3 code object used when a Python not in the range between 3.0 and 3.7
but is working on Python 3.0 ... 3.7 bytecode. It also functions as an object that can be used
to build or write a Python3 code object, since we allow mutable structures.
When done mutating, call method freeze().
"""
Class for a Python3 code object used when a Python not in the
range between 3.0 and 3.7 but is working on Python 3.0 ... 3.7
bytecode. It also functions as an object that can be used to
build or write a Python3 code object, since we allow mutable
structures. When done mutating, call method freeze().
For convenience in generating code objects, fields like
`co_consts`, co_names which are (immutable) tuples in the end-result can be stored
Expand Down
Loading

0 comments on commit ce90dac

Please sign in to comment.