Skip to content

Preserve return values from generator callers - #191

Open
xiehuanyi wants to merge 1 commit into
micheles:masterfrom
xiehuanyi:fix/generator-return-values
Open

xiehuanyi wants to merge 1 commit into
micheles:masterfrom
xiehuanyi:fix/generator-return-values

Conversation

@xiehuanyi

Copy link
Copy Markdown

Generator callers currently lose their return value in decorate(). An identity caller using return (yield from func(...)) yields the wrapped generator's items correctly, but exhausting the decorated generator raises StopIteration(None) even when the caller returns 42.

Return the result of the wrapper's yield from expression so generator composition preserves that value as well as yielded items.

Validation: the new regression observes the yielded item and StopIteration.value, and fails against the unmodified source. All 29 tests, including documentation examples, pass on CPython 3.12. git diff --check passes.

Prepared with OpenAI Codex assistance; no manual human testing is claimed.

Copilot AI balanced review requested due to automatic review settings October 11, 2026 12:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@carlitogwapito

Copy link
Copy Markdown

Here's a small test-only follow-up for completion through throw().

The test sends a value into the decorated generator, then throws a ValueError that the generator handles by returning a sentinel. It checks the returned object's identity, both finally blocks, and that the generator frame closes.

On Windows / CPython 3.14.4, it passes at aa9b563; the base loses the sentinel and returns None. The existing yielded-items control passes on both versions. These are focused checks, not a full-suite, documentation, or cross-version CI run.

Optional suggestion: keep this beside normal exhaustion, since yielded items can look right while a handled-error completion still loses its result.

@xiehuanyi

Copy link
Copy Markdown
Author

Thanks for checking completion through throw() and sharing the test. The returned sentinel and both finally blocks cover a useful path beyond normal exhaustion. I have reviewed the proposed test, but have not applied the optional commit or run it locally.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants