473,395 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

contextlib.closing annoyance

it looks like contextlib.closing fails to be idempotent,
i.e. wrapping closing() around another closing() doesn't work.

This is annoying because the idea of closing() is to let you
use legacy file-like objects as targets of the "with" statement,
e.g.

with closing(gzip.open(filename)) as zf: ...

but what happens if the gzip library gets updated the dumb way to
support the enter and exit methods so you don't need the explicit
closing call any more? The dumb way of course is to just call
closing() inside the library. It seems to me that
closing(closing(f)) ought to do the same thing as closing(f).

Let me know if I'm overlooking something. I'm thinking of submitting
an RFE.
Jun 22 '07 #1
1 1661
On Jun 22, 4:54 pm, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
it looks like contextlib.closing fails to be idempotent,
i.e. wrapping closing() around another closing() doesn't work.
This is annoying because the idea of closing() is to let you
use legacy file-like objects as targets of the "with" statement,
e.g.

with closing(gzip.open(filename)) as zf: ...

but what happens if the gzip library gets updated the dumb way to
support the enter and exit methods so you don't need the explicit
closing call any more? The dumb way of course is to just call
closing() inside the library. It seems to me that
closing(closing(f)) ought to do the same thing as closing(f).

Let me know if I'm overlooking something. I'm thinking of submitting
an RFE.
I'm not sure what "calling closing() inside the library" entails. In
the __enter__ method? I don't see how that could work. Nor anywhere
else, really: an object does not have the ability to wrap itself in a
context manager (without explicitly emulating the functionality by
calling the __-methods). Indeed, why wouldn't this be the shortest
(and dumbest) implementation?

class gzip.File:
def __enter__(self):
return self
def __exit__(self, *args):
self.close()

-Mike

Jun 25 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ron L | last post by:
I have an MDI application with a number of child windows. In each child window I am catching the Closing event and having the child window decide if it should set cancel to true. The intent here...
4
by: bbass | last post by:
thanks to all that replyied to my previous post with the following code in question: <a href="merc.htm" target="_new_merc" onfocusout=window.close class="left_link"> i understand that the...
6
by: Al the programmer | last post by:
I want to catch the Closing event for my form. I created a test windows app using the wizard. I then create the Closing event by clicking the lightning bolt on the properties pane. The code is...
1
by: **Developer** | last post by:
When I get a closing event in a MID Child form I don't know if the child form is closing or the main form is closing. Is there a way to tell? Thank
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
8
by: ds_lewis | last post by:
Is there a way to tell when the user closes the browser that doesn't also happen when a page is merely being refreshed? Thanks, Targus
4
by: Academic | last post by:
Does it make sense to put this If e.Cancel Then Exit Sub at the beginning of form closing events so if the user cancels the app's exiting in one Closing routine he will not be asked again by...
7
by: brasse | last post by:
Hello! I have been running in to some problems when using contextlib.nested(). My problem arises when using code similar to this: from __future__ import with_statement from contextlib...
1
by: Neal Becker | last post by:
http://www.python.org/doc/2.5.2/lib/module-contextlib.html has this example: from contextlib import contextmanager @contextmanager def tag(name): print "<%s>" % name yield print "</%s>" %...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.