Skip to content

Commit e48e799

Browse files
author
rocky
committed
Remove co_lnotab depreciatation messages
1 parent d65b622 commit e48e799

File tree

4 files changed

+46
-25
lines changed

4 files changed

+46
-25
lines changed

pytest/test_load_file.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from xdis import IS_GRAAL, IS_PYPY
66
from xdis.codetype import CodeTypeUnionFields
77
from xdis.load import check_object_path, load_file, load_module
8+
from xdis.version_info import PYTHON_VERSION_TRIPLE
89

910

1011
def get_srcdir() -> str:
@@ -59,6 +60,8 @@ def test_load_file() -> None:
5960
fields = CodeTypeUnionFields
6061

6162
for field in fields:
63+
if field == "co_lnotab" and PYTHON_VERSION_TRIPLE >= (3, 11):
64+
continue
6265
if hasattr(co_file, field):
6366
if field == "co_code" and (pypy or IS_PYPY):
6467
continue

pytest/test_std.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ def _create_python3_code_object():
202202

203203
code = list2bytecode(instructions, opcodes.opcode_34, varnames, consts)
204204
fn_code = compile(text, "<string>", "exec")
205+
206+
line_table_value = (
207+
fn_code.co_lnotab
208+
if PYTHON_VERSION_TRIPLE < (3, 11) and hasattr(fn_code, "co_lnotab")
209+
else fn_code.co_linetable
210+
)
211+
205212
return Code3(
206213
fn_code.co_argcount,
207214
fn_code.co_kwonlyargcount, # Add this in Python3
@@ -216,7 +223,7 @@ def _create_python3_code_object():
216223
fn_code.co_filename,
217224
fn_code.co_name,
218225
fn_code.co_firstlineno,
219-
fn_code.co_lnotab, # In general, You should adjust this
226+
line_table_value,
220227
fn_code.co_freevars,
221228
fn_code.co_cellvars,
222229
)

xdis/codetype/__init__.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
__docformat__ = "restructuredtext"
3232

