Mercurial > cpython-lru_cache-weakref
annotate Lib/py_compile.py @ 78069:a8aa77458e12 3.2
Add c_size_t and c_ssize_t to the ctypes summary table.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 12 Jul 2012 20:31:50 +0200 |
| parents | 661fb211f220 |
| children | 87331661042b |
| 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 |
|
44098
0889daf6d0d9
#1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents:
43798
diff
changeset
|
6 import builtins |
| 60648 | 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 | 41 If no value is given, a default exception message will be |
| 42 given, consistent with 'standard' py_compile output. | |
| 43 message (or default) can be accesses as class variable | |
| 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 | 51 tbtext = ''.join(traceback.format_exception_only( |
| 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 | 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 | 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 | 76 """Byte-compile one Python source file to Python bytecode. |
| 77 | |
| 60648 | 78 :param file: The source file name. |
| 79 :param cfile: The target byte compiled file name. When not given, this | |
| 80 defaults to the PEP 3147 location. | |
| 81 :param dfile: Purported file name, i.e. the file name that shows up in | |
| 82 error messages. Defaults to the source file name. | |
| 83 :param doraise: Flag indicating whether or not an exception should be | |
| 84 raised when a compile error is found. If an exception occurs and this | |
| 85 flag is set to False, a string indicating the nature of the exception | |
| 86 will be printed, and the function will return to the caller. If an | |
| 87 exception occurs and this flag is set to True, a PyCompileError | |
| 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 | 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 | 95 Note that it isn't necessary to byte-compile Python modules for |
| 96 execution efficiency -- Python itself byte-compiles a module when | |
| 97 it is loaded, and if it can, writes out the bytecode to the | |
| 98 corresponding .pyc (or .pyo) file. | |
| 99 | |
| 100 However, if a Python installation is shared between users, it is a | |
| 101 good idea to byte-compile all modules upon installation, since | |
| 102 other users may not be able to write in the source directories, | |
| 103 and thus they won't be able to write the .pyc/.pyo file, and then | |
| 104 they would be byte-compiling every module each time it is loaded. | |
| 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 | 107 See compileall.py for a script/module that uses this module to |
| 108 byte-compile all installed files (or all files in selected | |
| 109 directories). | |
| 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: |
|
3f4c7c4eef62
Merged revisions 78971-78972 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48769
diff
changeset
|
113 timestamp = int(os.fstat(f.fileno()).st_mtime) |
|
3f4c7c4eef62
Merged revisions 78971-78972 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48769
diff
changeset
|
114 except AttributeError: |
|
3f4c7c4eef62
Merged revisions 78971-78972 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48769
diff
changeset
|
115 timestamp = int(os.stat(file).st_mtime) |
|
3f4c7c4eef62
Merged revisions 78971-78972 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48769
diff
changeset
|
116 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
|
117 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
|
118 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
|
119 optimize=optimize) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
35060
diff
changeset
|
120 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
|
121 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
|
122 if doraise: |
|
16a876746285
Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents:
24938
diff
changeset
|
123 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
|
124 else: |
|
35060
2eb569b6ce23
Patch #1171150: add a newline to py_compile error output
Georg Brandl <georg@python.org>
parents:
28098
diff
changeset
|
125 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
|
126 return |
|
23622
5edd80726d96
Replace boolean test with is None.
Raymond Hettinger <python@rcn.com>
parents:
23029
diff
changeset
|
127 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
|
128 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
|
129 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
|
130 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
|
131 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
|
132 try: |
|
73773
661fb211f220
Issue #12618: py_compile cannot create files in current directory
Meador Inge <meadori@gmail.com>
parents:
66685
diff
changeset
|
133 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
|
134 if dirname: |
|
661fb211f220
Issue #12618: py_compile cannot create files in current directory
Meador Inge <meadori@gmail.com>
parents:
66685
diff
changeset
|
135 os.makedirs(dirname) |
|
61439
fcb21d2ceb73
Create __pycache__ dir when the pyc path is explicitly given
Benjamin Peterson <benjamin@python.org>
parents:
60781
diff
changeset
|
136 except OSError as error: |
|
fcb21d2ceb73
Create __pycache__ dir when the pyc path is explicitly given
Benjamin Peterson <benjamin@python.org>
parents:
60781
diff
changeset
|
137 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
|
138 raise |
|
59695
3f4c7c4eef62
Merged revisions 78971-78972 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48769
diff
changeset
|
139 with open(cfile, 'wb') as fc: |
|
3f4c7c4eef62
Merged revisions 78971-78972 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48769
diff
changeset
|
140 fc.write(b'\0\0\0\0') |
|
3f4c7c4eef62
Merged revisions 78971-78972 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48769
diff
changeset
|
141 wr_long(fc, timestamp) |
|
3f4c7c4eef62
Merged revisions 78971-78972 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48769
diff
changeset
|
142 marshal.dump(codeobject, fc) |
|
3f4c7c4eef62
Merged revisions 78971-78972 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48769
diff
changeset
|
143 fc.flush() |
|
3f4c7c4eef62
Merged revisions 78971-78972 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48769
diff
changeset
|
144 fc.seek(0, 0) |
|
3f4c7c4eef62
Merged revisions 78971-78972 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48769
diff
changeset
|
145 fc.write(MAGIC) |
| 60648 | 146 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
|
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 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
|
149 """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
|
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 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
|
152 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
|
153 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
|
154 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
|
155 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
|
156 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
|
157 |
|
e9500709d49c
Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents:
24937
diff
changeset
|
158 """ |
|
e9500709d49c
Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents:
24937
diff
changeset
|
159 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
|
160 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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 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
|
166 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
|
167 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
|
168 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
|
169 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
|
170 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
|
171 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
|
172 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
|
173 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
|
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) |
|
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 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
|
177 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
|
178 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
|
179 compile(filename, doraise=True) |
|
60781
506faddc8a9e
Merged revisions 80274 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
60648
diff
changeset
|
180 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
|
181 # 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
|
182 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
|
183 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
|
184 return rv |
|
27507
d9d57f5f98cd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27250
diff
changeset
|
185 |
|
24938
e9500709d49c
Added a main() function and support to run this module as a script.
Fred Drake <fdrake@acm.org>
parents:
24937
diff
changeset
|
186 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
|
187 sys.exit(main()) |
