Issue 13896: Make shelf instances work with 'with' as context managers (original) (raw)

Created on 2012-01-28 11:12 by gruszczy, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
13896.patch gruszczy,2012-01-28 11:12 review
Messages (10)
msg152159 - (view) Author: Filip Gruszczyński (gruszczy) Date: 2012-01-28 11:12
It would be cool if use could use with to manage shelf object and forget about close.
msg152185 - (view) Author: Zachary Richey (zachary r.) Date: 2012-01-28 20:03
The docs for shelve(see: http://docs.python.org/dev/library/shelve.html#shelve.open) recommend using contextlib.closing for this purpose.
msg152236 - (view) Author: Filip Gruszczyński (gruszczy) Date: 2012-01-29 15:29
Oh, I haven't noticed that. Using contextlib.closing solves my problem. Thanks.
msg152575 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-02-04 01:40
contextlib.closing is an adapter for using objects that have a close method but have not be modernized to be context managers by having _enter__ and __exit__ methods added. Its doc gives the example with closing(urlopen('http://www.python.org')) as page: This is now obsolete as the object returned is now a context manager. A shelf instance would make a better example now, but... I think it reasonable to update shelve.Shelf also. I believe def __enter__(self): return self def __exit__(self, e_typ, e_val, tb): self.close() are all that are needed. (Nick, true?)
msg152604 - (view) Author: Filip Gruszczyński (gruszczy) Date: 2012-02-04 08:55
closing was solving my problem, but I'll be happy on working on this patch, if you think it's useful after all. I don't think the stage should be test needed, since the patch has tests (I know the drill here ;-)).
msg172122 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2012-10-05 19:39
Filip, please describe why you prevent closing if writeback attribute is not present. I see comment for __del__ related to #1339007, but at __exit__ call __enter__ has called already and __init__ has finished definitely with success. Also I would to see reference to http://bugs.python.org/issue1339007 in __del__ comment to cleaner description of issue.
msg172159 - (view) Author: Filip Gruszczyński (gruszczy) Date: 2012-10-06 01:04
Crap, it was so long ago, that I honestly don't remember why I added this if. It made sense back then.
msg172177 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-10-06 10:52
New changeset 3c1df1ede882 by Andrew Svetlov in branch 'default': Issue #13896: Make shelf instances work with 'with' as context managers. http://hg.python.org/cpython/rev/3c1df1ede882
msg172178 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2012-10-06 10:54
Committed. Thanks. Please fill Python Contributor Agreement: http://www.python.org/psf/contrib/
msg180702 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-01-26 19:20
The patch did not update the doc. See #17040, with proposed patch.
History
Date User Action Args
2022-04-11 14:57:26 admin set github: 58104
2013-01-26 19:20:15 terry.reedy set messages: +
2012-10-06 10:54:40 asvetlov set status: open -> closedresolution: fixedmessages: + stage: patch review -> resolved
2012-10-06 10:52:39 python-dev set nosy: + python-devmessages: +
2012-10-06 01:04:27 gruszczy set messages: +
2012-10-05 19:40:03 asvetlov set assignee: asvetlov
2012-10-05 19:39:09 asvetlov set messages: +
2012-09-26 10:58:43 asvetlov set nosy: + asvetlov
2012-09-10 21:14:53 berker.peksag set versions: + Python 3.4, - Python 3.3
2012-09-06 05:49:00 Miguel.Angel.García set nosy: + Miguel.Angel.García
2012-02-04 09:51:16 terry.reedy set stage: test needed -> patch review
2012-02-04 08:55:03 gruszczy set messages: +
2012-02-04 01:41:00 terry.reedy set versions: + Python 3.3type: enhancementnosy: + terry.reedy, ncoghlantitle: shelf doesn't work with 'with' -> Make shelf instances work with 'with' as context managersmessages: + stage: test needed
2012-01-29 15:29:01 gruszczy set messages: +
2012-01-28 20:03:26 zachary r. set nosy: + zachary r.messages: +
2012-01-28 11:12:56 gruszczy set files: + 13896.patchkeywords: + patch
2012-01-28 11:12:29 gruszczy create