Skip to content
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

Make shelf instances work with 'with' as context managers #58104

Closed
gruszczy mannequin opened this issue Jan 28, 2012 · 10 comments
Closed

Make shelf instances work with 'with' as context managers #58104

gruszczy mannequin opened this issue Jan 28, 2012 · 10 comments
Assignees
Labels
type-feature A feature request or enhancement

Comments

@gruszczy
Copy link
Mannequin

gruszczy mannequin commented Jan 28, 2012

BPO 13896
Nosy @terryjreedy, @ncoghlan, @asvetlov
Files
  • 13896.patch
  • 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:

    assignee = 'https://github.com/asvetlov'
    closed_at = <Date 2012-10-06.10:54:40.786>
    created_at = <Date 2012-01-28.11:12:29.545>
    labels = ['type-feature']
    title = "Make shelf instances work with 'with' as context managers"
    updated_at = <Date 2013-01-26.19:20:15.938>
    user = 'https://bugs.python.org/gruszczy'

    bugs.python.org fields:

    activity = <Date 2013-01-26.19:20:15.938>
    actor = 'terry.reedy'
    assignee = 'asvetlov'
    closed = True
    closed_date = <Date 2012-10-06.10:54:40.786>
    closer = 'asvetlov'
    components = []
    creation = <Date 2012-01-28.11:12:29.545>
    creator = 'gruszczy'
    dependencies = []
    files = ['24347']
    hgrepos = []
    issue_num = 13896
    keywords = ['patch']
    message_count = 10.0
    messages = ['152159', '152185', '152236', '152575', '152604', '172122', '172159', '172177', '172178', '180702']
    nosy_count = 7.0
    nosy_names = ['terry.reedy', 'ncoghlan', 'asvetlov', 'gruszczy', 'python-dev', 'zachary r.', 'Miguel.Angel.Garc\xc3\xada']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue13896'
    versions = ['Python 3.4']

    @gruszczy
    Copy link
    Mannequin Author

    gruszczy mannequin commented Jan 28, 2012

    It would be cool if use could use with to manage shelf object and forget about close.

    @zacharyr
    Copy link
    Mannequin

    zacharyr mannequin commented Jan 28, 2012

    The docs for shelve(see: http://docs.python.org/dev/library/shelve.html#shelve.open) recommend using contextlib.closing for this purpose.

    @gruszczy
    Copy link
    Mannequin Author

    gruszczy mannequin commented Jan 29, 2012

    Oh, I haven't noticed that. Using contextlib.closing solves my problem. Thanks.

    @terryjreedy
    Copy link
    Member

    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?)

    @terryjreedy terryjreedy changed the title shelf doesn't work with 'with' Make shelf instances work with 'with' as context managers Feb 4, 2012
    @terryjreedy terryjreedy added the type-feature A feature request or enhancement label Feb 4, 2012
    @gruszczy
    Copy link
    Mannequin Author

    gruszczy mannequin commented Feb 4, 2012

    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 ;-)).

    @asvetlov
    Copy link
    Contributor

    asvetlov commented Oct 5, 2012

    Filip, please describe why you prevent closing if writeback attribute is not present.
    I see comment for __del__ related to bpo-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.

    @asvetlov asvetlov self-assigned this Oct 5, 2012
    @gruszczy
    Copy link
    Mannequin Author

    gruszczy mannequin commented Oct 6, 2012

    Crap, it was so long ago, that I honestly don't remember why I added this if. It made sense back then.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Oct 6, 2012

    New changeset 3c1df1ede882 by Andrew Svetlov in branch 'default':
    Issue bpo-13896: Make shelf instances work with 'with' as context managers.
    http://hg.python.org/cpython/rev/3c1df1ede882

    @asvetlov
    Copy link
    Contributor

    asvetlov commented Oct 6, 2012

    Committed. Thanks.
    Please fill Python Contributor Agreement: http://www.python.org/psf/contrib/

    @asvetlov asvetlov closed this as completed Oct 6, 2012
    @terryjreedy
    Copy link
    Member

    The patch did not update the doc. See bpo-17040, with proposed patch.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants