[Python-ideas] PEP 380 close and contextmanagers?

Ron Adam rrr at ronadam.com
Thu Oct 28 03:26:23 CEST 2010

On 10/27/2010 05:00 PM, Nick Coghlan wrote:
> On Thu, Oct 28, 2010 at 2:18 AM, Ron Adam<rrr at ronadam.com>  wrote:
>> It looks like No context managers return values in the finally or __exit__
>> part of a context manager.  Is there way to do that?
> The return value from __exit__ is used to decide whether or not to
> suppress the exception (i.e. bool(__exit__()) == True will suppress
> the exception that was passed in).
> There are a few CMs in the test suite (test.support) that provide info
> about things that happened during their with statement - they all use
> the trick of returning a stateful object from __enter__, then
> modifying the attributes of that object in __exit__. I seem to recall
> the CM variants of unittest.TestCase.assertRaises* doing the same
> thing (so you can poke and prod at the raised exception yourself).
> warnings.catch_warnings also appends encountered warnings to a list
> returned by __enter__ when record=True.
> Cheers,
> Nick.

Thanks, I'll take a look.  If for nothing else it will help me understand 
it better.

BTW, The use case of the (min/max) examples doesn't fit that particular 
need.  It turned out that just creating a custom exception and throwing it 
into the coroutine is probably the best and simplest way to do it.

That's not to say that some of the other things Guido is thinking of won't 
benefit close() returning a value, but that particular example doesn't.


More information about the Python-ideas mailing list