Skip to content

Commit 6a00ba0

Browse files
author
rocky
committed
Small tweaks
1 parent afa7bd1 commit 6a00ba0

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

xdis/load.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,12 @@ def load_module_from_file_object(
380380

381381

382382
def 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(
425434
if __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:

xdis/roundtrip_pyc.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import sys
4141
import tempfile
4242
import types
43-
from typing import List, Union
43+
from typing import Union
4444

4545
from xdis.codetype.base import CodeBase
4646
from xdis.load import load_module_from_file_object, write_bytecode_file
@@ -231,6 +231,7 @@ def roundtrip_pyc(input_path: str, unlink_on_success: bool, verbose: bool) -> in
231231
orig_magic_int,
232232
compilation_ts=orig_timestamp,
233233
filesize=orig_source_size or 0,
234+
allow_native=False,
234235
)
235236
except TypeError:
236237
# Older/newer signatures might name the timestamp param differently; try without names
@@ -261,7 +262,7 @@ def roundtrip_pyc(input_path: str, unlink_on_success: bool, verbose: bool) -> in
261262
except Exception as e:
262263
print(f"WARNING: could not do raw byte comparison: {e}", file=sys.stderr)
263264

264-
print("Original file:", input_path)
265+
print("Input file:", input_path)
265266
if verbose:
266267
print("Rewritten file:", tf_name)
267268
print("Raw-bytes identical:", same_bytes)

0 commit comments

Comments
 (0)