py

您所在的位置:网站首页 mdb文件生成脚本 py

py

2023-06-09 11:42| 来源: 网络整理| 查看: 265

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.

doraise 和 quiet 参数确定在编译文件时如何处理错误。 如果 quiet 为 0 或 1,并且 doraise 为假值,则会启用默认行为:写入错误信息到 sys.stderr,并且函数将返回 None 而非一个路径。 如果 doraise 为真值,则将改为引发 PyCompileError。 但是如果 quiet 为 2,则不会写入消息,并且 doraise 也不会有效果。

如果 cfile 所表示(显式指定或计算得出)的路径为符号链接或非常规文件,则将引发 FileExistsError。 此行为是用来警告如果允许写入编译后字节码文件到这些路径则导入操作将会把它们转为常规文件。 这是使用文件重命名来将最终编译后字节码文件放置到位以防止并发文件写入问题的导入操作的附带效果。

optimize 控制优化级别并会被传给内置的 compile() 函数。 默认值 -1 表示选择当前解释器的优化级别。

invalidation_mode 应当是 PycInvalidationMode 枚举的成员,它控制在运行时如何让已生成的字节码缓存失效。 如果设置了 SOURCE_DATE_EPOCH 环境变量则默认值为 PycInvalidationMode.CHECKED_HASH,否则默认值为 PycInvalidationMode.TIMESTAMP。

在 3.2 版本发生变更: 将 cfile 的默认值改成与 PEP 3147 兼容。 之前的默认值是 file + 'c' (如果启用优化则为 'o')。 同时也添加了 optimize 形参。

在 3.4 版本发生变更: 将代码更改为使用 importlib 执行字节码缓存文件写入。 这意味着文件创建/写入的语义现在与 importlib 所做的相匹配,例如权限、写入和移动语义等等。 同时也添加了当 cfile 为符号链接或非常规文件时引发 FileExistsError 的预警设置。

在 3.7 版本发生变更: invalidation_mode 形参是根据 PEP 552 的描述添加的。 如果设置了 SOURCE_DATE_EPOCH 环境变量,invalidation_mode 将被强制设为 PycInvalidationMode.CHECKED_HASH。

在 3.7.2 版本发生变更: SOURCE_DATE_EPOCH 环境变量不会再覆盖 invalidation_mode 参数的值,而改为确定其默认值。

在 3.8 版本发生变更: 增加了 quiet 形参。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3