This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Unsupported provider

classification
Title: Make shelf instances work with 'with' as context managers
Type: enhancement Stage: resolved
Components: Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: asvetlov Nosy List: Miguel.Angel.García, asvetlov, gruszczy, ncoghlan, python-dev, terry.reedy, zachary r.
Priority: normal Keywords: patch

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:26adminsetgithub: 58104
2013-01-26 19:20:15terry.reedysetmessages: + msg180702
2012-10-06 10:54:40asvetlovsetstatus: open -> closed
resolution: fixed
messages: + msg172178

stage: patch review -> resolved
2012-10-06 10:52:39python-devsetnosy: + python-dev
messages: + msg172177
2012-10-06 01:04:27gruszczysetmessages: + msg172159
2012-10-05 19:40:03asvetlovsetassignee: asvetlov
2012-10-05 19:39:09asvetlovsetmessages: + msg172122
2012-09-26 10:58:43asvetlovsetnosy: + asvetlov
2012-09-10 21:14:53berker.peksagsetversions: + Python 3.4, - Python 3.3
2012-09-06 05:49:00Miguel.Angel.Garcíasetnosy: + Miguel.Angel.García
2012-02-04 09:51:16terry.reedysetstage: test needed -> patch review
2012-02-04 08:55:03gruszczysetmessages: + msg152604
2012-02-04 01:41:00terry.reedysetversions: + Python 3.3
type: enhancement

nosy: + terry.reedy, ncoghlan
title: shelf doesn't work with 'with' -> Make shelf instances work with 'with' as context managers
messages: + msg152575
stage: test needed
2012-01-29 15:29:01gruszczysetmessages: + msg152236
2012-01-28 20:03:26zachary r.setnosy: + zachary r.
messages: + msg152185
2012-01-28 11:12:56gruszczysetfiles: + 13896.patch
keywords: + patch
2012-01-28 11:12:29gruszczycreate