summaryrefslogtreecommitdiffstats
path: root/Python/freeze_importlib.py
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2012-04-14 18:10:13 (GMT)
committerBrett Cannon <brett@python.org>2012-04-14 18:10:13 (GMT)
commitfd0741555b733f66c0a35c698d0cac5e73010ae0 (patch)
tree739b3aeb0a9d31f49dd334e5f57b5376b20d7dc7 /Python/freeze_importlib.py
parentd2cbd9053975d6d6a98adb23b2735b2125ed0626 (diff)
downloadcpython-fd0741555b733f66c0a35c698d0cac5e73010ae0.zip
cpython-fd0741555b733f66c0a35c698d0cac5e73010ae0.tar.gz
cpython-fd0741555b733f66c0a35c698d0cac5e73010ae0.tar.bz2
Issue #2377: Make importlib the implementation of __import__().
importlib._bootstrap is now frozen into Python/importlib.h and stored as _frozen_importlib in sys.modules. Py_Initialize() loads the frozen code along with sys and imp and then uses _frozen_importlib._install() to set builtins.__import__() w/ _frozen_importlib.__import__().
Diffstat (limited to 'Python/freeze_importlib.py')
-rw-r--r--Python/freeze_importlib.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/Python/freeze_importlib.py b/Python/freeze_importlib.py
new file mode 100644
index 0000000..a069a0b
--- /dev/null
+++ b/Python/freeze_importlib.py
@@ -0,0 +1,37 @@
+#! /usr/bin/env python
+"""Freeze importlib for use as the implementation of import."""
+import marshal
+
+
+header = """/* Auto-generated by Python/freeze_importlib.py */"""
+
+
+def main(input_path, output_path):
+ with open(input_path, 'r', encoding='utf-8') as input_file:
+ source = input_file.read()
+
+ code = compile(source, '<frozen importlib._bootstrap>', 'exec')
+
+ lines = [header]
+ lines.append('unsigned char _Py_M__importlib[] = {')
+ data = marshal.dumps(code)
+ # Code from Tools/freeze/makefreeze.py:writecode()
+ for i in range(0, len(data), 16):
+ line = [' ']
+ for c in data[i:i+16]:
+ line.append('%d,' % c)
+ lines.append(''.join(line))
+ lines.append('};\n')
+ with open(output_path, 'w') as output_file:
+ output_file.write('\n'.join(lines))
+
+
+if __name__ == '__main__':
+ import sys
+
+ args = sys.argv[1:]
+ if len(args) != 2:
+ print('Need to specify input and output file paths', file=sys.stderr)
+ sys.exit(1)
+
+ main(*args)