summaryrefslogtreecommitdiff
path: root/devdocs/python~3.12/library%2Fdevmode.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/python~3.12/library%2Fdevmode.html')
-rw-r--r--devdocs/python~3.12/library%2Fdevmode.html66
1 files changed, 66 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Fdevmode.html b/devdocs/python~3.12/library%2Fdevmode.html
new file mode 100644
index 00000000..1f32a201
--- /dev/null
+++ b/devdocs/python~3.12/library%2Fdevmode.html
@@ -0,0 +1,66 @@
+ <span id="devmode"></span><h1>Python Development Mode</h1> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> <p>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.</p> <p>It can be enabled using the <a class="reference internal" href="../using/cmdline#cmdoption-X"><code>-X dev</code></a> command line option or by setting the <span class="target" id="index-0"></span><a class="reference internal" href="../using/cmdline#envvar-PYTHONDEVMODE"><code>PYTHONDEVMODE</code></a> environment variable to <code>1</code>.</p> <p>See also <a class="reference internal" href="../using/configure#debug-build"><span class="std std-ref">Python debug build</span></a>.</p> <section id="effects-of-the-python-development-mode"> <h2>Effects of the Python Development Mode</h2> <p>Enabling the Python Development Mode is similar to the following command, but with additional effects described below:</p> <pre data-language="python">PYTHONMALLOC=debug PYTHONASYNCIODEBUG=1 python -W default -X faulthandler
+</pre> <p>Effects of the Python Development Mode:</p> <ul> <li>
+<p>Add <code>default</code> <a class="reference internal" href="warnings#describing-warning-filters"><span class="std std-ref">warning filter</span></a>. The following warnings are shown:</p> <ul class="simple"> <li><a class="reference internal" href="exceptions#DeprecationWarning" title="DeprecationWarning"><code>DeprecationWarning</code></a></li> <li><a class="reference internal" href="exceptions#ImportWarning" title="ImportWarning"><code>ImportWarning</code></a></li> <li><a class="reference internal" href="exceptions#PendingDeprecationWarning" title="PendingDeprecationWarning"><code>PendingDeprecationWarning</code></a></li> <li><a class="reference internal" href="exceptions#ResourceWarning" title="ResourceWarning"><code>ResourceWarning</code></a></li> </ul> <p>Normally, the above warnings are filtered by the default <a class="reference internal" href="warnings#describing-warning-filters"><span class="std std-ref">warning filters</span></a>.</p> <p>It behaves as if the <a class="reference internal" href="../using/cmdline#cmdoption-W"><code>-W default</code></a> command line option is used.</p> <p>Use the <a class="reference internal" href="../using/cmdline#cmdoption-W"><code>-W error</code></a> command line option or set the <span class="target" id="index-1"></span><a class="reference internal" href="../using/cmdline#envvar-PYTHONWARNINGS"><code>PYTHONWARNINGS</code></a> environment variable to <code>error</code> to treat warnings as errors.</p> </li> <li>
+<p>Install debug hooks on memory allocators to check for:</p> <ul class="simple"> <li>Buffer underflow</li> <li>Buffer overflow</li> <li>Memory allocator API violation</li> <li>Unsafe usage of the GIL</li> </ul> <p>See the <a class="reference internal" href="../c-api/memory#c.PyMem_SetupDebugHooks" title="PyMem_SetupDebugHooks"><code>PyMem_SetupDebugHooks()</code></a> C function.</p> <p>It behaves as if the <span class="target" id="index-2"></span><a class="reference internal" href="../using/cmdline#envvar-PYTHONMALLOC"><code>PYTHONMALLOC</code></a> environment variable is set to <code>debug</code>.</p> <p>To enable the Python Development Mode without installing debug hooks on memory allocators, set the <span class="target" id="index-3"></span><a class="reference internal" href="../using/cmdline#envvar-PYTHONMALLOC"><code>PYTHONMALLOC</code></a> environment variable to <code>default</code>.</p> </li> <li>
+<p>Call <a class="reference internal" href="faulthandler#faulthandler.enable" title="faulthandler.enable"><code>faulthandler.enable()</code></a> at Python startup to install handlers for the <a class="reference internal" href="signal#signal.SIGSEGV" title="signal.SIGSEGV"><code>SIGSEGV</code></a>, <a class="reference internal" href="signal#signal.SIGFPE" title="signal.SIGFPE"><code>SIGFPE</code></a>, <a class="reference internal" href="signal#signal.SIGABRT" title="signal.SIGABRT"><code>SIGABRT</code></a>, <a class="reference internal" href="signal#signal.SIGBUS" title="signal.SIGBUS"><code>SIGBUS</code></a> and <a class="reference internal" href="signal#signal.SIGILL" title="signal.SIGILL"><code>SIGILL</code></a> signals to dump the Python traceback on a crash.</p> <p>It behaves as if the <a class="reference internal" href="../using/cmdline#cmdoption-X"><code>-X faulthandler</code></a> command line option is used or if the <span class="target" id="index-4"></span><a class="reference internal" href="../using/cmdline#envvar-PYTHONFAULTHANDLER"><code>PYTHONFAULTHANDLER</code></a> environment variable is set to <code>1</code>.</p> </li> <li>
+<p>Enable <a class="reference internal" href="asyncio-dev#asyncio-debug-mode"><span class="std std-ref">asyncio debug mode</span></a>. For example, <a class="reference internal" href="asyncio#module-asyncio" title="asyncio: Asynchronous I/O."><code>asyncio</code></a> checks for coroutines that were not awaited and logs them.</p> <p>It behaves as if the <span class="target" id="index-5"></span><a class="reference internal" href="../using/cmdline#envvar-PYTHONASYNCIODEBUG"><code>PYTHONASYNCIODEBUG</code></a> environment variable is set to <code>1</code>.</p> </li> <li>
+<p>Check the <em>encoding</em> and <em>errors</em> arguments for string encoding and decoding operations. Examples: <a class="reference internal" href="functions#open" title="open"><code>open()</code></a>, <a class="reference internal" href="stdtypes#str.encode" title="str.encode"><code>str.encode()</code></a> and <a class="reference internal" href="stdtypes#bytes.decode" title="bytes.decode"><code>bytes.decode()</code></a>.</p> <p>By default, for best performance, the <em>errors</em> argument is only checked at the first encoding/decoding error and the <em>encoding</em> argument is sometimes ignored for empty strings.</p> </li> <li>The <a class="reference internal" href="io#io.IOBase" title="io.IOBase"><code>io.IOBase</code></a> destructor logs <code>close()</code> exceptions.</li> <li>Set the <a class="reference internal" href="sys#sys.flags.dev_mode" title="sys.flags.dev_mode"><code>dev_mode</code></a> attribute of <a class="reference internal" href="sys#sys.flags" title="sys.flags"><code>sys.flags</code></a> to <code>True</code>.</li> </ul> <p>The Python Development Mode does not enable the <a class="reference internal" href="tracemalloc#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code>tracemalloc</code></a> module by default, because the overhead cost (to performance and memory) would be too large. Enabling the <a class="reference internal" href="tracemalloc#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code>tracemalloc</code></a> module provides additional information on the origin of some errors. For example, <a class="reference internal" href="exceptions#ResourceWarning" title="ResourceWarning"><code>ResourceWarning</code></a> logs the traceback where the resource was allocated, and a buffer overflow error logs the traceback where the memory block was allocated.</p> <p>The Python Development Mode does not prevent the <a class="reference internal" href="../using/cmdline#cmdoption-O"><code>-O</code></a> command line option from removing <a class="reference internal" href="../reference/simple_stmts#assert"><code>assert</code></a> statements nor from setting <a class="reference internal" href="constants#debug__" title="__debug__"><code>__debug__</code></a> to <code>False</code>.</p> <p>The Python Development Mode can only be enabled at the Python startup. Its value can be read from <a class="reference internal" href="sys#sys.flags" title="sys.flags"><code>sys.flags.dev_mode</code></a>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>The <a class="reference internal" href="io#io.IOBase" title="io.IOBase"><code>io.IOBase</code></a> destructor now logs <code>close()</code> exceptions.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.9: </span>The <em>encoding</em> and <em>errors</em> arguments are now checked for string encoding and decoding operations.</p> </div> </section> <section id="resourcewarning-example"> <h2>ResourceWarning Example</h2> <p>Example of a script counting the number of lines of the text file specified in the command line:</p> <pre data-language="python">import sys
+
+def main():
+ fp = open(sys.argv[1])
+ nlines = len(fp.readlines())
+ print(nlines)
+ # The file is closed implicitly
+
+if __name__ == "__main__":
+ main()
+</pre> <p>The script does not close the file explicitly. By default, Python does not emit any warning. Example using README.txt, which has 269 lines:</p> <pre data-language="shell">$ python script.py README.txt
+269
+</pre> <p>Enabling the Python Development Mode displays a <a class="reference internal" href="exceptions#ResourceWarning" title="ResourceWarning"><code>ResourceWarning</code></a> warning:</p> <pre data-language="shell">$ python -X dev script.py README.txt
+269
+script.py:10: ResourceWarning: unclosed file &lt;_io.TextIOWrapper name='README.rst' mode='r' encoding='UTF-8'&gt;
+ main()
+ResourceWarning: Enable tracemalloc to get the object allocation traceback
+</pre> <p>In addition, enabling <a class="reference internal" href="tracemalloc#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code>tracemalloc</code></a> shows the line where the file was opened:</p> <pre data-language="shell">$ python -X dev -X tracemalloc=5 script.py README.rst
+269
+script.py:10: ResourceWarning: unclosed file &lt;_io.TextIOWrapper name='README.rst' mode='r' encoding='UTF-8'&gt;
+ main()
+Object allocated at (most recent call last):
+ File "script.py", lineno 10
+ main()
+ File "script.py", lineno 4
+ fp = open(sys.argv[1])
+</pre> <p>The fix is to close explicitly the file. Example using a context manager:</p> <pre data-language="python">def main():
+ # Close the file explicitly when exiting the with block
+ with open(sys.argv[1]) as fp:
+ nlines = len(fp.readlines())
+ print(nlines)
+</pre> <p>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.</p> </section> <section id="bad-file-descriptor-error-example"> <h2>Bad file descriptor error example</h2> <p>Script displaying the first line of itself:</p> <pre data-language="python">import os
+
+def main():
+ fp = open(__file__)
+ firstline = fp.readline()
+ print(firstline.rstrip())
+ os.close(fp.fileno())
+ # The file is closed implicitly
+
+main()
+</pre> <p>By default, Python does not emit any warning:</p> <pre data-language="shell">$ python script.py
+import os
+</pre> <p>The Python Development Mode shows a <a class="reference internal" href="exceptions#ResourceWarning" title="ResourceWarning"><code>ResourceWarning</code></a> and logs a “Bad file descriptor” error when finalizing the file object:</p> <pre data-language="shell">$ python -X dev script.py
+import os
+script.py:10: ResourceWarning: unclosed file &lt;_io.TextIOWrapper name='script.py' mode='r' encoding='UTF-8'&gt;
+ main()
+ResourceWarning: Enable tracemalloc to get the object allocation traceback
+Exception ignored in: &lt;_io.TextIOWrapper name='script.py' mode='r' encoding='UTF-8'&gt;
+Traceback (most recent call last):
+ File "script.py", line 10, in &lt;module&gt;
+ main()
+OSError: [Errno 9] Bad file descriptor
+</pre> <p><code>os.close(fp.fileno())</code> closes the file descriptor. When the file object finalizer tries to close the file descriptor again, it fails with the <code>Bad
+file descriptor</code> error. A file descriptor must be closed only once. In the worst case scenario, closing it twice can lead to a crash (see <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&amp;bpo=18748">bpo-18748</a> for an example).</p> <p>The fix is to remove the <code>os.close(fp.fileno())</code> line, or open the file with <code>closefd=False</code>.</p> </section> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; 2001&ndash;2023 Python Software Foundation<br>Licensed under the PSF License.<br>
+ <a href="https://docs.python.org/3.12/library/devmode.html" class="_attribution-link">https://docs.python.org/3.12/library/devmode.html</a>
+ </p>
+</div>