Uncatchable AttributeError, shelve raises exceptions in __del__

S

seth

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/ref/customization.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
... 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"
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.
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top