[Python-ideas] PEP 380 close and contextmanagers? (original) (raw)
Ron Adam rrr at ronadam.com
Thu Oct 28 03:26:23 CEST 2010
- Previous message: [Python-ideas] PEP 380 close and contextmanagers?
- Next message: [Python-ideas] Possible PEP 380 tweak
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.catchwarnings 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.
Cheers, Ron
- Previous message: [Python-ideas] PEP 380 close and contextmanagers?
- Next message: [Python-ideas] Possible PEP 380 tweak
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]