« Return to Thread: try-except in generator methods

Re: try-except in generator methods

by Avishay Lavie :: Rate this Message:

Reply to Author | View in Thread


How do you know if the "enumeration was aborted"? The calling code
gets back an IEnumerator and is free to call or not call MoveNext on
it any number of times. Suppose I do this:

def Generate():
  try:
    yield 42
    yield 4242
    yield 424242
  ensure:
    Dispose()

def Consume():
  for index, value in zip(range(2), Generate()):
    print "${index} => ${value}"

This will only consume only the first two yields (or even just the
first one, I'm never sure about range()...). When, if at all, will the
internal Dispose() code be called?

On Jan 22, 10:15 pm, Daniel Grunwald <dan...@...> wrote:

> Remember the cases in which the "ensure" code should run:
> 1) when control flow in the generator normally leaves the "try" block
> (but not when control flow leaves the "try" block due to "yield" being
> implemented as "return Yield(...)")
> 2) when the generator code causes an exception in the "try" block
> 3) when the enumeration is aborted (due to "break" or exception in the
> for loop) - in this case, Dispose() is called on the enumerator.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Boo Programming Language" group.
To post to this group, send email to boolang@...
To unsubscribe from this group, send email to boolang-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/boolang
-~----------~----~----~----~------~----~------~--~---

 « Return to Thread: try-except in generator methods