[Python-checkins] python/nondist/peps pep-0343.txt,1.20,1.21
gvanrossum@users.sourceforge.net
gvanrossum at users.sourceforge.net
Thu Jun 2 17:13:57 CEST 2005
Update of /cvsroot/python/python/nondist/peps
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22249
Modified Files:
pep-0343.txt
Log Message:
Include suggestions from Nick Coghlan.
Index: pep-0343.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/peps/pep-0343.txt,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -d -r1.20 -r1.21
--- pep-0343.txt 2 Jun 2005 14:56:36 -0000 1.20
+++ pep-0343.txt 2 Jun 2005 15:13:55 -0000 1.21
@@ -413,7 +413,9 @@
which does not do what one might think (f is closed before BLOCK2
is entered).
- OTOH such mistakes are easily diagnosed.
+ OTOH such mistakes are easily diagnosed; for example, the
+ with_template decorator above raises RuntimeError when the second
+ with-statement calls f.__enter__() again.
Open Issues
@@ -642,6 +644,29 @@
methods to the decimal.Context class so that this example can
be simplified to "with decimal.getcontext() as ctx: ...".)
+ 10. A generic "object-closing" template:
+
+ @with_template
+ def closing(obj):
+ try:
+ yield obj
+ finally:
+ obj.close()
+
+ This can be used to deterministically close anything with a
+ close method, be it file, generator, or something else:
+
+ # emulate opening():
+ with closing(open("argument.txt")) as contradiction:
+ for line in contradiction:
+ print line
+
+ # deterministically finalize a generator:
+ with closing(some_gen()) as data:
+ for datum in data:
+ process(datum)
+
+
References
[1] http://blogs.msdn.com/oldnewthing/archive/2005/01/06/347666.aspx
More information about the Python-checkins
mailing list