warnings filter?

R

rob

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


Thanks,
Rob
 
P

Peter Otten

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top