Closed atifaziz closed 1 month ago
Please can you extend this test so that it verifies that the generator returned True
, or change the generator to return something more exotic incase we accidentally return a truthy value anyway
https://github.com/tonybaloney/CSnakes/blob/main/src/Integration.Tests/GeneratorTests.cs#L14
Please can you extend this test so that it verifies that the generator returned
True
, …
I'm not sure what's missing since the test was already extended.
Looks good to me pending @tonybaloney's desires on testing
Sorry I didn't see the additional test change. LGTM
This PR completes the implementation of generator support by adding and testing the ability to retrieve the return value from a generator function.
Summary of changes:
Return
is added toIGeneratorIterator<,,>
that behaves likeIEnumerator<>.Current
.Send
ofIGeneratorIterator<,,>
is changed to match semantics ofMoveNext
. As an aside, I would even propose to renameSend
toMoveNext
on the C# side since overloading the same name is more natural given thatnext
in the Python is the same assend(None)
.Send
implementation ofGeneratorIterator<,,>
is changed to:MoveNext
. The latter would returnfalse
whereas the former would throwArgumentOutOfRangeException
(with the wrong parameter for message) when the generator was exhausted.StopIteration.value
.MoveNext
implementation ofGeneratorIterator<,,>
is changed to simply delegate to the internalSend
implementation withPyObject.None
for value. This matches the Python semantics:PythonStopIterationException
(that subclassesPythonRuntimeException
) is introduced and which captures thevalue
ofStopIteration
when it is raised so that it can be retrieved later. I don't think this is a great design on the .NET side, but this can be revisited later since the project is still a prototype. For now, it helps to complete generator support. Unfortunately, the implementation is also complicated by what I think is a regression inPyErr_Fetch
that's been introduced with 3.12 (see comment in code).