summaryrefslogtreecommitdiffstats
path: root/Lib/importlib/_bootstrap.py
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2012-01-25 23:58:03 (GMT)
committerBrett Cannon <brett@python.org>2012-01-25 23:58:03 (GMT)
commitc264e3ee206d5e5d1cb2aeff2cb0cd864dce83c8 (patch)
treedd9152609b6e0afb824d9aa731cae056e0cb1cb1 /Lib/importlib/_bootstrap.py
parentb0f30c9891a3ef45d616bf3ae73d764fd539946b (diff)
downloadcpython-c264e3ee206d5e5d1cb2aeff2cb0cd864dce83c8.zip
cpython-c264e3ee206d5e5d1cb2aeff2cb0cd864dce83c8.tar.gz
cpython-c264e3ee206d5e5d1cb2aeff2cb0cd864dce83c8.tar.bz2
Move some code from importlib.__init__ to importlib._bootstrap that
does not need to be exposed from C code for bootstrapping reasons.
Diffstat (limited to 'Lib/importlib/_bootstrap.py')
-rw-r--r--Lib/importlib/_bootstrap.py39
1 files changed, 35 insertions, 4 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index 11d7d0a..e81fa9f 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -18,6 +18,37 @@ work. One should use importlib as the public-facing version of this module.
# Bootstrap-related code ######################################################
+# TODO: Expose from marshal
+def _w_long(x):
+ """Convert a 32-bit integer to little-endian.
+
+ XXX Temporary until marshal's long functions are exposed.
+
+ """
+ x = int(x)
+ int_bytes = []
+ int_bytes.append(x & 0xFF)
+ int_bytes.append((x >> 8) & 0xFF)
+ int_bytes.append((x >> 16) & 0xFF)
+ int_bytes.append((x >> 24) & 0xFF)
+ return bytearray(int_bytes)
+
+
+# TODO: Expose from marshal
+def _r_long(int_bytes):
+ """Convert 4 bytes in little-endian to an integer.
+
+ XXX Temporary until marshal's long function are exposed.
+
+ """
+ x = int_bytes[0]
+ x |= int_bytes[1] << 8
+ x |= int_bytes[2] << 16
+ x |= int_bytes[3] << 24
+ return x
+
+
+
# XXX Could also expose Modules/getpath.c:joinpath()
def _path_join(*args):
"""Replacement for os.path.join."""
@@ -353,14 +384,14 @@ class _LoaderBasics:
except KeyError:
pass
else:
- if marshal._r_long(raw_timestamp) != source_mtime:
+ if _r_long(raw_timestamp) != source_mtime:
raise ImportError("bytecode is stale for {}".format(fullname))
try:
source_size = source_stats['size'] & 0xFFFFFFFF
except KeyError:
pass
else:
- if marshal._r_long(raw_size) != source_size:
+ if _r_long(raw_size) != source_size:
raise ImportError("bytecode is stale for {}".format(fullname))
# Can't return the code object as errors from marshal loading need to
# propagate even when source is available.
@@ -472,8 +503,8 @@ class SourceLoader(_LoaderBasics):
# their own cached file format, this block of code will most likely
# throw an exception.
data = bytearray(imp.get_magic())
- data.extend(marshal._w_long(source_mtime))
- data.extend(marshal._w_long(len(source_bytes)))
+ data.extend(_w_long(source_mtime))
+ data.extend(_w_long(len(source_bytes)))
data.extend(marshal.dumps(code_object))
try:
self.set_data(bytecode_path, data)