classification
Title: dbm.open should be a context manager
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ncoghlan Nosy List: Arfrever, Claudiu.Popa, kousu, ncoghlan, python-dev, rhettinger, terry.reedy
Priority: normal Keywords: patch

Created on 2013-10-18 07:45 by kousu, last changed 2014-03-08 17:54 by python-dev. 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: Claudiu Popa (Claudiu.Popa) * Date: 2013-10-18 09:10
Working on a patch for this.
msg200241 - (view) Author: Claudiu Popa (Claudiu.Popa) * 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(<NotOpenError>):
            db['a'] = 'a'  # or whatever

I haven't looked at the C code.
msg200376 - (view) Author: Claudiu Popa (Claudiu.Popa) * 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 "<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
>>>
msg200697 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-10-21 04:57
This seems like a reasonable request.
msg201545 - (view) Author: Claudiu Popa (Claudiu.Popa) * Date: 2013-10-28 14:47
There is issue19385 for the inconsistency of dbm.dumb, with a patch.
msg201818 - (view) Author: Nick 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) 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: Nick 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: Nick 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: Claudiu Popa (Claudiu.Popa) * Date: 2013-11-17 07:34
Cool, thanks!
msg212941 - (view) Author: Roundup Robot (python-dev) 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
2014-03-08 17:54:18python-devsetmessages: + msg212941
2013-11-17 07:34:11Claudiu.Popasetmessages: + msg203123
2013-11-17 06:04:57ncoghlansetmessages: + msg203121
2013-11-17 06:03:22ncoghlansetmessages: + msg203120
2013-11-17 06:00:20python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg203119

resolution: fixed
stage: patch review -> resolved
2013-10-31 14:33:54ncoghlansetmessages: + msg201818
2013-10-28 14:47:06Claudiu.Popasetmessages: + msg201545
2013-10-21 04:57:45rhettingersetassignee: ncoghlan

messages: + msg200697
nosy: + ncoghlan, rhettinger
2013-10-19 06:29:34Claudiu.Popasetfiles: + dbm1.patch

messages: + msg200376
2013-10-19 00:16:07terry.reedysetnosy: + terry.reedy
title: dbm is not a context manager -> dbm.open should be a context manager
messages: + msg200336

stage: needs patch -> patch review
2013-10-18 20:40:43Arfreversetnosy: + Arfrever
2013-10-18 10:45:46Claudiu.Popasetfiles: + dbm.patch
keywords: + patch
messages: + msg200241
2013-10-18 09:28:24serhiy.storchakasetstage: needs patch
type: enhancement
versions: - Python 3.1, Python 3.2, Python 3.3, Python 3.5
2013-10-18 09:10:13Claudiu.Popasetnosy: + Claudiu.Popa
messages: + msg200232
2013-10-18 07:45:10kousucreate