bug with exception handling or subtle bug on my end?

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

    bug with exception handling or subtle bug on my end?

    I'm encountering a place where if I use a for loop I can't catch
    the error raised by a member.

    I've tested python 2.3 and 2.3.1 ( the only ones I had quickly around
    ) and it's still occuring. Any insight would be appreciated but I
    can't figure out why the stand alone call works but for i in doesn't.

    I can work around this for now but if someone could please explain
    this behavior, it's driving me crazy. I've tried catching everything I
    could think of.. Thanks!

    #!/bin/env python
    import sys, os, ConfigParser

    class UGHConfigParser (ConfigParser.C onfigParser):
    default_conf = '/tmp/ugh.conf'

    def __init__(self, filename=None):
    ConfigParser.Co nfigParser.__in it__(self)
    if not filename:
    filename = self.default_co nf
    self.readfp(ope n(filename))

    def getNetworkConfi g(self):
    "test"
    try:
    # items = ConfigParser.Co nfigParser.item s(self,'Network ')
    items = self.items('Net work')
    except UGHConfigParser .NoSectionError :
    items = []
    except self.NoSectionE rror:
    items = []
    except ConfigParser.No SectionError:
    items = []
    except IGiveUp:
    items = []
    except:
    items = []

    return items

    parser = UGHConfigParser ()
    print "this works!"
    parser.getNetwo rkConfig()

    print "this doesn't!"
    for i in parser.getNetwo rkConfig():
    print i

    Output:

    % python ugh.py
    this works!
    this doesn't!
    Traceback (most recent call last):
    File "ugh.py", line 41, in ?
    for i in parser.getNetwo rkConfig():
    File "/usr/lib/python2.3/ConfigParser.py ", line 537, in items
    raise NoSectionError( section)
    ConfigParser.No SectionError: No section: 'Network'

    --
    Chris Green <cmg@dok.org>
    "I'm beginning to think that my router may be confused."
  • Peter Otten

    #2
    Re: bug with exception handling or subtle bug on my end?

    Chris Green wrote:
    [color=blue]
    > I'm encountering a place where if I use a for loop I can't catch
    > the error raised by a member.
    >
    > I've tested python 2.3 and 2.3.1 ( the only ones I had quickly around
    > ) and it's still occuring. Any insight would be appreciated but I
    > can't figure out why the stand alone call works but for i in doesn't.
    >
    > I can work around this for now but if someone could please explain
    > this behavior, it's driving me crazy. I've tried catching everything I
    > could think of.. Thanks!
    >[/color]

    The reason for this strange behaviour boils down to the following:

    SyntaxError: invalid syntax[color=blue][color=green][color=darkred]
    >>> def t():[/color][/color][/color]
    .... raise Exception, "so what"
    .... for i in range(3):
    .... yield i
    ....[color=blue][color=green][color=darkred]
    >>> x = t()
    >>> x.next()[/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "<stdin>", line 2, in t
    Exception: so what[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    I. e., an exception from inside a generator (and ConfigParser.it ems() is
    such a biest) is only thrown on the first invocation of its next() method
    which is implicitly done by the for loop. So you cannot catch the exception
    because it is just not raised inside the getNetworkConfi g() method.
    I would consider this behaviour a bug in ConfigParser.
    As a workaround, you can do (both untested):

    def getNetworkConfi g(self):
    if self.has_sectio n("Network"):
    return self.items("Net work")
    else:
    return []

    or

    def getNetworkConfi g(self):
    try:
    return list(self.items ("Network"))
    except ConfigParser.No SectionError:
    return []

    Peter

    Comment

    • Michael Schutte

      #3
      Re: bug with exception handling or subtle bug on my end?

      Chris Green wrote:[color=blue]
      > I'm encountering a place where if I use a for loop I can't catch
      > the error raised by a member.
      >
      > I can work around this for now but if someone could please explain
      > this behavior, it's driving me crazy. I've tried catching everything I
      > could think of.. Thanks!
      >
      > #!/bin/env python
      > import sys, os, ConfigParser
      >
      > class UGHConfigParser (ConfigParser.C onfigParser):
      > default_conf = '/tmp/ugh.conf'
      >
      > def __init__(self, filename=None):
      > ConfigParser.Co nfigParser.__in it__(self)
      > if not filename:
      > filename = self.default_co nf
      > self.readfp(ope n(filename))
      >
      > def getNetworkConfi g(self):
      > "test"
      > try:
      > # items = ConfigParser.Co nfigParser.item s(self,'Network ')
      > items = self.items('Net work')
      > except UGHConfigParser .NoSectionError :
      > items = []
      > except self.NoSectionE rror:
      > items = []
      > except ConfigParser.No SectionError:
      > items = []
      > except IGiveUp:
      > items = []
      > except:
      > items = []
      >
      > return items
      >
      > parser = UGHConfigParser ()
      > print "this works!"
      > parser.getNetwo rkConfig()
      >
      > print "this doesn't!"
      > for i in parser.getNetwo rkConfig():
      > print i
      >
      > Output:
      >
      > % python ugh.py
      > this works!
      > this doesn't!
      > Traceback (most recent call last):
      > File "ugh.py", line 41, in ?
      > for i in parser.getNetwo rkConfig():
      > File "/usr/lib/python2.3/ConfigParser.py ", line 537, in items
      > raise NoSectionError( section)
      > ConfigParser.No SectionError: No section: 'Network'
      >[/color]

      On my machine (MS Windows ME), with a Python 2.2.1 environment, the code
      does its work. This might be a bug in Python 2.3, but I don't use it
      (and don't want to update), but I don't think so.
      First, remove every except: statement except the last one. Does it work
      now? I have done this, because Python complained:

      NameError: UGHConfigParser instance has no attribute "NoSectionError " .

      But this might have changed with Python 2.3.
      If this doesn't work, try to use Python 2.2 (on an other machine). I am
      not able to understand, why this script works on my machine, and not on
      your one.

      If it really is a Python 2.3-bug, you should report it, you know...

      --
      Michael Schutte <m.schutte@aon. at>


      Comment

      Working...