Skip to content

Commit 94f58c3

Browse files
committed
#10454: clarify the compileall docs and help messages.
1 parent a396463 commit 94f58c3

File tree

2 files changed

+85
-47
lines changed

2 files changed

+85
-47
lines changed

Doc/library/compileall.rst

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66

77

88
This module provides some utility functions to support installing Python
9-
libraries. These functions compile Python source files in a directory tree,
10-
allowing users without permission to write to the libraries to take advantage of
11-
cached byte-code files.
9+
libraries. These functions compile Python source files in a directory tree.
10+
This module can be used to create the cached byte-code files at library
11+
installation time, which makes them available for use even by users who don't
12+
have write permission to the library directories.
1213

1314

1415
Command-line use
@@ -27,32 +28,42 @@ compile Python sources.
2728

2829
.. cmdoption:: -l
2930

30-
Do not recurse.
31+
Do not recurse into subdirectories, only compile source code files directly
32+
contained in the named or implied directories.
3133

3234
.. cmdoption:: -f
3335

3436
Force rebuild even if timestamps are up-to-date.
3537

3638
.. cmdoption:: -q
3739

38-
Do not print the list of files compiled.
40+
Do not print the list of files compiled, print only error messages.
3941

4042
.. cmdoption:: -d destdir
4143

42-
Purported directory name for error messages.
44+
Directory prepended to the path to each file being compiled. This will
45+
appear in compilation time tracebacks, and is also compiled in to the
46+
byte-code file, where it will be used in tracebacks and other messages in
47+
cases where the source file does not exist at the time the byte-code file is
48+
executed.
4349

4450
.. cmdoption:: -x regex
4551

46-
Skip files with a full path that matches given regular expression.
52+
regex is used to search the full path to each file considered for
53+
compilation, and if the regex produces a match, the file is skipped.
4754

4855
.. cmdoption:: -i list
4956

50-
Expand list with its content (file and directory names).
57+
Read the file ``list`` and add each line that it contains to the list of
58+
files and directories to compile. If ``list`` is ``-``, read lines from
59+
``stdin``.
5160

5261
.. cmdoption:: -b
5362

54-
Write legacy ``.pyc`` file path names. Default is to write :pep:`3147`-style
55-
byte-compiled path names.
63+
Write the byte-code files to their legacy locations and names, which may
64+
overwrite byte-code files created by another version of Python. The default
65+
is to write files to their :pep:`3147` locations and names, which allows
66+
byte-code files from multiple versions of Python to coexist.
5667

5768
.. versionchanged:: 3.2
5869
Added the ``-i``, ``-b`` and ``-h`` options.
@@ -64,20 +75,32 @@ Public functions
6475
.. function:: compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, quiet=False, legacy=False, optimize=-1)
6576

6677
Recursively descend the directory tree named by *dir*, compiling all :file:`.py`
67-
files along the way. The *maxlevels* parameter is used to limit the depth of
68-
the recursion; it defaults to ``10``. If *ddir* is given, it is used as the
69-
base path from which the filenames used in error messages will be generated.
78+
files along the way.
79+
80+
The *maxlevels* parameter is used to limit the depth of the recursion; it
81+
defaults to ``10``.
82+
83+
If *ddir* is given, it is prepended to the path to each file being compiled
84+
for use in compilation time tracebacks, and is also compiled in to the
85+
byte-code file, where it will be used in tracebacks and other messages in
86+
cases where the source file does not exist at the time the byte-code file is
87+
executed.
88+
7089
If *force* is true, modules are re-compiled even if the timestamps are up to
7190
date.
7291

73-
If *rx* is given, it specifies a regular expression of file names to exclude
74-
from the search; that expression is searched for in the full path.
92+
If *rx* is given, its search method is called on the complete path to each
93+
file considered for compilation, and if it returns a true value, the file
94+
is skipped.
7595

76-
If *quiet* is true, nothing is printed to the standard output in normal
77-
operation.
96+
If *quiet* is true, nothing is printed to the standard output unless errors
97+
occur.
7898

79-
If *legacy* is true, old-style ``.pyc`` file path names are written,
80-
otherwise (the default), :pep:`3147`-style path names are written.
99+
If *legacy* is true, byte-code files are written to their legacy locations
100+
and names, which may overwrite byte-code files created by another version of
101+
Python. The default is to write files to their :pep:`3147` locations and
102+
names, which allows byte-code files from multiple versions of Python to
103+
coexist.
81104

82105
*optimize* specifies the optimization level for the compiler. It is passed to
83106
the built-in :func:`compile` function.
@@ -88,19 +111,26 @@ Public functions
88111

89112
.. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=False, legacy=False, optimize=-1)
90113

91-
Compile the file with path *fullname*. If *ddir* is given, it is used as the
92-
base path from which the filename used in error messages will be generated.
93-
If *force* is true, modules are re-compiled even if the timestamp is up to
94-
date.
114+
Compile the file with path *fullname*.
115+
116+
If *ddir* is given, it is prepended to the path to the file being compiled
117+
for use in compilation time tracebacks, and is also compiled in to the
118+
byte-code file, where it will be used in tracebacks and other messages in
119+
cases where the source file does not exist at the time the byte-code file is
120+
executed.
95121

