Skip to content

Fix "done" method of generators.#724

Open
ghuls wants to merge 1 commit intoexaloop:developfrom
ghuls:fix_iter_done
Open

Fix "done" method of generators.#724
ghuls wants to merge 1 commit intoexaloop:developfrom
ghuls:fix_iter_done

Conversation

@ghuls
Copy link
Copy Markdown
Contributor

@ghuls ghuls commented Dec 23, 2025

Fix "done" method of generators as before it would segfault in debug mode when getting the next element when the iterator was finished.

$ cat test_iter.codon
b = [1, 2, 3]

d = iter(b)
print(min(d))
print(d.done())
print(min(d))

$ /software/exaloop/codon/codon-0.19.4/bin/codon run test_iter.codon
1
Segmentation fault (core dumped)

$ /software/exaloop/codon/codon-0.19.4_done_fix/bin/codon run test_iter.codon
1
True
ValueError: min() arg is an empty sequence

Raised from: std.internal.builtin.min.0:0
/software/exaloop/codon/codon-0.19.4_pipe/lib/codon/stdlib/internal/builtin.codon:83:13

Backtrace:
  [0x7f9950a9986f] std.internal.builtin.min.0:0[Tuple[Generator[int]],Optional[NoneType],Optional[NoneType]].851 at /software/exaloop/codon/codon-0.19.4_pipe/lib/codon/stdlib/internal/builtin.codon:83:13
  [0x7f9950a9e8d2] main.0 at /software/exaloop/codon/test_iter.codon:6:7
Aborted (core dumped)

Fixes: #246

Fix "done" method of generators as before it would segfault in debug
mode when getting the next element when the iterator was finished.

    $ cat test_iter.codon
    b = [1, 2, 3]

    d = iter(b)
    print(min(d))
    print(d.done())
    print(min(d))

    $ /software/exaloop/codon/codon-0.19.4/bin/codon run test_iter.codon
    1
    Segmentation fault (core dumped)

    $ /software/exaloop/codon/codon-0.19.4_done_fix/bin/codon run test_iter.codon
    1
    True
    ValueError: min() arg is an empty sequence

    Raised from: std.internal.builtin.min.0:0
    /software/exaloop/codon/codon-0.19.4_pipe/lib/codon/stdlib/internal/builtin.codon:83:13

    Backtrace:
      [0x7f9950a9986f] std.internal.builtin.min.0:0[Tuple[Generator[int]],Optional[NoneType],Optional[NoneType]].851 at /software/exaloop/codon/codon-0.19.4_pipe/lib/codon/stdlib/internal/builtin.codon:83:13
      [0x7f9950a9e8d2] main.0 at /software/exaloop/codon/test_iter.codon:6:7
    Aborted (core dumped)

Fixes: exaloop#246
@ghuls
Copy link
Copy Markdown
Contributor Author

ghuls commented Mar 6, 2026

@arshajii Can you take a look?

@arshajii
Copy link
Copy Markdown
Contributor

arshajii commented Mar 9, 2026

Sorry for the delay on this; just wanted to do some testing especially regarding LLVM's coroutine optimizations. Will circle back shortly...

@arshajii
Copy link
Copy Markdown
Contributor

Quick follow-up here: this actually changes semantics e.g. in

def foo(n):
    for i in range(n):
        yield i

g = foo(3)
while not g.done():
    print(g.__next__())

Before:

0
1
2

After:

0
1
2
2

I'm thinking about actually getting rid of this .done() method and just using the simpler __done__() + __resume__() as needed, in order to resolve those linked issues instead...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Segmentation fault in invoking list(zip(,))

2 participants