471,624 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,624 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 1581
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Ron L | last post: by
6 posts views Thread by Al the programmer | last post: by
8 posts views Thread by ds_lewis | last post: by
7 posts views Thread by brasse | last post: by
1 post views Thread by Neal Becker | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

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.