"py_compile" --- Compile Python source files
********************************************

**ソースコード:** Lib/py_compile.py

======================================================================

"py_compile" モジュールには、ソースファイルからバイトコードファイルを
作る関数と、モジュールのソースファイルがスクリプトとして呼び出される時
に使用される関数が定義されています。

頻繁に必要となるわけではありませんが、共有ライブラリとしてモジュールを
インストールする場合や、特にソースコードのあるディレクトリにバイトコー
ドのキャッシュファイルを書き込む権限がないユーザがいるときには、この関
数は役に立ちます。

exception py_compile.PyCompileError

   ファイルをコンパイル中にエラーが発生すると送出される例外。

py_compile.compile(file, cfile=None, dfile=None, doraise=False, optimize=-1, invalidation_mode=PycInvalidationMode.TIMESTAMP, quiet=0)

   Compile a source file to byte-code and write out the byte-code
   cache file. The source code is loaded from the file named *file*.
   The byte-code is written to *cfile*, which defaults to the **PEP
   3147**/**PEP 488** path, ending in ".pyc". For example, if *file*
   is "/foo/bar/baz.py" *cfile* will default to
   "/foo/bar/__pycache__/baz.cpython-32.pyc" for Python 3.2.  If
   *dfile* is specified, it is used instead of *file* as the name of
   the source file from which source lines are obtained for display in
   exception tracebacks. If *doraise* is true, a "PyCompileError" is
   raised when an error is encountered while compiling *file*. If
   *doraise* is false (the default), an error string is written to
   "sys.stderr", but no exception is raised.  This function returns
   the path to byte-compiled file, i.e. whatever *cfile* value was
   used.

   The *doraise* and *quiet* arguments determine how errors are
   handled while compiling file. If *quiet* is 0 or 1, and *doraise*
   is false, the default behaviour is enabled: an error string is
   written to "sys.stderr", and the function returns "None" instead of
   a path. If *doraise* is true, a "PyCompileError" is raised instead.
   However if *quiet* is 2, no message is written, and *doraise* has
   no effect.

   (明示的に指定されたか計算された結果の) *cfile* のパスがシンボリック
   リンクだったり通常のファイルでなかったりした場合は、
   "FileExistsError" が送出されます。この動作は、それらのパスにバイト
   コンパイルされたファイルを書き込む権限がある場合、インポートのとき
   にそのパスを通常のファイルに変えてしまうことを警告するためのもので
   す。これはリネームを使ってバイトコンパイルされたファイルを配置する
   インポートで、同時にファイル書き込みをしてしまう問題を避けるための
   副作用です。

   *optimize* は最適化レベルを制御するもので、組み込みの "compile()"
   関数に渡されます。デフォルトは現在のインタープリタの最適化レベルを
   選ぶ "-1" です。

   *invalidation_mode* should be a member of the "PycInvalidationMode"
   enum and controls how the generated bytecode cache is invalidated
   at runtime.  The default is "PycInvalidationMode.CHECKED_HASH" if
   the "SOURCE_DATE_EPOCH" environment variable is set, otherwise the
   default is "PycInvalidationMode.TIMESTAMP".

   バージョン 3.2 で変更: **PEP 3147** に準拠し *cfile* のデフォルト値
   を変更しました。以前のデフォルトは *file* + "'c'" (最適化が有効な場
   合は "'o'") でした。同時に *optimize* パラメータも追加されました。

   バージョン 3.4 で変更: バイトコードをキャッシュするファイルへの書き
   込みに "importlib" を使うようにコードを変更しました。これにより、例
   えば権限や write-and-move セマンティクスなどの、ファイルの作成や書
   き込みの動作が "importlib" と一致するようになりました。 *cfile* が
   シンボリックリンクであるか通常のファイルでない場合、
   "FileExistsError" を送出するという注意書きも追加されました。

   バージョン 3.7 で変更: The *invalidation_mode* parameter was added
   as specified in **PEP 552**. If the "SOURCE_DATE_EPOCH" environment
   variable is set, *invalidation_mode* will be forced to
   "PycInvalidationMode.CHECKED_HASH".

   バージョン 3.7.2 で変更: The "SOURCE_DATE_EPOCH" environment
   variable no longer overrides the value of the *invalidation_mode*
   argument, and determines its default value instead.

   バージョン 3.8 で変更: *quiet* パラメータが追加されました。

class py_compile.PycInvalidationMode

   A enumeration of possible methods the interpreter can use to
   determine whether a bytecode file is up to date with a source file.
   The ".pyc" file indicates the desired invalidation mode in its
   header. See キャッシュされたバイトコードの無効化 for more
   information on how Python invalidates ".pyc" files at runtime.

   バージョン 3.7 で追加.

   TIMESTAMP

      The ".pyc" file includes the timestamp and size of the source
      file, which Python will compare against the metadata of the
      source file at runtime to determine if the ".pyc" file needs to
      be regenerated.

   CHECKED_HASH

      The ".pyc" file includes a hash of the source file content,
      which Python will compare against the source at runtime to
      determine if the ".pyc" file needs to be regenerated.

   UNCHECKED_HASH

      Like "CHECKED_HASH", the ".pyc" file includes a hash of the
      source file content. However, Python will at runtime assume the
      ".pyc" file is up to date and not validate the ".pyc" against
      the source file at all.

      This option is useful when the ".pycs" are kept up to date by
      some system external to Python like a build system.


コマンドラインインターフェイス
==============================

This module can be invoked as a script to compile several source
files.  The files named in *filenames* are compiled and the resulting
bytecode is cached in the normal manner.  This program does not search
a directory structure to locate source files; it only compiles files
named explicitly. The exit status is nonzero if one of the files could
not be compiled.

<file> ... <fileN>
-

   Positional arguments are files to compile.  If "-" is the only
   parameter, the list of files is taken from standard input.

-q, --quiet

   Suppress errors output.

バージョン 3.2 で変更: "-" のサポートが追加されました。

バージョン 3.10 で変更: "-q" のサポートが追加されました。

参考:

  "compileall" モジュール
     ディレクトリツリー内の Python ソースファイルを全てコンパイルする
     ライブラリ。
