[Python-Dev] PEP 343 - Abstract Block Redux

Guido van Rossum gvanrossum at gmail.com
Sat May 14 05:58:12 CEST 2005


[Phillip J. Eby]
> May I suggest this alternative translation in the "Specification" section:
> 
>          abc = EXPR
>          __args = ()  # pseudo-variable, not visible to the user
> 
>          try:
>              VAR = abc.__enter__()
>              try:
>                  BLOCK
>              except:
>                  __args = sys.exc_info()
>          finally:
>              abc.__exit__(*__args)

Done (except you forgot to add a "raise" to the except claise).

> While slighly more complex than the current translation, the current
> translation seems a bit misleading to me.  OTOH, that may simply be because
> I see the *sys.exc_info() part and immediately wonder what happens when
> there was no exception, and sys.exc_info() contains some arbitrary previous
> data...

Right. Well, anyway, the actual implementation will just get the
exception info from the try/finally infrastructure -- it's squirreled
away somewhere on the stack even if sys.exc_info() (intentionally)
doesn't have access to it.

> Also, one question: will the "do protocol" be added to built-in "resource"
> types?  That is, locks, files, sockets, and so on?

One person proposed that and it was shot down by Greg Ewing. I think
it's better to require a separate wrapper.

> Or will there instead
> be "macros" like the "opening" and "locking" templates?  I notice that
> grammatically, "do gerund" works a lot better than "do noun", so all of
> your examples are words like locking, blocking, opening, redirecting, and
> so on.  This makes it seem awkward for e.g. "do self.__lock", which doesn't
> make any sense.  But the extra call needed to make it "do
> locking(self.__lock)" seems sort of gratuitous.

Maybe. There seems to be a surge of proponents for 'do' at the moment.

> It makes me wonder if "with" or "using" or some similar word that works
> better with nouns might be more appropriate, as then it would let us just
> add the resource protocol to common objects, and still read well.  For
> example, a Decimal Context object might implement __enter__ by setting
> itself as the thread-local context, and __exit__ by restoring the previous
> context.    "do aDecimalContext" doesn't make much sense, but "with
> aDecimalContext" or "using aDecimalContext" reads quite nicely.

Maybe. I think we ought to implement the basic mechanism first and
then decide how fancy we want to get, so I'd rather not get into this
in the PEP. I'll add this to the PEP.

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


More information about the Python-Dev mailing list