Understanding (and getting rid) of optparse.py:668: FutureWarning:%u/%o/%x/%X of negative int will r

H

hofer

Hi,

I get following warning with a python script:


optparse.py:668: FutureWarning: %u/%o/%x/%X of negative int will
return a signed string in Python 2.4 and up


my code:
from optparse import OptionParser

if __name__ == '__main__':
parser = OptionParser()
parser.add_option('-G','--green',action= 'store_const', const=
'#00FF00' , dest='color',
default='#808080',
help='life is so green')
parser.add_option('-R','--red',action= 'store_const', const =
'#FF0000' , dest='color',
help='I just see red')
# add more elaborated command line parsing and help text here
(options,argv) = parser.parse_args()
print 'options',options

I assume python wants to tell me that newer version will behave
differently for numeric arguments

What I wonder is: Why do I get the warning if my code doesn't try to
parse any numbers?

Is there any way to get rid of the warning without having to change
the python version?
(I noticed, the warning disappears if I remove the line printing
options)





thanks for any explanations. suggestions


H
 
P

Peter Otten

hofer said:
Hi,

I get following warning with a python script:


optparse.py:668: FutureWarning: %u/%o/%x/%X of negative int will
return a signed string in Python 2.4 and up


my code:
from optparse import OptionParser

if __name__ == '__main__':
parser = OptionParser()
parser.add_option('-G','--green',action= 'store_const', const=
'#00FF00' , dest='color',
default='#808080',
help='life is so green')
parser.add_option('-R','--red',action= 'store_const', const =
'#FF0000' , dest='color',
help='I just see red')
# add more elaborated command line parsing and help text here
(options,argv) = parser.parse_args()
print 'options',options

I assume python wants to tell me that newer version will behave
differently for numeric arguments

What I wonder is: Why do I get the warning if my code doesn't try to
parse any numbers?

The culprit is

print options

If you look into optparse.py you'll see that part of the __repr__() method
of the Value class is the object's address, roughly

"%x" % id(self)

id(self) is just the Value instance's address in memory which seems to be
= 0x80000000 (assuming you are on a 32-bit machine) in your case.

Such numbers were interpreted as negative ints but are now treated as
positive longs. Read

http://www.python.org/dev/peps/pep-0237/

for details.
Is there any way to get rid of the warning without having to change
the python version?
(I noticed, the warning disappears if I remove the line printing
options)

You can print options.__dict__ instead of options with little loss of
information, or turn the warning off

python -Wignore::FutureWarning myscript.py

Peter
 
H

hofer

You can print options.__dict__ instead of options with little loss of
information, or turn the warning off

python -Wignore::FutureWarning myscript.py

Peter

Thanks a lot Peter,

Any trick to disable warnings just for a given range of code?


bye H
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top