diff --git a/pep-0343.txt b/pep-0343.txt index 90da76dd2..4ae939de1 100644 --- a/pep-0343.txt +++ b/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: