Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Better error messages from PyObject.AsManagedObject and DelegateManager.TrueDispatch#1344

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
lostmsu merged 1 commit intopythonnet:masterfromtminka:error-messages
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes fromall 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
10 changes: 9 additions & 1 deletionsrc/embed_tests/TestPyObject.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -59,9 +59,17 @@ def add(self, x, y):
}

[Test]
public void InvokeNull() {
public void InvokeNull()
{
var list = PythonEngine.Eval("list");
Assert.Throws<ArgumentNullException>(() => list.Invoke(new PyObject[] {null}));
}

[Test]
public void AsManagedObjectInvalidCast()
{
var list = PythonEngine.Eval("list");
Assert.Throws<InvalidCastException>(() => list.AsManagedObject(typeof(int)));
}
}
}
24 changes: 5 additions & 19 deletionssrc/runtime/delegatemanager.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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;
}

Expand DownExpand Up@@ -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 newConversionException($"could not convert Python result to {rtype}");
throw newPythonException();
}

Runtime.XDecref(op);
return result;
}
}


public class ConversionException : Exception
{
public ConversionException()
{
}

public ConversionException(string msg) : base(msg)
{
}
}
}
11 changes: 3 additions & 8 deletionssrc/runtime/pyobject.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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;
}
Expand All@@ -154,12 +154,7 @@ public T As<T>()
{
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));
}


Expand Down
21 changes: 20 additions & 1 deletionsrc/tests/test_delegate.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
# TODO: Add test for ObjectDelegate

"""Test CLR delegate support."""

Expand DownExpand Up@@ -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
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp