warnings filter?

Peter Otten __peter__ at web.de
Fri Jul 9 15:33:04 EDT 2004


rob at encodia.biz wrote:

> In Python 2.3, this code still prints a warning (when run as a script,
> not from the interpreter).  How do I get rid of the warning?  I'd like
> to do it without passing command line args to python.
> 
> import warnings
> warnings.filterwarnings('ignore')
> a = 0xffffffff

The warning is issued during the compilation, not the execution of your
script. Therefore the attempt to turn off the warning, although before the
offensive statement, comes too late.

<offensive.py>
a = 0xffffffff
</offensive.py>

One workaround is to precompile offensive.py (By the way is there a flag to
do this that I've overlooked?):

$ python -c"import offensive"
offensive.py:1: FutureWarning: hex/oct constants > sys.maxint will return
positive values in Python 2.4 and up
  a = 0xffffffff


and then invoke offensive.pyc:

$ python offensive.pyc

All quiet :-)

The other option would be to reorganize your script to the same effect.

$ rm offensive.pyc
$ python driver.py
$

Where driver looks like so:

<driver.py>
import warnings
warnings.filterwarnings('ignore') # should probably be more specific

import offensive
</driver>

Of course you could omit the first two lines when you can live with a
warning the first time you run driver.py after every change of
offensive.py.

Peter









More information about the Python-list mailing list