Address most of Phillip Eby's comments.

This commit is contained in:
Guido van Rossum 2005-06-01 16:45:25 +00:00
parent 76de894c1a
commit c76b8b40fc
1 changed files with 16 additions and 15 deletions

View File

@ -282,6 +282,8 @@ Specification: Generator Enhancements
raise TypeError("generator ignored GeneratorExit")
# Other exceptions are not caught
(XXX is TypeError an acceptable exception here?)
New generator method: __del__()
g.__del__() is an alias for g.close(). This will be called when
@ -350,8 +352,10 @@ Generator Decorator
@with_template
def opening(filename):
f = open(filename) # IOError here is untouched by Wrapper
yield f
f.close() # Ditto for errors here (however unlikely)
try:
yield f
finally:
f.close() # Ditto for errors here (however unlikely)
A robust implementation of this decorator should be made part of
the standard library, but not necessarily as a built-in function.
@ -427,20 +431,17 @@ Examples
print line.rstrip()
3. A template for committing or rolling back a database
transaction; this is written as a class rather than as a
decorator since it requires access to the exception
information:
transaction:
class transactional:
def __init__(self, db):
self.db = db
def __enter__(self):
self.db.begin()
def __exit__(self, type, value, tb):
if type is not None:
self.db.rollback()
else:
self.db.commit()
@with_template
def transactional(db):
db.begin()
try:
yield None
except:
db.rollback()
else:
db.commit()
4. Example 1 rewritten without a generator: