annotate Lib/py_compile.py @ 79691:05c8928362ee

Merge __next__ method link fixes with 3.3.
author Ezio Melotti <ezio.melotti@gmail.com>
date Fri, 12 Oct 2012 13:46:37 +0300
parents 87331661042b
children 7d69d04522e3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7109
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
1 """Routine to "compile" a .py file to a .pyc (or .pyo) file.
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
2
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
3 This module has intimate knowledge of the format of .pyc files.
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
4 """
1828
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43798
diff changeset
6 import builtins
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 60113
diff changeset
7 import errno
2853
d42941d3a95f Get magic number from interpreter (using module imp).
Sjoerd Mullender <sjoerd@acm.org>
parents: 2665
diff changeset
8 import imp
24937
219e70389072 Refactor: Remove some code that was obsoleted when this module was
Fred Drake <fdrake@acm.org>
parents: 23625
diff changeset
9 import marshal
219e70389072 Refactor: Remove some code that was obsoleted when this module was
Fred Drake <fdrake@acm.org>
parents: 23625
diff changeset
10 import os
219e70389072 Refactor: Remove some code that was obsoleted when this module was
Fred Drake <fdrake@acm.org>
parents: 23625
diff changeset
11 import sys
59702
e15a8a476494 kill py_compile's homemade encoding detection in favor of tokenize.detect_encoding() (see #8168)
Benjamin Peterson <benjamin@python.org>
parents: 59695
diff changeset
12 import tokenize
24937
219e70389072 Refactor: Remove some code that was obsoleted when this module was
Fred Drake <fdrake@acm.org>
parents: 23625
diff changeset
13 import traceback
219e70389072 Refactor: Remove some code that was obsoleted when this module was
Fred Drake <fdrake@acm.org>
parents: 23625
diff changeset
14
2853
d42941d3a95f Get magic number from interpreter (using module imp).
Sjoerd Mullender <sjoerd@acm.org>
parents: 2665
diff changeset
15 MAGIC = imp.get_magic()
1828
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
17 __all__ = ["compile", "main", "PyCompileError"]
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
18
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
19
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
20 class PyCompileError(Exception):
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
21 """Exception raised when an error occurs while attempting to
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
22 compile the file.
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
23
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
24 To raise this exception, use
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
25
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
26 raise PyCompileError(exc_type,exc_value,file[,msg])
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
27
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
28 where
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27250
diff changeset
29
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
30 exc_type: exception type to be used in error message
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
31 type name can be accesses as class variable
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
32 'exc_type_name'
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27250
diff changeset
33
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
34 exc_value: exception value to be used in error message
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
35 can be accesses as class variable 'exc_value'
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27250
diff changeset
36
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
37 file: name of file being compiled to be used in error message
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
38 can be accesses as class variable 'file'
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27250
diff changeset
39
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
40 msg: string message to be written as error message
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 60113
diff changeset
41 If no value is given, a default exception message will be
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 60113
diff changeset
42 given, consistent with 'standard' py_compile output.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 60113
diff changeset
43 message (or default) can be accesses as class variable
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 60113
diff changeset
44 'msg'
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27250
diff changeset
45
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
46 """
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27250
diff changeset
47
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
48 def __init__(self, exc_type, exc_value, file, msg=''):
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
49 exc_type_name = exc_type.__name__
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
50 if exc_type is SyntaxError:
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 60113
diff changeset
51 tbtext = ''.join(traceback.format_exception_only(
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 60113
diff changeset
52 exc_type, exc_value))
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
53 errmsg = tbtext.replace('File "<string>"', 'File "%s"' % file)
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
54 else:
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
55 errmsg = "Sorry: %s: %s" % (exc_type_name,exc_value)
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27250
diff changeset
56
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
57 Exception.__init__(self,msg or errmsg,exc_type_name,exc_value,file)
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
58
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
59 self.exc_type_name = exc_type_name
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
60 self.exc_value = exc_value
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
61 self.file = file
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
62 self.msg = msg or errmsg
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
63
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
64 def __str__(self):
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
65 return self.msg
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
66
16562
623456576d6a __all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents: 16495
diff changeset
67
1828
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 def wr_long(f, x):
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 9276
diff changeset
69 """Internal; write a 32-bit int to a file in little-endian order."""
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 60113
diff changeset
70 f.write(bytes([x & 0xff,
41812
50dafa22b8a4 Make test_zipfile pass.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
71 (x >> 8) & 0xff,
50dafa22b8a4 Make test_zipfile pass.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
72 (x >> 16) & 0xff,
50dafa22b8a4 Make test_zipfile pass.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
73 (x >> 24) & 0xff]))
7109
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
74
66685
713c6b6ca5ce Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents: 66097
diff changeset
75 def compile(file, cfile=None, dfile=None, doraise=False, optimize=-1):
7109
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
76 """Byte-compile one Python source file to Python bytecode.
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
77
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 60113
diff changeset
78 :param file: The source file name.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 60113
diff changeset
79 :param cfile: The target byte compiled file name. When not given, this
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 60113
diff changeset
80 defaults to the PEP 3147 location.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 60113
diff changeset
81 :param dfile: Purported file name, i.e. the file name that shows up in
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 60113
diff changeset
82 error messages. Defaults to the source file name.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 60113
diff changeset
83 :param doraise: Flag indicating whether or not an exception should be
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 60113
diff changeset
84 raised when a compile error is found. If an exception occurs and this
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 60113
diff changeset
85 flag is set to False, a string indicating the nature of the exception
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 60113
diff changeset
86 will be printed, and the function will return to the caller. If an
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 60113
diff changeset
87 exception occurs and this flag is set to True, a PyCompileError
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 60113
diff changeset
88 exception will be raised.
66685
713c6b6ca5ce Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents: 66097
diff changeset
89 :param optimize: The optimization level for the compiler. Valid values
713c6b6ca5ce Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents: 66097
diff changeset
90 are -1, 0, 1 and 2. A value of -1 means to use the optimization
713c6b6ca5ce Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents: 66097
diff changeset
91 level of the current interpreter, as given by -O command line options.
713c6b6ca5ce Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents: 66097
diff changeset
92
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 60113
diff changeset
93 :return: Path to the resulting byte compiled file.
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27250
diff changeset
94
7109
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
95 Note that it isn't necessary to byte-compile Python modules for
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
96 execution efficiency -- Python itself byte-compiles a module when
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
97 it is loaded, and if it can, writes out the bytecode to the
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
98 corresponding .pyc (or .pyo) file.
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
99
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
100 However, if a Python installation is shared between users, it is a
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
101 good idea to byte-compile all modules upon installation, since
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
102 other users may not be able to write in the source directories,
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
103 and thus they won't be able to write the .pyc/.pyo file, and then
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
104 they would be byte-compiling every module each time it is loaded.
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
105 This can slow down program start-up considerably.
1828
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106
7109
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
107 See compileall.py for a script/module that uses this module to
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
108 byte-compile all installed files (or all files in selected
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
109 directories).
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
110 """
66097
306339c31758 Issue #10335: Add tokenize.open(), detect the file encoding using
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61439
diff changeset
111 with tokenize.open(file) as f:
59695
3f4c7c4eef62 Merged revisions 78971-78972 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
112 try:
74358
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73773
diff changeset
113 st = os.fstat(f.fileno())
59695
3f4c7c4eef62 Merged revisions 78971-78972 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
114 except AttributeError:
74358
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73773
diff changeset
115 st = os.stat(file)
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73773
diff changeset
116 timestamp = int(st.st_mtime)
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73773
diff changeset
117 size = st.st_size & 0xFFFFFFFF
59695
3f4c7c4eef62 Merged revisions 78971-78972 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
118 codestring = f.read()
9169
6be343da2914 Fix suggested by Sjoerd (long ago!) to get a better error message when
Guido van Rossum <guido@python.org>
parents: 8932
diff changeset
119 try:
66685
713c6b6ca5ce Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents: 66097
diff changeset
120 codeobject = builtins.compile(codestring, dfile or file, 'exec',
713c6b6ca5ce Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents: 66097
diff changeset
121 optimize=optimize)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 35060
diff changeset
122 except Exception as err:
42524
346d0b84030b A small tweak to avoid calling traceback.format_exception_only() with
Guido van Rossum <guido@python.org>
parents: 42286
diff changeset
123 py_exc = PyCompileError(err.__class__, err, dfile or file)
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
124 if doraise:
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
125 raise py_exc
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
126 else:
35060
2eb569b6ce23 Patch #1171150: add a newline to py_compile error output
Georg Brandl <georg@python.org>
parents: 28098
diff changeset
127 sys.stderr.write(py_exc.msg + '\n')
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
128 return
23622
5edd80726d96 Replace boolean test with is None.
Raymond Hettinger <python@rcn.com>
parents: 23029
diff changeset
129 if cfile is None:
66685
713c6b6ca5ce Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents: 66097
diff changeset
130 if optimize >= 0:
713c6b6ca5ce Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents: 66097
diff changeset
131 cfile = imp.cache_from_source(file, debug_override=not optimize)
713c6b6ca5ce Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents: 66097
diff changeset
132 else:
713c6b6ca5ce Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents: 66097
diff changeset
133 cfile = imp.cache_from_source(file)
61439
fcb21d2ceb73 Create __pycache__ dir when the pyc path is explicitly given
Benjamin Peterson <benjamin@python.org>
parents: 60781
diff changeset
134 try:
73773
661fb211f220 Issue #12618: py_compile cannot create files in current directory
Meador Inge <meadori@gmail.com>
parents: 66685
diff changeset
135 dirname = os.path.dirname(cfile)
661fb211f220 Issue #12618: py_compile cannot create files in current directory
Meador Inge <meadori@gmail.com>
parents: 66685
diff changeset
136 if dirname:
661fb211f220 Issue #12618: py_compile cannot create files in current directory
Meador Inge <meadori@gmail.com>
parents: 66685
diff changeset
137 os.makedirs(dirname)
61439
fcb21d2ceb73 Create __pycache__ dir when the pyc path is explicitly given
Benjamin Peterson <benjamin@python.org>
parents: 60781
diff changeset
138 except OSError as error:
fcb21d2ceb73 Create __pycache__ dir when the pyc path is explicitly given
Benjamin Peterson <benjamin@python.org>
parents: 60781
diff changeset
139 if error.errno != errno.EEXIST:
fcb21d2ceb73 Create __pycache__ dir when the pyc path is explicitly given
Benjamin Peterson <benjamin@python.org>
parents: 60781
diff changeset
140 raise
59695
3f4c7c4eef62 Merged revisions 78971-78972 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
141 with open(cfile, 'wb') as fc:
3f4c7c4eef62 Merged revisions 78971-78972 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
142 fc.write(b'\0\0\0\0')
3f4c7c4eef62 Merged revisions 78971-78972 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
143 wr_long(fc, timestamp)
74358
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73773
diff changeset
144 wr_long(fc, size)
59695
3f4c7c4eef62 Merged revisions 78971-78972 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
145 marshal.dump(codeobject, fc)
3f4c7c4eef62 Merged revisions 78971-78972 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
146 fc.flush()
3f4c7c4eef62 Merged revisions 78971-78972 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
147 fc.seek(0, 0)
3f4c7c4eef62 Merged revisions 78971-78972 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48769
diff changeset
148 fc.write(MAGIC)
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 60113
diff changeset
149 return cfile
24938
e9500709d49c Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents: 24937
diff changeset
150
e9500709d49c Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents: 24937
diff changeset
151 def main(args=None):
e9500709d49c Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents: 24937
diff changeset
152 """Compile several source files.
e9500709d49c Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents: 24937
diff changeset
153
e9500709d49c Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents: 24937
diff changeset
154 The files named in 'args' (or on the command line, if 'args' is
e9500709d49c Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents: 24937
diff changeset
155 not specified) are compiled and the resulting bytecode is cached
e9500709d49c Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents: 24937
diff changeset
156 in the normal manner. This function does not search a directory
e9500709d49c Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents: 24937
diff changeset
157 structure to locate source files; it only compiles files named
60113
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
158 explicitly. If '-' is the only parameter in args, the list of
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
159 files is taken from standard input.
24938
e9500709d49c Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents: 24937
diff changeset
160
e9500709d49c Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents: 24937
diff changeset
161 """
e9500709d49c Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents: 24937
diff changeset
162 if args is None:
e9500709d49c Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents: 24937
diff changeset
163 args = sys.argv[1:]
45773
832c820e9d14 Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44098
diff changeset
164 rv = 0
60113
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
165 if args == ['-']:
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
166 while True:
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
167 filename = sys.stdin.readline()
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
168 if not filename:
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
169 break
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
170 filename = filename.rstrip('\n')
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
171 try:
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
172 compile(filename, doraise=True)
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
173 except PyCompileError as error:
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
174 rv = 1
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
175 sys.stderr.write("%s\n" % error.msg)
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
176 except IOError as error:
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
177 rv = 1
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
178 sys.stderr.write("%s\n" % error)
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
179 else:
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
180 for filename in args:
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
181 try:
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
182 compile(filename, doraise=True)
60781
506faddc8a9e Merged revisions 80274 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents: 60648
diff changeset
183 except PyCompileError as error:
60113
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
184 # return value to indicate at least one failure
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
185 rv = 1
4fcaad3f669b - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59707
diff changeset
186 sys.stderr.write(error.msg)
45773
832c820e9d14 Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44098
diff changeset
187 return rv
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27250
diff changeset
188
24938
e9500709d49c Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents: 24937
diff changeset
189 if __name__ == "__main__":
45773
832c820e9d14 Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44098
diff changeset
190 sys.exit(main())