[Steven Bethard] > So I didn't see any examples that really needed TerminateBlock to > suppress an exception. If the semantics of a do-statement was simply: > > stmt = EXPR1 > try: > stmt_enter = stmt.__enter__ > stmt_exit = stmt.__exit__ > except AttributeError: > raise TypeError("User defined statement template required") > > VAR1 = stmt_enter() # Omit 'VAR1 =' if no 'as' clause > exc = () > try: > try: > BLOCK1 > except: > exc = sys.exc_info() > finally: > stmt_exit(*exc) > > would this make any of the examples impossible to write? All you have > to do to suppress an exception is to not reraise it in __exit__.
But this use case would contain a trap for the unwary user who is writing an __exit__ method -- they have to remember to re-raise an exception if it was passed in, but that's easy to forget (and slightly tricky since you have to check the arg count or whether the first argument is not None). Going for all-out simplicity, I would like to be able to write these examples: class locking: def __init__(self, lock): self.lock = lock def __enter__(self): self.lock.acquire() def __exit__(self, *args): self.lock.release() class opening: def __init__(self, filename): self.filename = filename def __enter__(self): self.f = open(self.filename); return self.f def __exit__(self, *args): self.f.close()\ And do EXPR as VAR: BLOCK would mentally be translated into itr = EXPR VAR = itr.__enter__() try: BLOCK finally: itr.__exit__(*sys.exc_info()) # Except sys.exc_info() isn't defined by finally -- --Guido van Rossum (home page: http://www.python.org/~guido/) _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com