Skip to content

Commit 62ec638

Browse files
authored
bpo-43862: Enhance -W cmdline option documentation (GH-25439)
The -W format is "action:message:category:module:lineno". Update also the Python manual page.
1 parent 0ad81d4 commit 62ec638

File tree

4 files changed

+113
-80
lines changed

4 files changed

+113
-80
lines changed

Doc/c-api/init_config.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,13 @@ PyConfig
11351135
item of :data:`warnings.filters` which is checked first (highest
11361136
priority).
11371137
1138+
The :option:`-W` command line options adds its value to
1139+
:c:member:`~PyConfig.warnoptions`, it can be used multiple times.
1140+
1141+
The :envvar:`PYTHONWARNINGS` environment variable can also be used to add
1142+
warning options. Multiple options can be specified, separated by commas
1143+
(``,``).
1144+
11381145
Default: empty list.
11391146
11401147
.. c:member:: int write_bytecode

Doc/tools/susp-ignored.csv

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,11 @@ tutorial/stdlib2,,:start,"fields = struct.unpack('<IIIHH', data[start:start+16])
279279
tutorial/stdlib2,,:start,filename = data[start:start+filenamesize]
280280
tutorial/stdlib2,,:Warning,WARNING:root:Warning:config file server.conf not found
281281
using/cmdline,,:errorhandler,:errorhandler
282-
using/cmdline,,:line,file:line: category: message
282+
using/cmdline,,:message,action:message:category:module:lineno
283+
using/cmdline,,:category,action:message:category:module:lineno
284+
using/cmdline,,:module,action:message:category:module:lineno
285+
using/cmdline,,:lineno,action:message:category:module:lineno
286+
using/cmdline,,::,-W ignore::DeprecationWarning
283287
using/unix,,:Packaging,https://en.opensuse.org/Portal:Packaging
284288
whatsnew/2.0,,:len,
285289
whatsnew/2.3,,::,

Doc/using/cmdline.rst

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -380,25 +380,8 @@ Miscellaneous options
380380
.. _using-on-warnings:
381381
.. cmdoption:: -W arg
382382

383-
Warning control. Python's warning machinery by default prints warning
384-
messages to :data:`sys.stderr`. A typical warning message has the following
385-
form:
386-
387-
.. code-block:: none
388-
389-
file:line: category: message
390-
391-
By default, each warning is printed once for each source line where it
392-
occurs. This option controls how often warnings are printed.
393-
394-
Multiple :option:`-W` options may be given; when a warning matches more than
395-
one option, the action for the last matching option is performed. Invalid
396-
:option:`-W` options are ignored (though, a warning message is printed about
397-
invalid options when the first warning is issued).
398-
399-
Warnings can also be controlled using the :envvar:`PYTHONWARNINGS`
400-
environment variable and from within a Python program using the
401-
:mod:`warnings` module.
383+
Warning control. Python's warning machinery by default prints warning
384+
messages to :data:`sys.stderr`.
402385

