[Python-Dev] Simpler finalization semantics (was Re: PEP 343 - Abstract Block Redux)
Guido van Rossum
gvanrossum at gmail.com
Tue May 17 16:17:44 CEST 2005
[Nick Coghlan (replying to Phillip)]
> Do you mean translating this:
>
> with EXPR1 as VAR1:
> BLOCK1
>
> To something along the lines of:
>
> the_stmt = EXPR1
> stmt_enter = getattr(the_stmt, "__enter__", None)
> stmt_exit = getattr(the_stmt, "__exit__", None)
>
> if stmt_enter is None:
> VAR1 = the_stmt
> else:
> VAR1 = stmt_enter()
>
> if stmt_exit is None:
> BLOCK1
> else:
> exc = (None, None, None)
> try:
> try:
> BLOCK1
> except:
> exc = sys.exc_info()
> raise
> finally:
> stmt_exit(*exc)
-1.
The compiler must generate both code paths but one is wasted.
I know this is not a very strong argument, but my gut tells me this
generalization of the with-statement is wrong, so I'll stick to it
regardless of the strength of the argument. The real reason will come
to me.
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev
mailing list