Mercurial > cpython-lru_cache-weakref
annotate Python/freeze_importlib.py @ 77115:352147bbefdb
#14862: Add missing names to os.__all__
| author | Petri Lehtinen <petri@digip.org> |
|---|---|
| date | Wed, 23 May 2012 21:36:16 +0300 |
| parents | 6e5c517da087 |
| children | 7d86e207598f |
| 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') |
|
76357
e97ead910cc1
Avoid compiler warning due to stray \x00
Antoine Pitrou <solipsis@pitrou.net>
parents:
76335
diff
changeset
|
25 with open(output_path, 'w', encoding='utf-8') as output_file: |
|
76303
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
26 output_file.write('\n'.join(lines)) |
|
76357
e97ead910cc1
Avoid compiler warning due to stray \x00
Antoine Pitrou <solipsis@pitrou.net>
parents:
76335
diff
changeset
|
27 output_file.write('/* Mercurial binary marker: \x00 */') |
|
76989
6e5c517da087
Avoid "warning: no newline at end of file" in importlib.h.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76357
diff
changeset
|
28 # Avoid a compiler warning for lack of EOL |
|
6e5c517da087
Avoid "warning: no newline at end of file" in importlib.h.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76357
diff
changeset
|
29 output_file.write('\n') |
|
76303
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
30 |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
31 |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
32 if __name__ == '__main__': |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
33 import sys |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
34 |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
35 args = sys.argv[1:] |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
36 if len(args) != 2: |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
37 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
|
38 sys.exit(1) |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
39 |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
40 main(*args) |
