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

GzipFile and BZ2File should support context manager protocol #48110

Closed
hagen mannequin opened this issue Sep 13, 2008 · 8 comments
Closed

GzipFile and BZ2File should support context manager protocol #48110

hagen mannequin opened this issue Sep 13, 2008 · 8 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@hagen
Copy link
Mannequin

hagen mannequin commented Sep 13, 2008

BPO 3860
Nosy @pitrou, @vstinner, @benjaminp
Files
  • withgzip.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 = None
    closed_at = <Date 2009-01-10.16:23:27.442>
    created_at = <Date 2008-09-13.19:29:02.127>
    labels = ['type-feature', 'library']
    title = 'GzipFile and BZ2File should support context manager protocol'
    updated_at = <Date 2011-02-12.19:32:25.462>
    user = 'https://bugs.python.org/hagen'

    bugs.python.org fields:

    activity = <Date 2011-02-12.19:32:25.462>
    actor = 'teamnoir'
    assignee = 'none'
    closed = True
    closed_date = <Date 2009-01-10.16:23:27.442>
    closer = 'pitrou'
    components = ['Library (Lib)']
    creation = <Date 2008-09-13.19:29:02.127>
    creator = 'hagen'
    dependencies = []
    files = ['12398']
    hgrepos = []
    issue_num = 3860
    keywords = ['patch']
    message_count = 8.0
    messages = ['73194', '78035', '78037', '78046', '78319', '78324', '79551', '128458']
    nosy_count = 5.0
    nosy_names = ['pitrou', 'vstinner', 'benjamin.peterson', 'hagen', 'teamnoir']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'patch review'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue3860'
    versions = ['Python 3.1', 'Python 2.7']

    @hagen
    Copy link
    Mannequin Author

    hagen mannequin commented Sep 13, 2008

    When you've become used to writing

    with open("xzy", "w") as f: 

    it's strange when it doesn't work for gzip.open or bz2.BZ2File.
    Or is there a reason for them not being context managers?

    @hagen hagen mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Sep 13, 2008
    @pitrou
    Copy link
    Member

    pitrou commented Dec 18, 2008

    Here is a patch for the trunk. I'm waiting for the opinion other
    developers before deciding to commit or no.

    @benjaminp
    Copy link
    Contributor

    Here's the StringIO discussion: bpo-1286

    @pitrou
    Copy link
    Member

    pitrou commented Dec 18, 2008

    Thanks. The outcome of the StringIO discussion isn't clearly negative,
    although Guido does not see a big benefit in it. If some people are
    opposed to this, please stand up :) Otherwise I'll commit the patch to
    trunk.

    @vstinner
    Copy link
    Member

    I love "with ... as f:" and I want it for gzip/bz2 files!

    Comment about the patch:

    (1) the IOError message is different for the two modules:
    "I/O operation on closed GzipFile object"
    vs
    "I/O operation on closed file"

    Specify the object name in both messages or remove it from both
    messages. I prefer verbose error message ("I/O operation on closed
    BZ2File object").

    (2) For bz2module, Why don't you call BZ2File_close() directly instead
    of the generic version PyObject_CallMethod(... "close" ...)?

    @pitrou
    Copy link
    Member

    pitrou commented Dec 27, 2008

    (1) the IOError message is different for the two modules:
    "I/O operation on closed GzipFile object"
    vs
    "I/O operation on closed file"

    I've reused the same error message as used in other parts of each
    module.

    (2) For bz2module, Why don't you call BZ2File_close() directly instead
    of the generic version PyObject_CallMethod(... "close" ...)?

    Because someone may subclass BZ2File and override the close() method. I
    don't know if it's likely to happen, though.

    @pitrou
    Copy link
    Member

    pitrou commented Jan 10, 2009

    Committed in trunk and py3k. Thanks!

    @pitrou pitrou closed this as completed Jan 10, 2009
    @teamnoir
    Copy link
    Mannequin

    teamnoir mannequin commented Feb 12, 2011

    Documentation needs to be updated to state that these are now context managers. This is important since they aren't in python-2.x.

    I'm not sure whether this should be added to the "new in python" blurbs.

    @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
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants