annotate Lib/py_compile.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 6da717bfe425
children c7f04c09dc56
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
24937
219e70389072 Refactor: Remove some code that was obsoleted when this module was
Fred Drake <fdrake@acm.org>
parents: 23625
diff changeset
6 import __builtin__
2853
d42941d3a95f Get magic number from interpreter (using module imp).
Sjoerd Mullender <sjoerd@acm.org>
parents: 2665
diff changeset
7 import imp
24937
219e70389072 Refactor: Remove some code that was obsoleted when this module was
Fred Drake <fdrake@acm.org>
parents: 23625
diff changeset
8 import marshal
219e70389072 Refactor: Remove some code that was obsoleted when this module was
Fred Drake <fdrake@acm.org>
parents: 23625
diff changeset
9 import os
219e70389072 Refactor: Remove some code that was obsoleted when this module was
Fred Drake <fdrake@acm.org>
parents: 23625
diff changeset
10 import sys
219e70389072 Refactor: Remove some code that was obsoleted when this module was
Fred Drake <fdrake@acm.org>
parents: 23625
diff changeset
11 import traceback
219e70389072 Refactor: Remove some code that was obsoleted when this module was
Fred Drake <fdrake@acm.org>
parents: 23625
diff changeset
12
2853
d42941d3a95f Get magic number from interpreter (using module imp).
Sjoerd Mullender <sjoerd@acm.org>
parents: 2665
diff changeset
13 MAGIC = imp.get_magic()
1828
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
15 __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
16
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
17
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
18 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
19 """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
20 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
21
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
22 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
23
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
24 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
25
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
26 where
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27250
diff changeset
27
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
28 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
29 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
30 'exc_type_name'
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27250
diff changeset
31
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
32 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
33 can be accesses as class variable 'exc_value'
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27250
diff changeset
34
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
35 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
36 can be accesses as class variable 'file'
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27250
diff changeset
37
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
38 msg: string message to be written as error message
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
39 If no value is given, a default exception message will be given,
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
40 consistent with 'standard' py_compile output.
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
41 message (or default) can be accesses as class variable 'msg'
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27250
diff changeset
42
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
43 """
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27250
diff changeset
44
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
45 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
46 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
47 if exc_type is SyntaxError:
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
48 tbtext = ''.join(traceback.format_exception_only(exc_type, exc_value))
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
49 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
50 else:
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
51 errmsg = "Sorry: %s: %s" % (exc_type_name,exc_value)
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27250
diff changeset
52
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
53 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
54
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
55 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
56 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
57 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
58 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
59
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
60 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
61 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
62
16562
623456576d6a __all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents: 16495
diff changeset
63
1828
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 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
65 """Internal; write a 32-bit int to a file in little-endian order."""
7109
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
66 f.write(chr( x & 0xff))
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
67 f.write(chr((x >> 8) & 0xff))
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
68 f.write(chr((x >> 16) & 0xff))
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
69 f.write(chr((x >> 24) & 0xff))
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
70
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
71 def compile(file, cfile=None, dfile=None, doraise=False):
7109
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
72 """Byte-compile one Python source file to Python bytecode.
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
73
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
74 Arguments:
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
75
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
76 file: source filename
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
77 cfile: target filename; defaults to source with 'c' or 'o' appended
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
78 ('c' normally, 'o' in optimizing mode, giving .pyc or .pyo)
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
79 dfile: purported filename; defaults to source (this is the filename
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
80 that will show up in error messages)
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
81 doraise: flag indicating whether or not an exception should be
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
82 raised when a compile error is found. If an exception
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
83 occurs and this flag is set to False, a string
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
84 indicating the nature of the exception will be printed,
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
85 and the function will return to the caller. If an
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
86 exception occurs and this flag is set to True, a
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
87 PyCompileError exception will be raised.
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27250
diff changeset
88
7109
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
89 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
90 execution efficiency -- Python itself byte-compiles a module when
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
91 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
92 corresponding .pyc (or .pyo) file.
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
93
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
94 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
95 good idea to byte-compile all modules upon installation, since
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
96 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
97 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
98 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
99 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
100
7109
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
101 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
102 byte-compile all installed files (or all files in selected
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
103 directories).
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
104
f43667866d5e Added docstrings.
Guido van Rossum <guido@python.org>
parents: 6623
diff changeset
105 """
59630
b74f1bb88e60 clean up files correctly
Benjamin Peterson <benjamin@python.org>
parents: 59629
diff changeset
106 with open(file, 'U') as f:
b74f1bb88e60 clean up files correctly
Benjamin Peterson <benjamin@python.org>
parents: 59629
diff changeset
107 try:
b74f1bb88e60 clean up files correctly
Benjamin Peterson <benjamin@python.org>
parents: 59629
diff changeset
108 timestamp = long(os.fstat(f.fileno()).st_mtime)
b74f1bb88e60 clean up files correctly
Benjamin Peterson <benjamin@python.org>
parents: 59629
diff changeset
109 except AttributeError:
b74f1bb88e60 clean up files correctly
Benjamin Peterson <benjamin@python.org>
parents: 59629
diff changeset
110 timestamp = long(os.stat(file).st_mtime)
b74f1bb88e60 clean up files correctly
Benjamin Peterson <benjamin@python.org>
parents: 59629
diff changeset
111 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
112 try:
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
113 codeobject = __builtin__.compile(codestring, dfile or file,'exec')
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
114 except Exception,err:
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
115 py_exc = PyCompileError(err.__class__,err.args,dfile or file)
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
116 if doraise:
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 24938
diff changeset
117 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
118 else:
35060
2eb569b6ce23 Patch #1171150: add a newline to py_compile error output
Georg Brandl <georg@python.org>
parents: 28098
diff changeset
119 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
120 return
23622
5edd80726d96 Replace boolean test with is None.
Raymond Hettinger <python@rcn.com>
parents: 23029
diff changeset
121 if cfile is None:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7510
diff changeset
122 cfile = file + (__debug__ and 'c' or 'o')
59630
b74f1bb88e60 clean up files correctly
Benjamin Peterson <benjamin@python.org>
parents: 59629
diff changeset
123 with open(cfile, 'wb') as fc:
b74f1bb88e60 clean up files correctly
Benjamin Peterson <benjamin@python.org>
parents: 59629
diff changeset
124 fc.write('\0\0\0\0')
b74f1bb88e60 clean up files correctly
Benjamin Peterson <benjamin@python.org>
parents: 59629
diff changeset
125 wr_long(fc, timestamp)
b74f1bb88e60 clean up files correctly
Benjamin Peterson <benjamin@python.org>
parents: 59629
diff changeset
126 marshal.dump(codeobject, fc)
b74f1bb88e60 clean up files correctly
Benjamin Peterson <benjamin@python.org>
parents: 59629
diff changeset
127 fc.flush()
b74f1bb88e60 clean up files correctly
Benjamin Peterson <benjamin@python.org>
parents: 59629
diff changeset
128 fc.seek(0, 0)
b74f1bb88e60 clean up files correctly
Benjamin Peterson <benjamin@python.org>
parents: 59629
diff changeset
129 fc.write(MAGIC)
24938
e9500709d49c Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents: 24937
diff changeset
130
e9500709d49c Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents: 24937
diff changeset
131 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
132 """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
133
e9500709d49c Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents: 24937
diff changeset
134 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
135 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
136 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
137 structure to locate source files; it only compiles files named
60110
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
138 explicitly. If '-' is the only parameter in args, the list of
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
139 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
140
e9500709d49c Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents: 24937
diff changeset
141 """
e9500709d49c Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents: 24937
diff changeset
142 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
143 args = sys.argv[1:]
45690
36fef99af18d #2225: return nonzero status code from py_compile if not all files could be compiled.
Georg Brandl <georg@python.org>
parents: 35060
diff changeset
144 rv = 0
60110
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
145 if args == ['-']:
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
146 while True:
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
147 filename = sys.stdin.readline()
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
148 if not filename:
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
149 break
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
150 filename = filename.rstrip('\n')
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
151 try:
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
152 compile(filename, doraise=True)
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
153 except PyCompileError as error:
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
154 rv = 1
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
155 sys.stderr.write("%s\n" % error.msg)
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
156 except IOError as error:
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
157 rv = 1
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
158 sys.stderr.write("%s\n" % error)
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
159 else:
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
160 for filename in args:
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
161 try:
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
162 compile(filename, doraise=True)
60780
6da717bfe425 fix typo in r79533, introduced by the fix for issue #8233
Matthias Klose <doko@ubuntu.com>
parents: 60110
diff changeset
163 except PyCompileError as error:
60110
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
164 # return value to indicate at least one failure
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
165 rv = 1
5920f9a5e22d - Issue #8233: When run as a script, py_compile.py optionally takes a single
Barry Warsaw <barry@python.org>
parents: 59704
diff changeset
166 sys.stderr.write(error.msg)
45690
36fef99af18d #2225: return nonzero status code from py_compile if not all files could be compiled.
Georg Brandl <georg@python.org>
parents: 35060
diff changeset
167 return rv
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27250
diff changeset
168
24938
e9500709d49c Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents: 24937
diff changeset
169 if __name__ == "__main__":
45690
36fef99af18d #2225: return nonzero status code from py_compile if not all files could be compiled.
Georg Brandl <georg@python.org>
parents: 35060
diff changeset
170 sys.exit(main())