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

Discussion in 'Python' started by hofer, Sep 26, 2008.

  1. hofer

    hofer Guest

    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
     
    hofer, Sep 26, 2008
    #1
    1. Advertising

  2. hofer

    Peter Otten Guest

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

    hofer wrote:

    > 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
     
    Peter Otten, Sep 26, 2008
    #2
    1. Advertising

  3. hofer

    hofer Guest

    Re: Understanding (and getting rid) of optparse.py:668:

    On Sep 26, 6:21 pm, Peter Otten <> wrote:
    > hofer wrote:
    > > 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

    >
    > 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
     
    hofer, Sep 26, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,252
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,712
    Old Wolf
    Jan 20, 2004
  3. Grant Edwards
    Replies:
    8
    Views:
    410
    J Correia
    Apr 6, 2005
  4. Tomi Silander
    Replies:
    3
    Views:
    979
    Grant Edwards
    Apr 6, 2005
  5. kosuke

    optparse.py: FutureWarning error

    kosuke, Jun 2, 2005, in forum: Python
    Replies:
    14
    Views:
    686
    John Abel
    Jun 9, 2005
Loading...

Share This Page