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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hofer

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

    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_opti on('-G','--green',action= 'store_const', const=
    '#00FF00' , dest='color',
    default='#80808 0',
    help='life is so green')
    parser.add_opti on('-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_ar gs()
    print 'options',optio ns

    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
  • Peter Otten

    #2
    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_opti on('-G','--green',action= 'store_const', const=
    '#00FF00' , dest='color',
    default='#80808 0',
    help='life is so green')
    parser.add_opti on('-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_ar gs()
    print 'options',optio ns
    >
    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

    Python currently distinguishes between two kinds of integers (ints): regular or short ints, limited by the size of a C long (typically 32 or 64 bits), and long ints, which are limited only by available memory. When operations on short ints yield result...


    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::Future Warning myscript.py

    Peter

    Comment

    • hofer

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

      On Sep 26, 6:21 pm, Peter Otten <__pete...@web. dewrote:
      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::Future Warning myscript.py
      >
      Peter
      Thanks a lot Peter,

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


      bye H

      Comment

      Working...