Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Catching exceptions from iterator MoveNext() (related to #475) #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
- Added `clr.GetClrType` (#432, #433)
- Allowed passing `None` for nullable args (#460)
- Added keyword arguments based on C# syntax for calling CPython methods (#461)
- Catches exceptions thrown in C# iterators (yield returns) and rethrows them in python (#475)
- Implemented GetDynamicMemberNames() for PyObject to allow dynamic object members to be visible in the debugger (#443)

### Changed
Expand Down
16 changes: 14 additions & 2 deletions src/runtime/iterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,21 @@ public Iterator(IEnumerator e)
public static IntPtr tp_iternext(IntPtr ob)
{
var self = GetManagedObject(ob) as Iterator;
if (!self.iter.MoveNext())
try
{
Exceptions.SetError(Exceptions.StopIteration, Runtime.PyNone);
if (!self.iter.MoveNext())
{
Exceptions.SetError(Exceptions.StopIteration, Runtime.PyNone);
return IntPtr.Zero;
}
}
catch (Exception e)
{
if (e.InnerException != null)
{
e = e.InnerException;
}
Exceptions.SetError(e);
return IntPtr.Zero;
}
object item = self.iter.Current;
Expand Down
9 changes: 9 additions & 0 deletions src/testing/exceptiontest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;

namespace Python.Test
{
Expand Down Expand Up @@ -54,6 +56,13 @@ public static bool ThrowException()
throw new OverflowException("error");
}

public static IEnumerable<int> ThrowExceptionInIterator(Exception e)
{
yield return 1;
yield return 2;
throw e;
}

public static void ThrowChainedExceptions()
{
try
Expand Down
32 changes: 32 additions & 0 deletions src/tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,35 @@ def test_chained_exceptions():
assert exc.Message == msg
assert exc.__cause__ == exc.InnerException
exc = exc.__cause__

def test_iteration_exception():
from Python.Test import ExceptionTest
from System import OverflowException

exception = OverflowException("error")

val = ExceptionTest.ThrowExceptionInIterator(exception).__iter__()
assert next(val) == 1
assert next(val) == 2
with pytest.raises(OverflowException) as cm:
next(val)

exc = cm.value

assert exc == exception

def test_iteration_innerexception():
from Python.Test import ExceptionTest
from System import OverflowException

exception = System.Exception("message", OverflowException("error"))

val = ExceptionTest.ThrowExceptionInIterator(exception).__iter__()
assert next(val) == 1
assert next(val) == 2
with pytest.raises(OverflowException) as cm:
next(val)

exc = cm.value

assert exc == exception.InnerException