Uncatchable AttributeError, shelve raises exceptions in __del__

Discussion in 'Python' started by seth, Oct 12, 2003.

  1. seth

    seth Guest

    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

    >>> 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.
     
    seth, Oct 12, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AAguiar
    Replies:
    5
    Views:
    937
  2. Steven Cheng[MSFT]
    Replies:
    11
    Views:
    617
    AAguiar
    Jan 23, 2004
  3. =?Utf-8?B?c3VkaGVlcg==?=

    Unable to run asp.net application, it raises error

    =?Utf-8?B?c3VkaGVlcg==?=, Apr 8, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    436
    Alvin Bruney [MVP]
    Apr 11, 2004
  4. Rune
    Replies:
    6
    Views:
    436
    Raymond Hettinger
    Aug 26, 2003
  5. Brian Schröder
    Replies:
    1
    Views:
    124
    Austin Ziegler
    Sep 26, 2004
Loading...

Share This Page