Retry: Question about FutureWarning

Peter Otten __peter__ at web.de
Thu Aug 16 07:43:39 CEST 2007


Steven W. Orr wrote:

> I'm trying again, since no response indicates that I'm not providing 
> enough info.

No, you were providing too much irrelevant information. It would have been
better to put it that way:

> Thanks guys, but that's not my question. The question is this: Why does
> the call to filterwarnings not work if it's placed inside the M1 module?
> Why do I have to place the call in M4 to make it work? This is more a
> question about how import works than it is about what the value of -1 is.
> ;-)

You cannot switch off the warning inside the module because it is issued
during its compilation, not its execution:

$ cat tmp.py
import warnings
warnings.filterwarnings("ignore", category=FutureWarning)
print "FutureWarning switched off"
warnings.warn("yadda", category=FutureWarning)
0xffffffff

$ rm tmp.pyc
$ python2.3 -c'import tmp'
tmp.py:5: FutureWarning: hex/oct constants > sys.maxint will return positive
values in Python 2.4 and up
  0xffffffff
FutureWarning switched off

First you get the warning, then the "switched off" message. Oops.

$ python2.3 -c'import tmp'
FutureWarning switched off

When importing the module a second time you don't get the warning because
the compiled module already exists and is reused.

Note that you can disable warnings on the commmand line...

$ rm tmp.pyc
$ python2.3 -Wignore::FutureWarning -c'import tmp'
FutureWarning switched off

... but as others already told you it is normally not a good idea to squash
these warnings. Well, you have been warned ;)

Peter



More information about the Python-list mailing list