why doesn't this filter the warning?

T

Terry Carroll

If I do the following code, whether in the interactive session or in a
file, I get a FutureWarning:
<stdin>:1: FutureWarning: hex/oct constants > sys.maxint will return
positive values in Python 2.4 and up

Okay; that's expected. No problem, I'll just turn off the
FutureWarning. Here's the results from within an interactive session:

Note that no warning is produced. Great; that's what I wanted.

But now, the same thing, but executing a file, rather than
interactively:

[begin-quote]
C:\test\misc>cat testfw.py
import warnings
warnings.filterwarnings('ignore', "hex/oct constants", FutureWarning)
x = 0xffffffff

C:\test\misc>python testfw.py
testfw.py:3: FutureWarning: hex/oct constants > sys.maxint will return
positive values in Python 2.4 and up
x = 0xffffffff
[end-quote]

Why won't the filterwarnings call suppress this?
 
J

Jeff Epler

Why won't the filterwarnings call suppress this?

The warning is produced at the time the code is *parsed*, not when it is
*executed*. You could suppress the warning in an imported module by
putting the suppression above the import of the problem module, for
instance.

This use of 'compile' to produce a code object which is subsequently
called demonstrates the point:-1

Or, without using weird functions not used from day to day, so does
this:-1

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFBcx35Jd01MZaTXX0RAn3sAKCbZqKPTRNx68gLBR8UzwQLGwaVgwCeOUOK
H0ApHbTFeR36qXV+xXEpm4Y=
=NKXH
-----END PGP SIGNATURE-----
 
T

Terry Carroll

The warning is produced at the time the code is *parsed*, not when it is
*executed*. You could suppress the warning in an imported module by
putting the suppression above the import of the problem module, for
instance.

Thanks!

The problem code was actually:

sum = sum & 0xffffffff

Which was unnecessary, so I removed it, but I was curious why the
suppression technique, my first cut at fixing it (just in case I
misunderstood something) didn't work.
 

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