Skip to content

Commit 3f768b3

Browse files
committed
use memoryview to avoid copying
1 parent 4b691eb commit 3f768b3

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

Lib/importlib/_bootstrap_external.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ def get_code(self, fullname):
797797
}
798798
try:
799799
flags = _classify_pyc(data, fullname, exc_details)
800-
bytes_data = data[16:]
800+
bytes_data = memoryview(data)[16:]
801801
hash_based = flags & 0b1 != 0
802802
if hash_based:
803803
check_source = flags & 0b10 != 0
@@ -943,7 +943,11 @@ def get_code(self, fullname):
943943
'path': path,
944944
}
945945
_classify_pyc(data, fullname, exc_details)
946-
return _compile_bytecode(data[16:], name=fullname, bytecode_path=path)
946+
return _compile_bytecode(
947+
memoryview(data)[16:],
948+
name=fullname,
949+
bytecode_path=path,
950+
)
947951

948952
def get_source(self, fullname):
949953
"""Return None as there is no source code."""

Lib/modulefinder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def load_module(self, fqname, fp, pathname, file_info):
292292
except ImportError as exc:
293293
self.msgout(2, "raise ImportError: " + str(exc), pathname)
294294
raise
295-
co = marshal.loads(data[16:])
295+
co = marshal.loads(memoryview(data)[16:])
296296
else:
297297
co = None
298298
m = self.add_module(fqname)

0 commit comments

Comments
 (0)