bug with exception handling or subtle bug on my end?

Chris Green cmg at dok.org
Thu Dec 18 12:27:17 EST 2003


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.ConfigParser):
    default_conf = '/tmp/ugh.conf'

    def __init__(self, filename=None):
        ConfigParser.ConfigParser.__init__(self)
        if not filename:
            filename = self.default_conf
        self.readfp(open(filename))

    def getNetworkConfig(self):
        "test"
        try:
            # items = ConfigParser.ConfigParser.items(self,'Network')
            items = self.items('Network')
        except UGHConfigParser.NoSectionError:
            items = []
        except self.NoSectionError: 
            items = []
        except ConfigParser.NoSectionError:
            items = []
        except IGiveUp:
            items = []
        except:
            items = []

        return items

parser = UGHConfigParser()
print "this works!"
parser.getNetworkConfig()

print "this doesn't!"
for i in parser.getNetworkConfig():
    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.getNetworkConfig():
  File "/usr/lib/python2.3/ConfigParser.py", line 537, in items
    raise NoSectionError(section)
ConfigParser.NoSectionError: No section: 'Network'

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




More information about the Python-list mailing list