467,074 Members | 1,131 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,074 developers. It's quick & easy.

Uncatchable AttributeError, shelve raises exceptions in __del__

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

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?

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


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.
Jul 18 '05 #1
  • viewed: 2042

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Rami A. Kishek | last post: by
6 posts views Thread by Rune | last post: by
reply views Thread by Erlend Fuglum | last post: by
16 posts views Thread by David Turner | last post: by
reply views Thread by PiedmontBiz | last post: by
7 posts views Thread by cs | last post: by
6 posts views Thread by aomighty@gmail.com | last post: by
4 posts views Thread by Nikhil | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.