annotate Python/freeze_importlib.py @ 76357:e97ead910cc1

Avoid compiler warning due to stray \x00
author Antoine Pitrou <solipsis@pitrou.net>
date Mon, 16 Apr 2012 18:30:54 +0200
parents c3b823e8dcd3
children 6e5c517da087
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 */')
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)