[Tutor] Why doesn't this filter FutureWarning?

Terry Carroll carroll at tjc.com
Mon Oct 18 03:00:50 CEST 2004


Here's what I get in the interactive session:

>>> num=0x1234
>>> import warnings
>>> num=num & 0xffffffff
<stdin>:1: FutureWarning: hex/oct constants > sys.maxint will return positive va
lues in Python 2.4 and up
>>> warnings.filterwarnings('ignore', "hex/oct constants", FutureWarning)
>>> num=num & 0xffffffff


That's just what I'd expect.  I get the FutureWarning on the first try, 
but after invoking filterwarnings, I no longer get it.

But when I do the same code from inside a file:

<begin quote>

C:\test>cat fw.py
num=0x1234
import warnings
num=num & 0xffffffff
warnings.filterwarnings('ignore', "hex/oct constants", FutureWarning)
num=num & 0xffffffff

C:\test>python fw.py
fw.py:3: FutureWarning: hex/oct constants > sys.maxint will return positive valu
es in Python 2.4 and up
  num=num & 0xffffffff
fw.py:5: FutureWarning: hex/oct constants > sys.maxint will return positive valu
es in Python 2.4 and up
  num=num & 0xffffffff

<end-quote>

What gives?  Why would this work in an interactive session, but not from 
a file?



More information about the Tutor mailing list