Mercurial > cpython-lru_cache-weakref
annotate Python/freeze_importlib.py @ 76335:c3b823e8dcd3
Try to get hg to treat importlib.h as binary.
| author | Brett Cannon <brett@python.org> |
|---|---|
| date | Sun, 15 Apr 2012 19:05:31 -0400 |
| parents | 2dd046be2c88 |
| children | e97ead910cc1 |
| rev | line source |
|---|---|
|
76303
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
1 #! /usr/bin/env python |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
2 """Freeze importlib for use as the implementation of import.""" |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
3 import marshal |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
4 |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
5 |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
6 header = """/* Auto-generated by Python/freeze_importlib.py */""" |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
7 |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
8 |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
9 def main(input_path, output_path): |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
10 with open(input_path, 'r', encoding='utf-8') as input_file: |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
11 source = input_file.read() |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
12 |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
13 code = compile(source, '<frozen importlib._bootstrap>', 'exec') |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
14 |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
15 lines = [header] |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
16 lines.append('unsigned char _Py_M__importlib[] = {') |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
17 data = marshal.dumps(code) |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
18 # Code from Tools/freeze/makefreeze.py:writecode() |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
19 for i in range(0, len(data), 16): |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
20 line = [' '] |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
21 for c in data[i:i+16]: |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
22 line.append('%d,' % c) |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
23 lines.append(''.join(line)) |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
24 lines.append('};\n') |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
25 with open(output_path, 'w') as output_file: |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
26 output_file.write('\n'.join(lines)) |
|
76335
c3b823e8dcd3
Try to get hg to treat importlib.h as binary.
Brett Cannon <brett@python.org>
parents:
76303
diff
changeset
|
27 output_file.write('\u0000') |
|
76303
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
28 |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
29 |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
30 if __name__ == '__main__': |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
31 import sys |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
32 |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
33 args = sys.argv[1:] |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
34 if len(args) != 2: |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
35 print('Need to specify input and output file paths', file=sys.stderr) |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
36 sys.exit(1) |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
37 |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
38 main(*args) |
