-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dbm.open should be a context manager #63481
Comments
This code doesn't work. I think it should. import dbm
with dbm.open("what is box.db", "c") as db:
db["Bpoind"] = Boing Indeed, there is nothing supporting PEP-343 for dbm on my system: |
Working on a patch for this. |
Here's a patch. |
I agree with the revised title. Test_context_manager() should also test that db is actually closed after the with statement. I presume you can use self.assertRaises and try to do something with db that will fail if it is properly closed. with self.assertRaises(<NotOpenError>):
db['a'] = 'a' # or whatever I haven't looked at the C code. |
Attached patch checks that the db is actually closed after the >>> import dbm.dumb as d
>>> db = d.open('test.dat', 'c')
>>> db.close()
>>> db.keys()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/tank/libs/cpython/Lib/dbm/dumb.py", line 212, in keys
return list(self._index.keys())
AttributeError: 'NoneType' object has no attribute 'keys'
>>> vs >>> import dbm.gnu as g
>>> db = g.open('test.dat', 'c')
>>> db.close()
>>> db.keys()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
_gdbm.error: GDBM object has already been closed
>>> |
This seems like a reasonable request. |
There is bpo-19385 for the inconsistency of dbm.dumb, with a patch. |
Patch looks reasonable to me - if nobody beats me to it, I'll commit this before beta 1 :) |
New changeset c2f1bb56760d by Nick Coghlan in branch 'default': |
Thanks Claudiu! Additional tweaks in the committed version:
|
I also changed the signature of the enter/exit methods to just use PyObject * (rather than dbmobject *), since that allowed a bunch of casts to be removed and eliminated a couple of compiler warnings about type mismatches. |
Cool, thanks! |
New changeset 200207e50cbf by R David Murray in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: