Re: Distinguishing active generators from exhausted ones

2009-07-27 Thread Terry Reedy
Steven D'Aprano wrote: On Sun, 26 Jul 2009 20:10:00 -0400, Terry Reedy wrote: Michal Kwiatkowski wrote: The thing is I don't need the next item. I need to know if the generator has stopped without invoking it. Write a one-ahead iterator class, which I have posted before, that sets

Re: Distinguishing active generators from exhausted ones

2009-07-27 Thread Steven D'Aprano
On Mon, 27 Jul 2009 02:02:19 -0400, Terry Reedy wrote: Steven D'Aprano wrote: On Sun, 26 Jul 2009 20:10:00 -0400, Terry Reedy wrote: Michal Kwiatkowski wrote: The thing is I don't need the next item. I need to know if the generator has stopped without invoking it. Write a one-ahead

Re: Distinguishing active generators from exhausted ones

2009-07-27 Thread Michal Kwiatkowski
On Jul 27, 1:56 am, a...@pythoncraft.com (Aahz) wrote: Upon a cursory look, after a generator 'gen' is exhausted (meaning gen.next() has raised StopIteration), it seems that gen.gi_frame will be None. Only in Python 2.5 or higher though. I need to support Python 2.3 and 2.4 as well, sorry

Re: Distinguishing active generators from exhausted ones

2009-07-27 Thread Aahz
In article 1c8ae01e-2e9c-497c-9f8d-408f56f9c...@g31g2000yqc.googlegroups.com, Michal Kwiatkowski constant.b...@gmail.com wrote: On Jul 27, 1:56 am, a...@pythoncraft.com (Aahz) wrote: Upon a cursory look, after a generator 'gen' is exhausted (meaning gen.next() has raised StopIteration), it

Re: Distinguishing active generators from exhausted ones

2009-07-27 Thread Terry Reedy
Steven D'Aprano wrote: On Mon, 27 Jul 2009 02:02:19 -0400, Terry Reedy wrote: Steven D'Aprano wrote: On Sun, 26 Jul 2009 20:10:00 -0400, Terry Reedy wrote: Michal Kwiatkowski wrote: The thing is I don't need the next item. I need to know if the generator has stopped without invoking it.

Re: Distinguishing active generators from exhausted ones

2009-07-27 Thread Michal Kwiatkowski
On Jul 27, 10:47 pm, Terry Reedy tjre...@udel.edu wrote: There are two possible definition of 'exhausted': 1) will raise StopIteration on the next next() call; 2) has raised StopIteration at least once. The wrapper converts 2) to 1), which is to say, it obeys definition 1 once the underlying

Re: Distinguishing active generators from exhausted ones

2009-07-26 Thread Hendrik van Rooyen
On Saturday 25 July 2009 20:30:54 Michal Kwiatkowski wrote: Hi, Is there a way to tell if a generator has been exhausted using pure Python code? I've looked at CPython sources and it seems that something like active/exhausted attribute on genobject is missing from the API. For the time being

Re: Distinguishing active generators from exhausted ones

2009-07-26 Thread Michal Kwiatkowski
On Jul 26, 1:10 am, Ben Finney ben+pyt...@benfinney.id.au wrote: Michal Kwiatkowski constant.b...@gmail.com writes: I may be missing something obvious here. Is there a better way to tell if a given generator object is still active or not?     foo = the_generator_object     try:        

Re: Distinguishing active generators from exhausted ones

2009-07-26 Thread Aahz
In article 2a408da6-af57-45d0-a75f-4cbe384bb...@s15g2000yqs.googlegroups.com, Michal Kwiatkowski constant.b...@gmail.com wrote: On Jul 25, 10:00=A0pm, Jason Tackaberry t...@urandom.ca wrote: On Sat, 2009-07-25 at 11:30 -0700, Michal Kwiatkowski wrote: Is there a way to tell if a generator has

Re: Distinguishing active generators from exhausted ones

2009-07-26 Thread Terry Reedy
Michal Kwiatkowski wrote: The thing is I don't need the next item. I need to know if the generator has stopped without invoking it. Write a one-ahead iterator class, which I have posted before, that sets .exhausted to True when next fails. tjr --

Re: Distinguishing active generators from exhausted ones

2009-07-26 Thread greg
Michal Kwiatkowski wrote: The first generator isn't finished, it yielded 1 and None. Second one is exhausted after yielding a single value (1). The problem is that, under Python 2.4 or 2.3 both invocations will generate the same trace output. This seems to be a deficiency in the trace

Re: Distinguishing active generators from exhausted ones

2009-07-26 Thread Steven D'Aprano
On Sun, 26 Jul 2009 20:10:00 -0400, Terry Reedy wrote: Michal Kwiatkowski wrote: The thing is I don't need the next item. I need to know if the generator has stopped without invoking it. Write a one-ahead iterator class, which I have posted before, that sets .exhausted to True when next

Distinguishing active generators from exhausted ones

2009-07-25 Thread Michal Kwiatkowski
Hi, Is there a way to tell if a generator has been exhausted using pure Python code? I've looked at CPython sources and it seems that something like active/exhausted attribute on genobject is missing from the API. For the time being I am using a simple C extension to look at f_stacktop pointer of

Re: Distinguishing active generators from exhausted ones

2009-07-25 Thread Jason Tackaberry
On Sat, 2009-07-25 at 11:30 -0700, Michal Kwiatkowski wrote: Is there a way to tell if a generator has been exhausted using pure Python code? I've looked at CPython sources and it seems that Upon a cursory look, after a generator 'gen' is exhausted (meaning gen.next() has raised StopIteration),

Re: Distinguishing active generators from exhausted ones

2009-07-25 Thread Michal Kwiatkowski
On Jul 25, 10:00 pm, Jason Tackaberry t...@urandom.ca wrote: On Sat, 2009-07-25 at 11:30 -0700, Michal Kwiatkowski wrote: Is there a way to tell if a generator has been exhausted using pure Python code? I've looked at CPython sources and it seems that Upon a cursory look, after a generator

Re: Distinguishing active generators from exhausted ones

2009-07-25 Thread Ben Finney
Michal Kwiatkowski constant.b...@gmail.com writes: I may be missing something obvious here. Is there a better way to tell if a given generator object is still active or not? foo = the_generator_object try: do_interesting_thing_that_needs(foo.next()) except StopIteration: