outsmarting context managers with coroutines

B

Burak Arslan

Hi,

Have a look at the following code snippets:
https://gist.github.com/plq/8164035

Observations:

output2: I can break out of outer context without closing the inner one
in Python 2
output3: Breaking out of outer context closes the inner one, but the
closing order is wrong.
output3-yf: With yield from, the closing order is fine but yield returns
None before throwing.

All of the above seem buggy in their own way. And actually, Python 2
seems to leak memory when generators and context managers are used this way.

Are these behaviours intentional? How much of it is
implementation-dependent? Are they documented somewhere? Neither PEP-342
nor PEP-380 talk about context managers and PEP-343 talks about
generators but not coroutines.

My humble opinion:

1) All three should behave in the exact same way.
2) Throwing into a generator should not yield None before throwing.

Best,
Burak

ps: I have:

$ python -V; python3 -V
Python 2.7.5
Python 3.3.2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top