Address most of Phillip Eby's comments.
This commit is contained in:
parent
76de894c1a
commit
c76b8b40fc
31
pep-0343.txt
31
pep-0343.txt
|
@ -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:
|
||||
|
||||
|
|
Loading…
Reference in New Issue