Issue 19282: dbm.open should be a context manager (original) (raw)

Created on 2013-10-18 07:45 by kousu, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
dbm.patch Claudiu.Popa,2013-10-18 10:45 review
dbm1.patch Claudiu.Popa,2013-10-19 06:29 review
Messages (13)
msg200191 - (view) Author: Nick Guenther (kousu) Date: 2013-10-18 07:45
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: [kousu@galleon ~]$ grep -r __exit__ /usr/lib/python3.3/dbm [kousu@galleon ~]$
msg200232 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2013-10-18 09:10
Working on a patch for this.
msg200241 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2013-10-18 10:45
Here's a patch.
msg200336 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-10-19 00:16
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(): db['a'] = 'a' # or whatever I haven't looked at the C code.
msg200376 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2013-10-19 06:29
Attached patch checks that the db is actually closed after the `with`. Also, I noticed that dbm.dumb doesn't behave like the rest of the variants, as seen in the following: >>> import dbm.dumb as d >>> db = d.open('test.dat', 'c') >>> db.close() >>> db.keys() Traceback (most recent call last): File "", line 1, in 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 "", line 1, in _gdbm.error: GDBM object has already been closed >>>
msg200697 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-10-21 04:57
This seems like a reasonable request.
msg201545 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2013-10-28 14:47
There is for the inconsistency of dbm.dumb, with a patch.
msg201818 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2013-10-31 14:33
Patch looks reasonable to me - if nobody beats me to it, I'll commit this before beta 1 :)
msg203119 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-11-17 06:00
New changeset c2f1bb56760d by Nick Coghlan in branch 'default': Close #19282: Native context management in dbm http://hg.python.org/cpython/rev/c2f1bb56760d
msg203120 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2013-11-17 06:03
Thanks Claudiu! Additional tweaks in the committed version: - added a paragraph to the docs about the with statement support and moved the versionchanged note there - changed the example in the docs to use a with statement - added a basic test that the dumbdbm impl was actually closed, with a comment referencing issue 19385 (since that isn't subject to the beta 1 deadline)
msg203121 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2013-11-17 06:04
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.
msg203123 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2013-11-17 07:34
Cool, thanks!
msg212941 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-03-08 17:54
New changeset 200207e50cbf by R David Murray in branch 'default': whatsnew: dbm.open is context manager. (#19282) http://hg.python.org/cpython/rev/200207e50cbf
History
Date User Action Args
2022-04-11 14:57:52 admin set github: 63481
2014-03-08 17:54:18 python-dev set messages: +
2013-11-17 07:34:11 Claudiu.Popa set messages: +
2013-11-17 06:04:57 ncoghlan set messages: +
2013-11-17 06:03:22 ncoghlan set messages: +
2013-11-17 06:00:20 python-dev set status: open -> closednosy: + python-devmessages: + resolution: fixedstage: patch review -> resolved
2013-10-31 14:33:54 ncoghlan set messages: +
2013-10-28 14:47:06 Claudiu.Popa set messages: +
2013-10-21 04:57:45 rhettinger set assignee: ncoghlanmessages: + nosy: + ncoghlan, rhettinger
2013-10-19 06:29:34 Claudiu.Popa set files: + dbm1.patchmessages: +
2013-10-19 00:16:07 terry.reedy set nosy: + terry.reedytitle: dbm is not a context manager -> dbm.open should be a context managermessages: + stage: needs patch -> patch review
2013-10-18 20:40:43 Arfrever set nosy: + Arfrever
2013-10-18 10:45:46 Claudiu.Popa set files: + dbm.patchkeywords: + patchmessages: +
2013-10-18 09:28:24 serhiy.storchaka set stage: needs patchtype: enhancementversions: - Python 3.1, Python 3.2, Python 3.3, Python 3.5
2013-10-18 09:10:13 Claudiu.Popa set nosy: + Claudiu.Popamessages: +
2013-10-18 07:45:10 kousu create