python/nondist/peps pep-0343.txt,1.17,1.18

Update of /cvsroot/python/python/nondist/peps In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5610 Modified Files: pep-0343.txt Log Message: Address most of Phillip Eby's comments. Index: pep-0343.txt =================================================================== RCS file: /cvsroot/python/python/nondist/peps/pep-0343.txt,v retrieving revision 1.17 retrieving revision 1.18 diff -u -d -r1.17 -r1.18 --- pep-0343.txt 1 Jun 2005 15:13:37 -0000 1.17 +++ pep-0343.txt 1 Jun 2005 16:45:25 -0000 1.18 @@ -282,6 +282,8 @@ 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 @@ @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 @@ 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:
participants (1)
-
gvanrossumļ¼ users.sourceforge.net