[Python-Dev] PEP 343 and __with__

Guido van Rossum guido at python.org
Tue Oct 4 16:54:15 CEST 2005

On 10/4/05, Jason Orendorff <jason.orendorff at gmail.com> wrote:
> This:
>     with EXPR as VAR:
>         BLOCK
> expands to this under PEP 342:
>     _cm = contextmanager(EXPR)
>     VAR = _cm.next()
>     try:
>         BLOCK
>     except:
>         try:
>             _cm.throw(*sys.exc_info())
>         except:
>             pass
>         raise
>     finally:
>         try:
>             _cm.next()
>         except StopIteration:
>             pass
>         except:
>             raise
>         else:
>             raise RuntimeError

Where in the world do you get this idea? The translation is as
follows, according to PEP 343:

        abc = EXPR
        exc = (None, None, None)
        VAR = abc.__enter__()
                exc = sys.exc_info()

PEP 342 doesn't touch on the expansion of with-statements at all.

I think I know where you're coming from, but please do us a favor and
don't misrepresent the PEPs.  If anything, your proposal is more
complicated; it requires four new APIs instead of two, and requires an
extra call to set up (__with__() followed by start()).

Proposals like yours (and every other permutation) were brought up
during the initial discussion. We picked one. Don't create more churn
by arguing for a different variant. Spend your efforts on implementing
it so you can actually use it and see how bad it is (I predict it
won't be bad at all).

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list