3030
3131__docformat__ = "restructuredtext"
3232
33+
3334def 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+ )
223232CodeTypeUnion = 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 ,
0 commit comments