@@ -380,7 +380,12 @@ def load_module_from_file_object(
380380
381381
382382def write_bytecode_file (
383- bytecode_path , code_obj , magic_int , compilation_ts = None , filesize : int = 0
383+ bytecode_path ,
384+ code_obj ,
385+ magic_int ,
386+ compilation_ts = None ,
387+ filesize : int = 0 ,
388+ allow_native : bool = True ,
384389) -> None :
385390 """Write bytecode file _bytecode_path_, with code for having Python
386391 magic_int (i.e. bytecode associated with some version of Python)
@@ -407,15 +412,19 @@ def write_bytecode_file(
407412 if version_tuple >= (3 , 3 ):
408413 # In Python 3.3+, these 4 bytes are the size of the source code_obj file (mod 2^32)
409414 fp .write (pack ("<I" , filesize ))
410- if isinstance (code_obj , types .CodeType ):
415+ if allow_native and isinstance (code_obj , types .CodeType ):
411416 fp .write (marshal .dumps (code_obj ))
412417 else :
413418 code_sequence = xdis .marsh .dumps (code_obj , python_version = version_tuple )
414419 if isinstance (code_sequence , str ):
415420 # Python 1.x uses code strings, not bytes. To get this into bytes needed by
416421 # fp.write, encode the string using 'latin-1' and 'unicode_escape' to convert escape sequences
417422 # into the raw byte values. 'latin-1' is a single-byte encoding that works well for this.
418- code_bytes = code_sequence .encode ('latin-1' ).decode ('unicode_escape' ).encode ('latin-1' )
423+ code_bytes = (
424+ code_sequence .encode ("latin-1" )
425+ .decode ("unicode_escape" )
426+ .encode ("latin-1" )
427+ )
419428 else :
420429 code_bytes = code_sequence
421430 fp .write (code_bytes )
@@ -425,8 +434,8 @@ def write_bytecode_file(
425434if __name__ == "__main__" :
426435 co = load_file (__file__ )
427436 obj_path = check_object_path (__file__ )
428- version , timestamp , magic_int , co2 , pypy , source_size , sip_hash , file_offsets = load_module (
429- obj_path
437+ version , timestamp , magic_int , co2 , pypy , source_size , sip_hash , file_offsets = (
438+ load_module ( obj_path )
430439 )
431440 print ("version" , version , "magic int" , magic_int , "is_pypy" , pypy )
432441 if timestamp is not None :
0 commit comments