how to convert all warnings to errors?

Alex Martelli aleax at aleax.it
Tue Oct 1 21:46:28 CEST 2002


Skip Montanaro wrote:

> I thought there was a simple command line flag to turn warnings into
> errors,
> but I can't seem to find it.  Pointers?

python -W error

should do it.  E.g.:

[alex at arthur alex]$ cat warn.py
import sys, warnings
print 'before'
warnings.warn(' '.join(sys.argv[1:]))
print 'after'
 
[alex at arthur alex]$ python warn.py one two
before
warn.py:3: UserWarning: one two
  warnings.warn(' '.join(sys.argv[1:]))
after
[alex at arthur alex]$ python -W error warn.py one two
before
Traceback (most recent call last):
  File "warn.py", line 3, in ?
    warnings.warn(' '.join(sys.argv[1:]))
  File "/usr/local/lib/python2.2/warnings.py", line 42, in warn
    warn_explicit(message, category, filename, lineno, module, registry)
  File "/usr/local/lib/python2.2/warnings.py", line 71, in warn_explicit
    raise category(message)
UserWarning: one two
[alex at arthur alex]$

More generally, -W lets you specify conditions that warnings must match
as well as actions (such as error) to perform when they do.  python -h
mentions this (rather summarily, of course) and the online docs for module
warnings gives more details.


Alex




More information about the Python-list mailing list