By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,485 Members | 1,061 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,485 IT Pros & Developers. It's quick & easy.

Error type for shelve.open()

P: n/a
I wanted to write the following code:

import shelve
try:
db = shelve.open(file, "r")
except SomeError:
print "Oh no, db not found"

Only, I'm not sure what SomeError should be. I tried error,
anydbm.error, shelve.open.anydb.error, etc. but can't find it. Things
worked fine with simply except:, but that's not optimal.

Does anyone know either the what the error is or where I can find it
for certain?

Thanks,
Martin

Jul 5 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ao******@gmail.com wrote:
I wanted to write the following code:

import shelve
try:
db = shelve.open(file, "r")
except SomeError:
print "Oh no, db not found"

Only, I'm not sure what SomeError should be. I tried error,
anydbm.error, shelve.open.anydb.error, etc. but can't find it. Things
worked fine with simply except:, but that's not optimal.

Does anyone know either the what the error is or where I can find it
for certain?

Thanks,
Martin
What error did you get with just the bare except?

you can find out by saying (for instance):
>>try:
1/0
except Exception, err:
E = err

>>E
<exceptions.ZeroDivisionError instance at 0xb6efd8cc>
or to get really fancy, use the traceback module
>>from traceback import format_exc
try:
1/0
except:
print format_exc()
Traceback (most recent call last):
File "<pyshell#10>", line 2, in ?
ZeroDivisionError: integer division or modulo by zero

Peace,
~Simon

Jul 6 '06 #2

P: n/a
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?

Thanks,
Martin
Simon Forman wrote:
ao******@gmail.com wrote:
I wanted to write the following code:

import shelve
try:
db = shelve.open(file, "r")
except SomeError:
print "Oh no, db not found"

Only, I'm not sure what SomeError should be. I tried error,
anydbm.error, shelve.open.anydb.error, etc. but can't find it. Things
worked fine with simply except:, but that's not optimal.

Does anyone know either the what the error is or where I can find it
for certain?

Thanks,
Martin

What error did you get with just the bare except?

you can find out by saying (for instance):
>try:
1/0
except Exception, err:
E = err

>E
<exceptions.ZeroDivisionError instance at 0xb6efd8cc>
or to get really fancy, use the traceback module
>from traceback import format_exc
try:
1/0
except:
print format_exc()
Traceback (most recent call last):
File "<pyshell#10>", line 2, in ?
ZeroDivisionError: integer division or modulo by zero

Peace,
~Simon
Jul 6 '06 #3

P: n/a
ao******@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

Jul 7 '06 #4

P: n/a
Yes, the problem was that I hadn't imported anydbm.error... it's
working now.

As for the AttributeError at the end, I talked to someone else, and he
looked at the source and said it was a bug in shelve. I think I will
report it to python.org.

Anyway, thanks :).
Simon Forman wrote:
ao******@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
Jul 7 '06 #5

P: n/a
I reported the bug to python.org and apparently it has already been
fixed in the latest SVN build :).
ao******@gmail.com wrote:
Yes, the problem was that I hadn't imported anydbm.error... it's
working now.

As for the AttributeError at the end, I talked to someone else, and he
looked at the source and said it was a bug in shelve. I think I will
report it to python.org.

Anyway, thanks :).
Simon Forman wrote:
ao******@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
Jul 10 '06 #6

P: n/a
ao******@gmail.com wrote:
I reported the bug to python.org and apparently it has already been
fixed in the latest SVN build :).
Awesome! Open Source at work! :D

Jul 10 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.