bug with exception handling or subtle bug on my end?

C

Chris Green

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'
 
P

Peter Otten

Chris said:
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!

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

SyntaxError: invalid syntax.... raise Exception, "so what"
.... for i in range(3):
.... yield i
....Traceback (most recent call last):
File "<stdin>", line 1, in ?

I. e., an exception from inside a generator (and ConfigParser.items() 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 getNetworkConfig() method.
I would consider this behaviour a bug in ConfigParser.
As a workaround, you can do (both untested):

def getNetworkConfig(self):
if self.has_section("Network"):
return self.items("Network")
else:
return []

or

def getNetworkConfig(self):
try:
return list(self.items("Network"))
except ConfigParser.NoSectionError:
return []

Peter
 
M

Michael Schutte

Chris said:
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.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'

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...
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top