Mercurial > cpython-lru_cache-weakref
annotate Lib/py_compile.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | 36fef99af18d |
| children | e1de080c4556 |
| rev | line source |
|---|---|
| 7109 | 1 """Routine to "compile" a .py file to a .pyc (or .pyo) file. |
| 2 | |
| 3 This module has intimate knowledge of the format of .pyc files. | |
| 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 |
|
24937
219e70389072
Refactor: Remove some code that was obsoleted when this module was
Fred Drake <fdrake@acm.org>
parents:
23625
diff
changeset
|
64 # Define an internal helper according to the platform |
|
219e70389072
Refactor: Remove some code that was obsoleted when this module was
Fred Drake <fdrake@acm.org>
parents:
23625
diff
changeset
|
65 if os.name == "mac": |
| 28098 | 66 import MacOS |
|
24937
219e70389072
Refactor: Remove some code that was obsoleted when this module was
Fred Drake <fdrake@acm.org>
parents:
23625
diff
changeset
|
67 def set_creator_type(file): |
| 28098 | 68 MacOS.SetCreatorAndType(file, 'Pyth', 'PYC ') |
|
24937
219e70389072
Refactor: Remove some code that was obsoleted when this module was
Fred Drake <fdrake@acm.org>
parents:
23625
diff
changeset
|
69 else: |
|
219e70389072
Refactor: Remove some code that was obsoleted when this module was
Fred Drake <fdrake@acm.org>
parents:
23625
diff
changeset
|
70 def set_creator_type(file): |
|
219e70389072
Refactor: Remove some code that was obsoleted when this module was
Fred Drake <fdrake@acm.org>
parents:
23625
diff
changeset
|
71 pass |
|
219e70389072
Refactor: Remove some code that was obsoleted when this module was
Fred Drake <fdrake@acm.org>
parents:
23625
diff
changeset
|
72 |
|
1828
b464e1d0b2fb
New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 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
|
74 """Internal; write a 32-bit int to a file in little-endian order.""" |
| 7109 | 75 f.write(chr( x & 0xff)) |
| 76 f.write(chr((x >> 8) & 0xff)) | |
| 77 f.write(chr((x >> 16) & 0xff)) | |
| 78 f.write(chr((x >> 24) & 0xff)) | |
| 79 | |
|
27250
16a876746285
Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents:
24938
diff
changeset
|
80 def compile(file, cfile=None, dfile=None, doraise=False): |
| 7109 | 81 """Byte-compile one Python source file to Python bytecode. |
| 82 | |
| 83 Arguments: | |
| 84 | |
|
27250
16a876746285
Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents:
24938
diff
changeset
|
85 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
|
86 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
|
87 ('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
|
88 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
|
89 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
|
90 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
|
91 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
|
92 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
|
93 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
|
94 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
|
95 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
|
96 PyCompileError exception will be raised. |
|
27507
d9d57f5f98cd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27250
diff
changeset
|
97 |
| 7109 | 98 Note that it isn't necessary to byte-compile Python modules for |
| 99 execution efficiency -- Python itself byte-compiles a module when | |
| 100 it is loaded, and if it can, writes out the bytecode to the | |
| 101 corresponding .pyc (or .pyo) file. | |
| 102 | |
| 103 However, if a Python installation is shared between users, it is a | |
| 104 good idea to byte-compile all modules upon installation, since | |
| 105 other users may not be able to write in the source directories, | |
| 106 and thus they won't be able to write the .pyc/.pyo file, and then | |
| 107 they would be byte-compiling every module each time it is loaded. | |
| 108 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
|
109 |
| 7109 | 110 See compileall.py for a script/module that uses this module to |
| 111 byte-compile all installed files (or all files in selected | |
| 112 directories). | |
| 113 | |
| 114 """ | |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
16562
diff
changeset
|
115 f = open(file, 'U') |
| 7109 | 116 try: |
|
23625
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23622
diff
changeset
|
117 timestamp = long(os.fstat(f.fileno()).st_mtime) |
| 7109 | 118 except AttributeError: |
|
23625
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23622
diff
changeset
|
119 timestamp = long(os.stat(file).st_mtime) |
| 7109 | 120 codestring = f.read() |
| 121 f.close() | |
| 122 if codestring and codestring[-1] != '\n': | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7510
diff
changeset
|
123 codestring = codestring + '\n' |
|
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
|
124 try: |
|
27250
16a876746285
Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents:
24938
diff
changeset
|
125 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
|
126 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
|
127 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
|
128 if doraise: |
|
16a876746285
Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents:
24938
diff
changeset
|
129 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
|
130 else: |
|
35060
2eb569b6ce23
Patch #1171150: add a newline to py_compile error output
Georg Brandl <georg@python.org>
parents:
28098
diff
changeset
|
131 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
|
132 return |
|
23622
5edd80726d96
Replace boolean test with is None.
Raymond Hettinger <python@rcn.com>
parents:
23029
diff
changeset
|
133 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
|
134 cfile = file + (__debug__ and 'c' or 'o') |
| 7109 | 135 fc = open(cfile, 'wb') |
| 136 fc.write('\0\0\0\0') | |
| 137 wr_long(fc, timestamp) | |
| 138 marshal.dump(codeobject, fc) | |
| 139 fc.flush() | |
| 140 fc.seek(0, 0) | |
| 141 fc.write(MAGIC) | |
| 142 fc.close() | |
|
24937
219e70389072
Refactor: Remove some code that was obsoleted when this module was
Fred Drake <fdrake@acm.org>
parents:
23625
diff
changeset
|
143 set_creator_type(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
|
144 |
|
e9500709d49c
Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents:
24937
diff
changeset
|
145 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
|
146 """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
|
147 |
|
e9500709d49c
Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents:
24937
diff
changeset
|
148 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
|
149 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
|
150 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
|
151 structure to locate source files; it only compiles files named |
|
e9500709d49c
Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents:
24937
diff
changeset
|
152 explicitly. |
|
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 """ |
|
e9500709d49c
Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents:
24937
diff
changeset
|
155 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
|
156 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
|
157 rv = 0 |
|
24938
e9500709d49c
Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents:
24937
diff
changeset
|
158 for filename in args: |
|
27250
16a876746285
Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents:
24938
diff
changeset
|
159 try: |
|
16a876746285
Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents:
24938
diff
changeset
|
160 compile(filename, doraise=True) |
|
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
|
161 except PyCompileError, err: |
|
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
|
162 # return value to indicate at least one failure |
|
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
|
163 rv = 1 |
|
27250
16a876746285
Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents:
24938
diff
changeset
|
164 sys.stderr.write(err.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
|
165 return rv |
|
27507
d9d57f5f98cd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27250
diff
changeset
|
166 |
|
24938
e9500709d49c
Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents:
24937
diff
changeset
|
167 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
|
168 sys.exit(main()) |