403386
The simplest settings apply a particular action unconditionally to all
404387
warnings emitted by a process (even those that are otherwise ignored by
@@ -411,14 +394,48 @@ Miscellaneous options
411394
-Wonce # Warn once per Python process
412395
-Wignore # Never warn
413396

414-
The action names can be abbreviated as desired (e.g. ``-Wi``, ``-Wd``,
415-
``-Wa``, ``-We``) and the interpreter will resolve them to the appropriate
416-
action name.
397+
The action names can be abbreviated as desired and the interpreter will
398+
resolve them to the appropriate action name. For example, ``-Wi`` is the
399+
same as ``-Wignore``.
400+
401+
The full form of argument is::
402+
403+
action:message:category:module:lineno
404+
405+
Empty fields match all values; trailing empty fields may be omitted. For
406+
example ``-W ignore::DeprecationWarning`` ignores all DeprecationWarning
407+
warnings.
408+
409+
The *action* field is as explained above but only applies to warnings that
410+
match the remaining fields.
411+
412+
The *message* field must match the whole warning message; this match is
413+
case-insensitive.
414+
415+
The *category* field matches the warning category
416+
(ex: ``DeprecationWarning``). This must be a class name; the match test
417+
whether the actual warning category of the message is a subclass of the
418+
specified warning category.
419+
420+
The *module* field matches the (fully-qualified) module name; this match is
421+
case-sensitive.
422+
423+
The *lineno* field matches the line number, where zero matches all line
424+
numbers and is thus equivalent to an omitted line number.
425+
426+
Multiple :option:`-W` options can be given; when a warning matches more than
427+
one option, the action for the last matching option is performed. Invalid
428+
:option:`-W` options are ignored (though, a warning message is printed about
429+
invalid options when the first warning is issued).
430+
431+
Warnings can also be controlled using the :envvar:`PYTHONWARNINGS`
432+
environment variable and from within a Python program using the
433+
:mod:`warnings` module. For example, the :func:`warnings.filterwarnings`
434+
function can be used to use a regular expression on the warning message.
417435

418436
See :ref:`warning-filter` and :ref:`describing-warning-filters` for more
419437
details.
420438

421-
422439
.. cmdoption:: -x
423440

424441
Skip the first line of the source, allowing use of non-Unix forms of

Misc/python.man

Lines changed: 61 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -207,71 +207,76 @@ at exit.
207207
.B \-V ", " \-\-version
208208
Prints the Python version number of the executable and exits. When given
209209
twice, print more information about the build.
210+
210211
.TP
211212
.BI "\-W " argument
212-
Warning control. Python sometimes prints warning message to
213+
Warning control. Python's warning machinery by default prints warning messages
214+
to
213215
.IR sys.stderr .
214-
A typical warning message has the following form:
215-
.IB file ":" line ": " category ": " message.
216-
By default, each warning is printed once for each source line where it
217-
occurs. This option controls how often warnings are printed.
218-
Multiple
219-
.B \-W
220-
options may be given; when a warning matches more than one
221-
option, the action for the last matching option is performed.
222-
Invalid
223-
.B \-W
224-
options are ignored (a warning message is printed about invalid
225-
options when the first warning is issued). Warnings can also be
226-
controlled from within a Python program using the
227-
.I warnings
228-
module.
229216

230-
The simplest form of
231-
.I argument
232-
is one of the following
233-
.I action
234-
strings (or a unique abbreviation):
235-
.B ignore
236-
to ignore all warnings;
237-
.B default
238-
to explicitly request the default behavior (printing each warning once
239-
per source line);
240-
.B all
241-
to print a warning each time it occurs (this may generate many
242-
messages if a warning is triggered repeatedly for the same source
243-
line, such as inside a loop);
244-
.B module
245-
to print each warning only the first time it occurs in each
246-
module;
247-
.B once
248-
to print each warning only the first time it occurs in the program; or
249-
.B error
250-
to raise an exception instead of printing a warning message.
217+
The simplest settings apply a particular action unconditionally to all warnings
218+
emitted by a process (even those that are otherwise ignored by default):
251219

252-
The full form of
253-
.I argument
254-
is
255-
.IB action : message : category : module : line.
256-
Here,
220+
-Wdefault # Warn once per call location
221+
-Werror # Convert to exceptions
222+
-Walways # Warn every time
223+
-Wmodule # Warn once per calling module
224+
-Wonce # Warn once per Python process
225+
-Wignore # Never warn
226+
227+
The action names can be abbreviated as desired and the interpreter will resolve
228+
them to the appropriate action name. For example,
229+
.B -Wi
230+
is the same as
231+
.B -Wignore .
232+
233+
The full form of argument is:
234+
.IB action:message:category:module:lineno
235+
236+
Empty fields match all values; trailing empty fields may be omitted. For
237+
example
238+
.B -W ignore::DeprecationWarning
239+
ignores all DeprecationWarning warnings.
240+
241+
The
257242
.I action
258-
is as explained above but only applies to messages that match the
259-
remaining fields. Empty fields match all values; trailing empty
260-
fields may be omitted. The
243+
field is as explained above but only applies to warnings that match
244+
the remaining fields.
245+
246+
The
261247
.I message
262-
field matches the start of the warning message printed; this match is
263-
case-insensitive. The
248+
field must match the whole printed warning message; this match is
249+
case-insensitive.
250+
251+
The
264252
.I category
265-
field matches the warning category. This must be a class name; the
266-
match test whether the actual warning category of the message is a
267-
subclass of the specified warning category. The full class name must
268-
be given. The
253+
field matches the warning category (ex: "DeprecationWarning"). This must be a
254+
class name; the match test whether the actual warning category of the message
255+
is a subclass of the specified warning category.
256+
257+
The
269258
.I module
270-
field matches the (fully-qualified) module name; this match is
271-
case-sensitive. The
272-
.I line
273-
field matches the line number, where zero matches all line numbers and
274-
is thus equivalent to an omitted line number.
259+
field matches the (fully-qualified) module name; this match is case-sensitive.
260+
261+
The
262+
.I lineno
263+
field matches the line number, where zero matches all line numbers and is thus
264+
equivalent to an omitted line number.
265+
266+
Multiple
267+
.B -W
268+
options can be given; when a warning matches more than one option, the action
269+
for the last matching option is performed. Invalid
270+
.B -W
271+
options are ignored (though, a warning message is printed about invalid options
272+
when the first warning is issued).
273+
274+
Warnings can also be controlled using the
275+
.B PYTHONWARNINGS
276+
environment variable and from within a Python program using the warnings
277+
module. For example, the warnings.filterwarnings() function can be used to use
278+
a regular expression on the warning message.
279+
275280
.TP
276281
.BI "\-X " option
277282
Set implementation specific option. The following options are available:

0 commit comments

Comments
 (0)