Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Uncatchable AttributeError, shelve raises exceptions in __del__

Reply
Thread Tools

Uncatchable AttributeError, shelve raises exceptions in __del__

 
 
seth
Guest
Posts: n/a
 
      10-12-2003
Last week I encountered an AttributeError in my unit tests that I
wasn'table to catch with an "except AttributeError" statement.

The problem stemmed from a class that raised an error inside
__init__and defined a __del__ method to clean up resources. I then
discovered asimilar problem in the shelve module. This led me to two
importantdiscoveries:

1. Attributes defined in __init__ after an error is raised will not be
apart of the "self" passed to __del__. On reflection, this seems
quiteobvious, but it is easy to have this bite you because it is
"morePythonic", I think, to define class attributes inside __init__ on
anas-needed basis. In less dynamic languages, say Java, one defines
allclass attributes at the class level, which is allowed in Python,
and soall class attributes are defined when the constructor is called.

2. As documented(http://www.python.org/doc/current/re...mization.html),
If anexception is raised in __del__, a message is printed to stderr
and it isignored. This means that such an exception cannot be caught
with"except"! This can be a pain if such an error is raised in a
modulethat isn't your code.

As an aside, why isn't __del__ documentation in the index with the
other"_ (underscore)" stuff?
See:http://www.python.org/doc/current/lib/genindex.html

Here's an example that shows how the location of attribute
definitionsinside __init__ can lead to AttributeErrors in __del__ (and
henceuncatchable errors):

class AttributeErrorRaiser:
def __init__(self, be_bad=True):
self.earlyAttribute = "early"
if be_bad:
raise SomeError()
self.lateAttribute = "late"
def __del__(self):
print self.earlyAttribute
print self.lateAttribute

class SomeError(Exception):
def __init__(self, args=None):
self.args = args

>>> try:

... aer = AttributeErrorRaiser()
... except SomeError:
... print "Caught SomeError"
...
Caught SomeError
early
Exception exceptions.AttributeError: "AttributeErrorRaiser
instance has no attribute 'lateAttribute'" in <bound method
AttributeErrorRaiser.__del__ of <trytocatchme.AttributeErrorRaiser
instance at 0x402e9d0c>> ignored

And here is the same programming error in the shelve module. Assume
that 'notashelf.txt' is an existing plain text file:

import shelve
import anydbm

class ShowShelfBadness:
def __init__(self):
try:
self.shelf = shelve.open('notashelf.txt')
except anydbm.error:
print "caught a shelve related error"

>>> ssb = ShowShelfBadness()

caught a shelve related error
Exception exceptions.AttributeError: "DbfilenameShelf instance has
no attribute 'writeback'" in ignored

The workaround that I found was to use the whichdb module to test for
a valid shelf-type file, if the file exists, before opening with
shelve, but I still don't like the fact that shelve.open raises an
"uncatchable" error when you give it a bad file.

Questions:

Does anyone have suggestions for "best practice" w.r.t. defining
attributes in __init__ or at the class level?

Thoughts on workarounds for the behavior shown in the shelve module?


Thanks for listening.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
rdoc Question: How to document which exceptions a function raises Brian Schröder Ruby 1 09-26-2004 02:21 PM
Unable to run asp.net application, it raises error =?Utf-8?B?c3VkaGVlcg==?= ASP .Net 1 04-11-2004 05:17 AM
RE: C# call to C++ static class raises NullReferenceException when web.config gets modifided Steven Cheng[MSFT] ASP .Net 11 01-23-2004 12:37 PM
C# call to C++ static class raises NullReferenceException when web.config gets modifided AAguiar ASP .Net 5 12-22-2003 02:53 AM
Uncatchable socket.error in socket.py (?) Rune Python 6 08-26-2003 08:45 PM



Advertisments