# SOME DESCRIPTIVE TITLE. # Copyright (C) 2001-2025, Python Software Foundation # This file is distributed under the same license as the Python package. # FIRST AUTHOR , YEAR. # # Translators: # Rafael Fontenelle , 2025 # #, fuzzy msgid "" msgstr "" "Project-Id-Version: Python 3.12\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2025-07-25 16:03+0000\n" "PO-Revision-Date: 2025-07-18 19:58+0000\n" "Last-Translator: Rafael Fontenelle , 2025\n" "Language-Team: Chinese (China) (https://app.transifex.com/python-doc/teams/5390/zh_CN/)\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Language: zh_CN\n" "Plural-Forms: nplurals=1; plural=0;\n" #: ../../library/devmode.rst:4 msgid "Python Development Mode" msgstr "Python 开发模式" #: ../../library/devmode.rst:8 msgid "" "The Python Development Mode introduces additional runtime checks that are " "too expensive to be enabled by default. It should not be more verbose than " "the default if the code is correct; new warnings are only emitted when an " "issue is detected." msgstr "" "开发模式下的 Python " "加入了额外的运行时检查,由于开销太大,并非默认启用的。如果代码能够正确执行,默认的调试级别足矣,不应再提高了;仅当觉察到问题时再提升警告触发的级别。" #: ../../library/devmode.rst:13 msgid "" "It can be enabled using the :option:`-X dev <-X>` command line option or by " "setting the :envvar:`PYTHONDEVMODE` environment variable to ``1``." msgstr "" "使用 :option:`-X dev <-X>` 命令行参数或将环境变量 :envvar:`PYTHONDEVMODE` 置为 ``1`` " ",可以启用开发模式。" #: ../../library/devmode.rst:16 msgid "See also :ref:`Python debug build `." msgstr "另请参考 :ref:`Python debug build ` 。" #: ../../library/devmode.rst:19 msgid "Effects of the Python Development Mode" msgstr "Python 开发模式的效果" #: ../../library/devmode.rst:21 msgid "" "Enabling the Python Development Mode is similar to the following command, " "but with additional effects described below::" msgstr "启用 Python 开发模式后的效果,与以下命令类似,不过还有下面的额外效果:" #: ../../library/devmode.rst:24 msgid "" "PYTHONMALLOC=debug PYTHONASYNCIODEBUG=1 python -W default -X faulthandler" msgstr "" "PYTHONMALLOC=debug PYTHONASYNCIODEBUG=1 python -W default -X faulthandler" #: ../../library/devmode.rst:26 msgid "Effects of the Python Development Mode:" msgstr "Python 开发模式的效果:" #: ../../library/devmode.rst:28 msgid "" "Add ``default`` :ref:`warning filter `. The " "following warnings are shown:" msgstr "" "加入 ``default`` :ref:`warning filter ` " "。下述警告信息将会显示出来:" #: ../../library/devmode.rst:31 msgid ":exc:`DeprecationWarning`" msgstr ":exc:`DeprecationWarning`" #: ../../library/devmode.rst:32 msgid ":exc:`ImportWarning`" msgstr ":exc:`ImportWarning`" #: ../../library/devmode.rst:33 msgid ":exc:`PendingDeprecationWarning`" msgstr ":exc:`PendingDeprecationWarning`" #: ../../library/devmode.rst:34 msgid ":exc:`ResourceWarning`" msgstr ":exc:`ResourceWarning`" #: ../../library/devmode.rst:36 msgid "" "Normally, the above warnings are filtered by the default :ref:`warning " "filters `." msgstr "" "通常上述警告是由默认的 :ref:`warning filters ` 负责处理的。" #: ../../library/devmode.rst:39 msgid "" "It behaves as if the :option:`-W default <-W>` command line option is used." msgstr "效果类似于采用了 :option:`-W default <-W>` 命令行参数。" #: ../../library/devmode.rst:41 msgid "" "Use the :option:`-W error <-W>` command line option or set the " ":envvar:`PYTHONWARNINGS` environment variable to ``error`` to treat warnings" " as errors." msgstr "" "使用命令行参数 :option:`-W error <-W>` 或将环境变量 :envvar:`PYTHONWARNINGS` 设为 " "``error``,可将警告视为错误。" #: ../../library/devmode.rst:45 msgid "Install debug hooks on memory allocators to check for:" msgstr "在内存分配程序中安装调试钩子,用以查看:" #: ../../library/devmode.rst:47 msgid "Buffer underflow" msgstr "缓冲区下溢" #: ../../library/devmode.rst:48 msgid "Buffer overflow" msgstr "缓冲区上溢" #: ../../library/devmode.rst:49 msgid "Memory allocator API violation" msgstr "内存分配 API 冲突" #: ../../library/devmode.rst:50 msgid "Unsafe usage of the GIL" msgstr "不安全的 GIL 调用" #: ../../library/devmode.rst:52 msgid "See the :c:func:`PyMem_SetupDebugHooks` C function." msgstr "参见 C 函数 :c:func:`PyMem_SetupDebugHooks` 。" #: ../../library/devmode.rst:54 msgid "" "It behaves as if the :envvar:`PYTHONMALLOC` environment variable is set to " "``debug``." msgstr "效果如同将环境变量 :envvar:`PYTHONMALLOC` 设为 ``debug``。" #: ../../library/devmode.rst:57 msgid "" "To enable the Python Development Mode without installing debug hooks on " "memory allocators, set the :envvar:`PYTHONMALLOC` environment variable to " "``default``." msgstr "" "若要启用 Python 开发模式,却又不要在内存分配程序中安装调试钩子,请将 环境变量 :envvar:`PYTHONMALLOC` 设为 " "``default``。" #: ../../library/devmode.rst:61 msgid "" "Call :func:`faulthandler.enable` at Python startup to install handlers for " "the :const:`~signal.SIGSEGV`, :const:`~signal.SIGFPE`, " ":const:`~signal.SIGABRT`, :const:`~signal.SIGBUS` and " ":const:`~signal.SIGILL` signals to dump the Python traceback on a crash." msgstr "" "在 Python 启动时调用 :func:`faulthandler.enable` 来为 :const:`~signal.SIGSEGV`, " ":const:`~signal.SIGFPE`, :const:`~signal.SIGABRT`, :const:`~signal.SIGBUS` 和" " :const:`~signal.SIGILL` 信号安装处理器以便在程序崩溃时转储 Python 回溯信息。" #: ../../library/devmode.rst:66 msgid "" "It behaves as if the :option:`-X faulthandler <-X>` command line option is " "used or if the :envvar:`PYTHONFAULTHANDLER` environment variable is set to " "``1``." msgstr "" "其行为如同使用了 :option:`-X faulthandler <-X>` 命令行选项或将 :envvar:`PYTHONFAULTHANDLER`" " 环境变量设为 ``1``。" #: ../../library/devmode.rst:70 msgid "" "Enable :ref:`asyncio debug mode `. For example, " ":mod:`asyncio` checks for coroutines that were not awaited and logs them." msgstr "" "启用 :ref:`asyncio debug mode `。比如 :mod:`asyncio` " "会检查没有等待的协程并记录下来。" #: ../../library/devmode.rst:73 msgid "" "It behaves as if the :envvar:`PYTHONASYNCIODEBUG` environment variable is " "set to ``1``." msgstr "效果如同将环境变量 :envvar:`PYTHONASYNCIODEBUG` 设为 ``1``。" #: ../../library/devmode.rst:76 msgid "" "Check the *encoding* and *errors* arguments for string encoding and decoding" " operations. Examples: :func:`open`, :meth:`str.encode` and " ":meth:`bytes.decode`." msgstr "" "检查字符串编码和解码函数的 *encoding* 和 *errors* 参数。例如: :func:`open` 、 :meth:`str.encode`" " 和 :meth:`bytes.decode`。" #: ../../library/devmode.rst:80 msgid "" "By default, for best performance, the *errors* argument is only checked at " "the first encoding/decoding error and the *encoding* argument is sometimes " "ignored for empty strings." msgstr "" "为了获得最佳性能,默认只会在第一次编码/解码错误时才会检查 *errors* 参数,有时 *encoding* 参数为空字符串时还会被忽略。" #: ../../library/devmode.rst:84 msgid "The :class:`io.IOBase` destructor logs ``close()`` exceptions." msgstr ":class:`io.IOBase` 的析构函数会记录 ``close()`` 触发的异常。" #: ../../library/devmode.rst:85 msgid "" "Set the :attr:`~sys.flags.dev_mode` attribute of :data:`sys.flags` to " "``True``." msgstr "将 :data:`sys.flags` 的 :attr:`~sys.flags.dev_mode` 属性设为 ``True``。" #: ../../library/devmode.rst:88 msgid "" "The Python Development Mode does not enable the :mod:`tracemalloc` module by" " default, because the overhead cost (to performance and memory) would be too" " large. Enabling the :mod:`tracemalloc` module provides additional " "information on the origin of some errors. For example, " ":exc:`ResourceWarning` logs the traceback where the resource was allocated, " "and a buffer overflow error logs the traceback where the memory block was " "allocated." msgstr "" "Python 开发模式下,默认不会启用 :mod:`tracemalloc` 模块,因为其性能和内存开销太大。启用 :mod:`tracemalloc`" " 模块后,能够提供有关错误来源的一些额外信息。例如,:exc:`ResourceWarning` " "记录了资源分配的跟踪信息,而缓冲区溢出错误记录了内存块分配的跟踪信息。" #: ../../library/devmode.rst:95 msgid "" "The Python Development Mode does not prevent the :option:`-O` command line " "option from removing :keyword:`assert` statements nor from setting " ":const:`__debug__` to ``False``." msgstr "" "Python 开发模式不会阻止命令行参数 :option:`-O` 删除 :keyword:`assert` 语句,也不会阻止将 " ":const:`__debug__` 设为 ``False``。" #: ../../library/devmode.rst:99 msgid "" "The Python Development Mode can only be enabled at the Python startup. Its " "value can be read from :data:`sys.flags.dev_mode `." msgstr "" "Python 开发模式只能在 Python 启动时启用。其参数值可从 :data:`sys.flags.dev_mode ` " "读取。" #: ../../library/devmode.rst:102 msgid "The :class:`io.IOBase` destructor now logs ``close()`` exceptions." msgstr "现在, :class:`io.IOBase` 的析构函数会记录 ``close()`` 触发的异常。" #: ../../library/devmode.rst:105 msgid "" "The *encoding* and *errors* arguments are now checked for string encoding " "and decoding operations." msgstr "现在,字符串编码和解码操作时会检查 *encoding* 和 *errors* 参数。" #: ../../library/devmode.rst:111 msgid "ResourceWarning Example" msgstr "ResourceWarning 示例" #: ../../library/devmode.rst:113 msgid "" "Example of a script counting the number of lines of the text file specified " "in the command line::" msgstr "以下示例将统计由命令行指定的文本文件的行数:" #: ../../library/devmode.rst:116 msgid "" "import sys\n" "\n" "def main():\n" " fp = open(sys.argv[1])\n" " nlines = len(fp.readlines())\n" " print(nlines)\n" " # The file is closed implicitly\n" "\n" "if __name__ == \"__main__\":\n" " main()" msgstr "" "import sys\n" "\n" "def main():\n" " fp = open(sys.argv[1])\n" " nlines = len(fp.readlines())\n" " print(nlines)\n" " # 文件将隐式地关闭\n" "\n" "if __name__ == \"__main__\":\n" " main()" #: ../../library/devmode.rst:127 msgid "" "The script does not close the file explicitly. By default, Python does not " "emit any warning. Example using README.txt, which has 269 lines:" msgstr "上述代码没有显式关闭文件。默认情况下,Python 不会触发任何警告。下面用 README.txt 文件测试下,有 269 行:" #: ../../library/devmode.rst:130 msgid "" "$ python script.py README.txt\n" "269" msgstr "" "$ python script.py README.txt\n" "269" #: ../../library/devmode.rst:135 msgid "" "Enabling the Python Development Mode displays a :exc:`ResourceWarning` " "warning:" msgstr "启用 Python 开发模式后,则会显示一条 :exc:`ResourceWarning` 警告:" #: ../../library/devmode.rst:137 msgid "" "$ python -X dev script.py README.txt\n" "269\n" "script.py:10: ResourceWarning: unclosed file <_io.TextIOWrapper name='README.rst' mode='r' encoding='UTF-8'>\n" " main()\n" "ResourceWarning: Enable tracemalloc to get the object allocation traceback" msgstr "" "$ python -X dev script.py README.txt\n" "269\n" "script.py:10: ResourceWarning: unclosed file <_io.TextIOWrapper name='README.rst' mode='r' encoding='UTF-8'>\n" " main()\n" "ResourceWarning: Enable tracemalloc to get the object allocation traceback" #: ../../library/devmode.rst:145 msgid "" "In addition, enabling :mod:`tracemalloc` shows the line where the file was " "opened:" msgstr "启用 :mod:`tracemalloc` 后,则还会显示打开文件的那行代码:" #: ../../library/devmode.rst:148 msgid "" "$ python -X dev -X tracemalloc=5 script.py README.rst\n" "269\n" "script.py:10: ResourceWarning: unclosed file <_io.TextIOWrapper name='README.rst' mode='r' encoding='UTF-8'>\n" " main()\n" "Object allocated at (most recent call last):\n" " File \"script.py\", lineno 10\n" " main()\n" " File \"script.py\", lineno 4\n" " fp = open(sys.argv[1])" msgstr "" "$ python -X dev -X tracemalloc=5 script.py README.rst\n" "269\n" "script.py:10: ResourceWarning: unclosed file <_io.TextIOWrapper name='README.rst' mode='r' encoding='UTF-8'>\n" " main()\n" "Object allocated at (most recent call last):\n" " File \"script.py\", lineno 10\n" " main()\n" " File \"script.py\", lineno 4\n" " fp = open(sys.argv[1])" #: ../../library/devmode.rst:160 msgid "" "The fix is to close explicitly the file. Example using a context manager::" msgstr "修正方案就是显式关闭文件。下面用上下文管理器作为示例:" #: ../../library/devmode.rst:162 msgid "" "def main():\n" " # Close the file explicitly when exiting the with block\n" " with open(sys.argv[1]) as fp:\n" " nlines = len(fp.readlines())\n" " print(nlines)" msgstr "" "def main():\n" " # 在退出 with 语句块时显式地关闭文件\n" " with open(sys.argv[1]) as fp:\n" " nlines = len(fp.readlines())\n" " print(nlines)" #: ../../library/devmode.rst:168 msgid "" "Not closing a resource explicitly can leave a resource open for way longer " "than expected; it can cause severe issues upon exiting Python. It is bad in " "CPython, but it is even worse in PyPy. Closing resources explicitly makes an" " application more deterministic and more reliable." msgstr "" "未能显式关闭资源,会让资源打开时长远超预期;在退出 Python 时可能会导致严重问题。这在 CPython 中比较糟糕,但在 PyPy " "中会更糟。显式关闭资源能让应用程序更加稳定可靠。" #: ../../library/devmode.rst:175 msgid "Bad file descriptor error example" msgstr "文件描述符错误示例" #: ../../library/devmode.rst:177 msgid "Script displaying the first line of itself::" msgstr "显示自身的第一行代码:" #: ../../library/devmode.rst:179 msgid "" "import os\n" "\n" "def main():\n" " fp = open(__file__)\n" " firstline = fp.readline()\n" " print(firstline.rstrip())\n" " os.close(fp.fileno())\n" " # The file is closed implicitly\n" "\n" "main()" msgstr "" "import os\n" "\n" "def main():\n" " fp = open(__file__)\n" " firstline = fp.readline()\n" " print(firstline.rstrip())\n" " os.close(fp.fileno())\n" " # 文件被隐式地关闭\n" "\n" "main()" #: ../../library/devmode.rst:190 msgid "By default, Python does not emit any warning:" msgstr "默认情况下,Python 不会触发任何警告:" #: ../../library/devmode.rst:192 msgid "" "$ python script.py\n" "import os" msgstr "" "$ python script.py\n" "import os" #: ../../library/devmode.rst:197 msgid "" "The Python Development Mode shows a :exc:`ResourceWarning` and logs a \"Bad " "file descriptor\" error when finalizing the file object:" msgstr "" "在 Python 开发模式下,会在析构文件对象时显示 :exc:`ResourceWarning` 并记录 “Bad file descriptor” " "错误。" #: ../../library/devmode.rst:200 msgid "" "$ python -X dev script.py\n" "import os\n" "script.py:10: ResourceWarning: unclosed file <_io.TextIOWrapper name='script.py' mode='r' encoding='UTF-8'>\n" " main()\n" "ResourceWarning: Enable tracemalloc to get the object allocation traceback\n" "Exception ignored in: <_io.TextIOWrapper name='script.py' mode='r' encoding='UTF-8'>\n" "Traceback (most recent call last):\n" " File \"script.py\", line 10, in \n" " main()\n" "OSError: [Errno 9] Bad file descriptor" msgstr "" "$ python -X dev script.py\n" "import os\n" "script.py:10: ResourceWarning: unclosed file <_io.TextIOWrapper name='script.py' mode='r' encoding='UTF-8'>\n" " main()\n" "ResourceWarning: Enable tracemalloc to get the object allocation traceback\n" "Exception ignored in: <_io.TextIOWrapper name='script.py' mode='r' encoding='UTF-8'>\n" "Traceback (most recent call last):\n" " File \"script.py\", line 10, in \n" " main()\n" "OSError: [Errno 9] Bad file descriptor" #: ../../library/devmode.rst:213 msgid "" "``os.close(fp.fileno())`` closes the file descriptor. When the file object " "finalizer tries to close the file descriptor again, it fails with the ``Bad " "file descriptor`` error. A file descriptor must be closed only once. In the " "worst case scenario, closing it twice can lead to a crash (see " ":issue:`18748` for an example)." msgstr "" "``os.close(fp.fileno())`` 会关闭文件描述符。当文件对象析构函数试图再次关闭文件描述符时会失败,并触发 ``Bad file " "descriptor`` 错误。每个文件描述符只允许关闭一次。在最坏的情况下,关闭两次会导致程序崩溃(示例可参见 :issue:`18748` )。" #: ../../library/devmode.rst:219 msgid "" "The fix is to remove the ``os.close(fp.fileno())`` line, or open the file " "with ``closefd=False``." msgstr "修正方案是删除 ``os.close(fp.fileno())`` 这一行,或者打开文件时带上 ``closefd=False`` 参数。"