33+
3334
def codeType2Portable(code, version_triple=PYTHON_VERSION_TRIPLE):
3435
"""Converts a native types.CodeType code object into a
3536
corresponding more flexible xdis Code type.
@@ -40,7 +41,11 @@ def codeType2Portable(code, version_triple=PYTHON_VERSION_TRIPLE):
4041
raise TypeError(
4142
f"parameter expected to be a types.CodeType type; is {type(code)} instead"
4243
)
43-
line_table_field = "co_lnotab" if hasattr(code, "co_lnotab") else "co_linetable"
44+
line_table_field = (
45+
"co_lnotab"
46+
if PYTHON_VERSION_TRIPLE < (3, 11) and hasattr(code, "co_lnotab")
47+
else "co_linetable"
48+
)
4449
line_table = getattr(code, line_table_field)
4550
if version_triple >= (3, 0):
4651
if version_triple < (3, 8):
@@ -60,7 +65,6 @@ def codeType2Portable(code, version_triple=PYTHON_VERSION_TRIPLE):
6065
line_table,
6166
code.co_freevars,
6267
code.co_cellvars,
63-
6468
# THINK ABOUT: If collection_order isn't defined, i.e. native code
6569
# type, should we try to extract it?
6670
code.collection_order if hasattr(code, "collection_order") else {},
@@ -146,13 +150,15 @@ def codeType2Portable(code, version_triple=PYTHON_VERSION_TRIPLE):
146150
co_lnotab=line_table,
147151
co_freevars=code.co_freevars, # not in 1.x
148152
co_cellvars=code.co_cellvars, # not in 1.x
149-
150153
# THINK ABOUT: If collection_order isn't defined, i.e. native code
151154
# type, should we try to extract it?
152-
collection_order=code.collection_order if hasattr(code, "collection_order") else {},
153-
reference_objects=code.reference_objects if hasattr(code, "reference_objects") else set(),
155+
collection_order=(
156+
code.collection_order if hasattr(code, "collection_order") else {}
157+
),
158+
reference_objects=(
159+
code.reference_objects if hasattr(code, "reference_objects") else set()
160+
),
154161
version_triple=version_triple,
155-
156162
)
157163
else:
158164
# 1.0 .. 1.5
@@ -219,7 +225,10 @@ def portableCodeType(version_triple=PYTHON_VERSION_TRIPLE):
219225
# In contrast to Code3, Code2, etc. you can use CodeTypeUnint for building
220226
# an incomplete code type, which might be converted to another code type
221227
# later.
222-
CodeTypeUnionFields = tuple(Code311FieldNames.split() + ["collection_order", "reference_objects", "version_triple"])
228+
CodeTypeUnionFields = tuple(
229+
Code311FieldNames.split()
230+
+ ["collection_order", "reference_objects", "version_triple"]
231+
)
223232
CodeTypeUnion = namedtuple("CodeTypeUnion", CodeTypeUnionFields)
224233

225234

@@ -229,21 +238,21 @@ def to_portable(
229238
co_argcount: int,
230239
co_posonlyargcount: Optional[int] = -1, # 3.8 .. 3.10
231240
co_kwonlyargcount: Optional[int] = -1, # 3.0+
232-
co_nlocals: int=0,
241+
co_nlocals: int = 0,
233242
co_stacksize: Optional[int] = -1, # 1.5+
234-
co_flags: int=0,
235-
co_code: Union[str, bytes]="", # 3.0+ this type changes from <str> to <bytes>
236-
co_consts: tuple[str, ...]=tuple(),
237-
co_names: tuple[str, ...]=tuple(),
238-
co_varnames: tuple[str, ...]=tuple(),
239-
co_filename: str="??",
240-
co_name: str="??",
241-
co_qualname: str="??",
242-
co_firstlineno: int=-1,
243-
co_lnotab: str="", # 1.5+; 3.0+ this type changes from <str> to <bytes>
243+
co_flags: int = 0,
244+
co_code: Union[str, bytes] = "", # 3.0+ this type changes from <str> to <bytes>
245+
co_consts: tuple[str, ...] = tuple(),
246+
co_names: tuple[str, ...] = tuple(),
247+
co_varnames: tuple[str, ...] = tuple(),
248+
co_filename: str = "??",
249+
co_name: str = "??",
250+
co_qualname: str = "??",
251+
co_firstlineno: int = -1,
252+
co_lnotab: str = "", # 1.5+; 3.0+ this type changes from <str> to <bytes>
244253
# In 3.11 it is different
245-
co_freevars: tuple[str, ...]=tuple(), # 2.0+
246-
co_cellvars: tuple[str, ...]=tuple(), # 2.0+
254+
co_freevars: tuple[str, ...] = tuple(), # 2.0+
255+
co_cellvars: tuple[str, ...] = tuple(), # 2.0+
247256
co_exceptiontable=None, # 3.11+
248257
version_triple=PYTHON_VERSION_TRIPLE,
249258
collection_order: Dict[Union[set, frozenset, dict], Tuple[Any]] = {},
@@ -265,6 +274,7 @@ def to_portable(
265274
co_qualname=co_name if co_qualname is None else co_qualname,
266275
co_firstlineno=co_firstlineno,
267276
co_linetable=co_lnotab,
277+
co_lnotab=co_lnotab,
268278
co_freevars=co_freevars,
269279
co_cellvars=co_cellvars,
270280
co_exceptiontable=co_exceptiontable,

xdis/codetype/code311.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
# "posonlyargcount" is not used, but it is in other Python versions, so it
2929
# has to be included since this structure is used as the Union type
3030
# for all code types.
31+
#
32+
# Methods co_lines and co_positions do not get added to field names.
3133
Code311FieldNames = """
3234
co_argcount
3335
co_posonlyargcount
@@ -46,11 +48,12 @@
4648
co_qualname
4749
co_firstlineno
4850
co_linetable
51+
co_lnotab
4952
co_exceptiontable
5053
"""
5154

5255
Code311FieldTypes = deepcopy(Code310FieldTypes)
53-
Code311FieldTypes.update({"co_qualname": str, "co_exceptiontable": bytes})
56+
Code311FieldTypes.update({"co_qualname": str, "co_exceptiontable": bytes, "co_linetable": bytes})
5457

5558

5659
##### Parse location table #####
@@ -110,7 +113,7 @@ def get_value(b):
110113
current_value = 0
111114
shift_amt = 0
112115

113-
def decode_signed_varint(s: int):
116+
def decode_signed_varint(s: int) -> int:
114117
return -(s >> 1) if s & 1 else (s >> 1)
115118

116119
entries = (
@@ -453,8 +456,6 @@ def to_native(self) -> CodeType:
453456
except AssertionError as e:
454457
raise TypeError(e)
455458

456-
if code.co_exceptiontable is None:
457-
code.co_exceptiontable = b""
458459
return types.CodeType(
459460
code.co_argcount,
460461
code.co_posonlyargcount,

0 commit comments

Comments
 (0)