473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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.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 <cm*@dok.org>
"I'm beginning to think that my router may be confused."
Jul 18 '05 #1
2 1803
Chris Green wrote:
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
def t(): .... raise Exception, "so what"
.... for i in range(3):
.... yield i
.... x = t()
x.next() Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in t
Exception: so what


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
Jul 18 '05 #2
Chris Green wrote:
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...

--
Michael Schutte <m.*******@aon.at>
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Kevin Goodsell | last post by:
If I'm writing a class that will be used as an exception, what kinds of things do I need to watch out for? For example, is it necessary to make sure that the members of the class don't throw? ...
11
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
2
by: hazz | last post by:
I have spent more time than I care to admit trying to track down a very subtle error. Here is my app's xx.exe.config file. <?xml version="1.0" encoding="utf-8" ?> <configuration>...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
13
by: tolisss | last post by:
Hi i have setup a global exception handler b4 Application.Run like Application.ThreadException += new ThreadExceptionEventHandler(GlobalExceptionProcessing.AppThreadException ); then after...
10
by: junw2000 | last post by:
Hi, Below is a simple code about exception. #include <iostream> using namespace std; struct E { const char* message; E(const char* arg) : message(arg) { } };
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.