diff --git a/src/embed_tests/TestPyObject.cs b/src/embed_tests/TestPyObject.cs index d4952d4a3..d0d8eab45 100644 --- a/src/embed_tests/TestPyObject.cs +++ b/src/embed_tests/TestPyObject.cs @@ -59,9 +59,17 @@ def add(self, x, y): } [Test] - public void InvokeNull() { + public void InvokeNull() + { var list = PythonEngine.Eval("list"); Assert.Throws(() => list.Invoke(new PyObject[] {null})); } + + [Test] + public void AsManagedObjectInvalidCast() + { + var list = PythonEngine.Eval("list"); + Assert.Throws(() => list.AsManagedObject(typeof(int))); + } } } diff --git a/src/runtime/delegatemanager.cs b/src/runtime/delegatemanager.cs index a8ab6d2b5..3e6541c44 100644 --- a/src/runtime/delegatemanager.cs +++ b/src/runtime/delegatemanager.cs @@ -221,19 +221,17 @@ public void Dispose() public object Dispatch(ArrayList args) { IntPtr gs = PythonEngine.AcquireLock(); - object ob = null; + object ob; try { ob = TrueDispatch(args); } - catch (Exception e) + finally { PythonEngine.ReleaseLock(gs); - throw e; } - PythonEngine.ReleaseLock(gs); return ob; } @@ -266,27 +264,15 @@ public object TrueDispatch(ArrayList args) return null; } - object result = null; - if (!Converter.ToManaged(op, rtype, out result, false)) + object result; + if (!Converter.ToManaged(op, rtype, out result, true)) { Runtime.XDecref(op); - throw new ConversionException($"could not convert Python result to {rtype}"); + throw new PythonException(); } Runtime.XDecref(op); return result; } } - - - public class ConversionException : Exception - { - public ConversionException() - { - } - - public ConversionException(string msg) : base(msg) - { - } - } } diff --git a/src/runtime/pyobject.cs b/src/runtime/pyobject.cs index 524d6e4b6..d68a9905b 100644 --- a/src/runtime/pyobject.cs +++ b/src/runtime/pyobject.cs @@ -134,9 +134,9 @@ public static PyObject FromManagedObject(object ob) public object AsManagedObject(Type t) { object result; - if (!Converter.ToManaged(obj, t, out result, false)) + if (!Converter.ToManaged(obj, t, out result, true)) { - throw new InvalidCastException("cannot convert object to target type"); + throw new InvalidCastException("cannot convert object to target type", new PythonException()); } return result; } @@ -154,12 +154,7 @@ public T As() { return (T)(this as object); } - object result; - if (!Converter.ToManaged(obj, typeof(T), out result, false)) - { - throw new InvalidCastException("cannot convert object to target type"); - } - return (T)result; + return (T)AsManagedObject(typeof(T)); } diff --git a/src/tests/test_delegate.py b/src/tests/test_delegate.py index 1bfc4e903..909fd0f05 100644 --- a/src/tests/test_delegate.py +++ b/src/tests/test_delegate.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- -# TODO: Add test for ObjectDelegate """Test CLR delegate support.""" @@ -257,6 +256,26 @@ def always_so_negative(): assert not d() assert not ob.CallBoolDelegate(d) +def test_object_delegate(): + """Test object delegate.""" + from Python.Test import ObjectDelegate + + def create_object(): + return DelegateTest() + + d = ObjectDelegate(create_object) + ob = DelegateTest() + ob.CallObjectDelegate(d) + +def test_invalid_object_delegate(): + """Test invalid object delegate with mismatched return type.""" + from Python.Test import ObjectDelegate + + d = ObjectDelegate(hello_func) + ob = DelegateTest() + with pytest.raises(TypeError): + ob.CallObjectDelegate(d) + # test async delegates # test multicast delegates