96-
If *rx* is given, it specifies a regular expression which, if matched, will
97-
prevent compilation; that expression is searched for in the full path.
122+
If *ra* is given, its search method is passed the full path name to the
123+
file being compiled, and if it returns a true value, the file is not
124+
compiled and ``True`` is returned.
98125

99-
If *quiet* is true, nothing is printed to the standard output in normal
100-
operation.
126+
If *quiet* is true, nothing is printed to the standard output unless errors
127+
occur.
101128

102-
If *legacy* is true, old-style ``.pyc`` file path names are written,
103-
otherwise (the default), :pep:`3147`-style path names are written.
129+
If *legacy* is true, byte-code files are written to their legacy locations
130+
and names, which may overwrite byte-code files created by another version of
131+
Python. The default is to write files to their :pep:`3147` locations and
132+
names, which allows byte-code files from multiple versions of Python to
133+
coexist.
104134

105135
*optimize* specifies the optimization level for the compiler. It is passed to
106136
the built-in :func:`compile` function.
@@ -111,9 +141,10 @@ Public functions
111141
.. function:: compile_path(skip_curdir=True, maxlevels=0, force=False, legacy=False, optimize=-1)
112142

113143
Byte-compile all the :file:`.py` files found along ``sys.path``. If
114-
*skip_curdir* is true (the default), the current directory is not included in
115-
the search. All other parameters are passed to the :func:`compile_dir`
116-
function.
144+
*skip_curdir* is true (the default), the current directory is not included
145+
in the search. All other parameters are passed to the :func:`compile_dir`
146+
function. Note that unlike the other compile functions, ``maxlevels``
147+
defaults to ``0``.
117148

118149
.. versionchanged:: 3.2
119150
Added the *legacy* and *optimize* parameter.

Lib/compileall.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None,
2727
2828
dir: the directory to byte-compile
2929
maxlevels: maximum recursion level (default 10)
30-
ddir: if given, purported directory name (this is the
31-
directory name that will show up in error messages)
30+
ddir: the directory that will be prepended to the path to the
31+
file as it is compiled into each byte-code file.
3232
force: if True, force compilation, even if timestamps are up-to-date
3333
quiet: if True, be quiet during compilation
3434
legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
@@ -66,8 +66,8 @@ def compile_file(fullname, ddir=None, force=0, rx=None, quiet=False,
6666
legacy=False, optimize=-1):
6767
"""Byte-compile file.
6868
fullname: the file to byte-compile
69-
ddir: if given, purported directory name (this is the
70-
directory name that will show up in error messages)
69+
ddir: if given, the directory name compiled in to the
70+
byte-code file.
7171
force: if True, force compilation, even if timestamps are up-to-date
7272
quiet: if True, be quiet during compilation
7373
legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
@@ -163,25 +163,32 @@ def main():
163163

164164
parser = argparse.ArgumentParser(
165165
description='Utilities to support installing Python libraries.')
166-
parser.add_argument('-l', action='store_const', default=10, const=0,
167-
dest='maxlevels', help="don't recurse down")
166+
parser.add_argument('-l', action='store_const', const=0,
167+
default=10, dest='maxlevels',
168+
help="don't recurse into subdirectories")
168169
parser.add_argument('-f', action='store_true', dest='force',
169170
help='force rebuild even if timestamps are up to date')
170171
parser.add_argument('-q', action='store_true', dest='quiet',
171-
help='reduce output')
172+
help='output only error messages')
172173
parser.add_argument('-b', action='store_true', dest='legacy',
173-
help='produce legacy byte-compiled file paths')
174+
help='use legacy (pre-PEP3147) compiled file locations')
174175
parser.add_argument('-d', metavar='DESTDIR', dest='ddir', default=None,
175-
help=('purported directory name for error messages; '
176-
'if no directory arguments, -l sys.path '
177-
'is assumed.'))
176+
help=('directory to prepend to file paths for use in '
177+
'compile time tracebacks and in runtime '
178+
'tracebacks in cases where the source file is '
179+
'unavailable'))
178180
parser.add_argument('-x', metavar='REGEXP', dest='rx', default=None,
179-
help=('skip files matching the regular expression.\n\t'
181+
help=('skip files matching the regular expression. '
180182
'The regexp is searched for in the full path '
181-
'of the file'))
183+
'to each file considered for compilation.'))
182184
parser.add_argument('-i', metavar='FILE', dest='flist',
183-
help='expand the list with the content of FILE.')
184-
parser.add_argument('compile_dest', metavar='FILE|DIR', nargs='*')
185+
help=('add all the files and directories listed in '
186+
'FILE to the list considered for compilation. '
187+
'If "-", names are read from stdin.'))
188+
parser.add_argument('compile_dest', metavar='FILE|DIR', nargs='*',
189+
help=('zero or more file and directory names '
190+
'to compile; if no arguments given, defaults '
191+
'to the equivalent of -l sys.path'))
185192
args = parser.parse_args()
186193

187194
compile_dests = args.compile_dest

0 commit comments

Comments
 (0)