blob: e6ed59496c242e3a6317040fb8138feadd6cff0f [file] [log] [blame]
Victor Stinnerb9783d22020-01-24 10:22:18 +01001.. _devmode:
2
3Python Development Mode
4=======================
5
6.. versionadded:: 3.7
7
8The Python Development Mode introduces additional runtime checks that are too
9expensive to be enabled by default. It should not be more verbose than the
10default if the code is correct; new warnings are only emitted when an issue is
11detected.
12
13It can be enabled using the :option:`-X dev <-X>` command line option or by
14setting the :envvar:`PYTHONDEVMODE` environment variable to ``1``.
15
16Effects of the Python Development Mode
17======================================
18
19Enabling the Python Development Mode is similar to the following command, but
20with additional effects described below::
21
22 PYTHONMALLOC=debug PYTHONASYNCIODEBUG=1 python3 -W default -X faulthandler
23
24Effects of the Python Development Mode:
25
26* Add ``default`` :ref:`warning filter <describing-warning-filters>`. The
27 following warnings are shown:
28
29 * :exc:`DeprecationWarning`
30 * :exc:`ImportWarning`
31 * :exc:`PendingDeprecationWarning`
32 * :exc:`ResourceWarning`
33
34 Normally, the above warnings are filtered by the default :ref:`warning
35 filters <describing-warning-filters>`.
36
37 It behaves as if the :option:`-W default <-W>` command line option is used.
38
39 Use the :option:`-W error <-W>` command line option or set the
40 :envvar:`PYTHONWARNINGS` environment variable to ``error`` to treat warnings
41 as errors.
42
43* Install debug hooks on memory allocators to check for:
44
45 * Buffer underflow
46 * Buffer overflow
47 * Memory allocator API violation
48 * Unsafe usage of the GIL
49
50 See the :c:func:`PyMem_SetupDebugHooks` C function.
51
52 It behaves as if the :envvar:`PYTHONMALLOC` environment variable is set to
53 ``debug``.
54
55 To enable the Python Development Mode without installing debug hooks on
56 memory allocators, set the :envvar:`PYTHONMALLOC` environment variable to
57 ``default``.
58
59* Call :func:`faulthandler.enable` at Python startup to install handlers for
60 the :const:`SIGSEGV`, :const:`SIGFPE`, :const:`SIGABRT`, :const:`SIGBUS` and
61 :const:`SIGILL` signals to dump the Python traceback on a crash.
62
63 It behaves as if the :option:`-X faulthandler <-X>` command line option is
64 used or if the :envvar:`PYTHONFAULTHANDLER` environment variable is set to
65 ``1``.
66
67* Enable :ref:`asyncio debug mode <asyncio-debug-mode>`. For example,
68 :mod:`asyncio` checks for coroutines that were not awaited and logs them.
69
70 It behaves as if the :envvar:`PYTHONASYNCIODEBUG` environment variable is set
71 to ``1``.
72
73* Check the *encoding* and *errors* arguments for string encoding and decoding
74 operations. Examples: :func:`open`, :meth:`str.encode` and
75 :meth:`bytes.decode`.
76
77 By default, for best performance, the *errors* argument is only checked at
78 the first encoding/decoding error and the *encoding* argument is sometimes
79 ignored for empty strings.
80
81* The :class:`io.IOBase` destructor logs ``close()`` exceptions.
82* Set the :attr:`~sys.flags.dev_mode` attribute of :attr:`sys.flags` to
83 ``True``.
84
85The Python Development Mode does not enable the :mod:`tracemalloc` module by
86default, because the overhead cost (to performance and memory) would be too
87large. Enabling the :mod:`tracemalloc` module provides additional information
88on the origin of some errors. For example, :exc:`ResourceWarning` logs the
89traceback where the resource was allocated, and a buffer overflow error logs
90the traceback where the memory block was allocated.
91
92The Python Development Mode does not prevent the :option:`-O` command line
93option from removing :keyword:`assert` statements nor from setting
94:const:`__debug__` to ``False``.
95
Victor Stinner4b9aad42020-11-02 16:49:54 +010096The Python Development Mode can only be enabled at the Python startup. Its
97value can be read from :data:`sys.flags.dev_mode <sys.flags>`.
98
Victor Stinnerb9783d22020-01-24 10:22:18 +010099.. versionchanged:: 3.8
100 The :class:`io.IOBase` destructor now logs ``close()`` exceptions.
101
102.. versionchanged:: 3.9
103 The *encoding* and *errors* arguments are now checked for string encoding
104 and decoding operations.
105
106
107ResourceWarning Example
108=======================
109
110Example of a script counting the number of lines of the text file specified in
111the command line::
112
113 import sys
114
115 def main():
116 fp = open(sys.argv[1])
117 nlines = len(fp.readlines())
118 print(nlines)
119 # The file is closed implicitly
120
121 if __name__ == "__main__":
122 main()
123
124The script does not close the file explicitly. By default, Python does not emit
125any warning. Example using README.txt, which has 269 lines:
126
127.. code-block:: shell-session
128
129 $ python3 script.py README.txt
130 269
131
132Enabling the Python Development Mode displays a :exc:`ResourceWarning` warning:
133
134.. code-block:: shell-session
135
136 $ python3 -X dev script.py README.txt
137 269
138 script.py:10: ResourceWarning: unclosed file <_io.TextIOWrapper name='README.rst' mode='r' encoding='UTF-8'>
139 main()
140 ResourceWarning: Enable tracemalloc to get the object allocation traceback
141
142In addition, enabling :mod:`tracemalloc` shows the line where the file was
143opened:
144
145.. code-block:: shell-session
146
147 $ python3 -X dev -X tracemalloc=5 script.py README.rst
148 269
149 script.py:10: ResourceWarning: unclosed file <_io.TextIOWrapper name='README.rst' mode='r' encoding='UTF-8'>
150 main()
151 Object allocated at (most recent call last):
152 File "script.py", lineno 10
153 main()
154 File "script.py", lineno 4
155 fp = open(sys.argv[1])
156
157The fix is to close explicitly the file. Example using a context manager::
158
159 def main():
160 # Close the file explicitly when exiting the with block
161 with open(sys.argv[1]) as fp:
162 nlines = len(fp.readlines())
163 print(nlines)
164
165Not closing a resource explicitly can leave a resource open for way longer than
166expected; it can cause severe issues upon exiting Python. It is bad in
167CPython, but it is even worse in PyPy. Closing resources explicitly makes an
168application more deterministic and more reliable.
169
170
171Bad file descriptor error example
172=================================
173
174Script displaying the first line of itself::
175
176 import os
177
178 def main():
179 fp = open(__file__)
180 firstline = fp.readline()
181 print(firstline.rstrip())
182 os.close(fp.fileno())
183 # The file is closed implicitly
184
185 main()
186
187By default, Python does not emit any warning:
188
189.. code-block:: shell-session
190
191 $ python3 script.py
192 import os
193
194The Python Development Mode shows a :exc:`ResourceWarning` and logs a "Bad file
195descriptor" error when finalizing the file object:
196
197.. code-block:: shell-session
198
199 $ python3 script.py
200 import os
201 script.py:10: ResourceWarning: unclosed file <_io.TextIOWrapper name='script.py' mode='r' encoding='UTF-8'>
202 main()
203 ResourceWarning: Enable tracemalloc to get the object allocation traceback
204 Exception ignored in: <_io.TextIOWrapper name='script.py' mode='r' encoding='UTF-8'>
205 Traceback (most recent call last):
206 File "script.py", line 10, in <module>
207 main()
208 OSError: [Errno 9] Bad file descriptor
209
210``os.close(fp.fileno())`` closes the file descriptor. When the file object
211finalizer tries to close the file descriptor again, it fails with the ``Bad
212file descriptor`` error. A file descriptor must be closed only once. In the
213worst case scenario, closing it twice can lead to a crash (see :issue:`18748`
214for an example).
215
216The fix is to remove the ``os.close(fp.fileno())`` line, or open the file with
217``closefd=False``.