Error type for shelve.open()

Simon Forman rogue_pedro at yahoo.com
Fri Jul 7 12:18:21 EDT 2006


aomighty at gmail.com wrote:
> I tried what you said and it looked like maybe AttributeError, but that
> didn't work either.
>
> This code snippet:
>
> import shelve
> from traceback import format_exc
>
> try:
>    db = shelve.open("meh", "r")
> except:
>    print format_exc()
>
> Gave me this output:
> Traceback (most recent call last):
>   File "test.py", line 5, in ?
>     db = shelve.open("meh", "r")
>   File "/usr/lib/python2.4/shelve.py", line 231, in open
>     return DbfilenameShelf(filename, flag, protocol, writeback, binary)
>   File "/usr/lib/python2.4/shelve.py", line 212, in __init__
>     Shelf.__init__(self, anydbm.open(filename, flag), protocol,
> writeback, binary)
>   File "/usr/lib/python2.4/anydbm.py", line 77, in open
>     raise error, "need 'c' or 'n' flag to open new db"
> error: need 'c' or 'n' flag to open new db
>
> Exception exceptions.AttributeError: "DbfilenameShelf instance has no
> attribute 'writeback'" in  ignored
>
> Do you know what the error is?

No.  If you tried catching AttributeError and it didn't work then I'd
guess that the AttributeError is a secondary result of the initial
error.

This part of the traceback,

>   File "/usr/lib/python2.4/anydbm.py", line 77, in open
>     raise error, "need 'c' or 'n' flag to open new db"
> error: need 'c' or 'n' flag to open new db

indicates that some sort of custom error, probably defined in the
anydbm.py module.

Catching the execption and binding it to a var,

>>> try:
...     db = shelve.open("meh", "r")
... except Exception, err:
...     E = err
...
Exception exceptions.AttributeError: "DbfilenameShelf instance has no
attribute 'writeback'" in  ignored
>>> E
<anydbm.error instance at 0xb7d8270c>



So:
>>> from anydbm import error
>>> try:
...     db = shelve.open("meh", "r")
... except error:
...     print 'Aha!  got it!'
...
Aha!  got it!
Exception exceptions.AttributeError: "DbfilenameShelf instance has no
attribute 'writeback'" in  ignored


Well, that catches the error, but I don't know what's going on with the
additional AttributeError or what to do about it.

Peace,
~Simon




More information about the Python-list mailing list