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

Commit107c8b9

Browse files
tminkalostmsu
authored andcommitted
PyObject.AsManagedObject gives a better error message
DelegateManager.TrueDispatch gives a better error message
1 parentcac82a6 commit107c8b9

File tree

4 files changed

+37
-29
lines changed

4 files changed

+37
-29
lines changed

‎src/embed_tests/TestPyObject.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,17 @@ def add(self, x, y):
5959
}
6060

6161
[Test]
62-
publicvoidInvokeNull(){
62+
publicvoidInvokeNull()
63+
{
6364
varlist=PythonEngine.Eval("list");
6465
Assert.Throws<ArgumentNullException>(()=>list.Invoke(newPyObject[]{null}));
6566
}
67+
68+
[Test]
69+
publicvoidAsManagedObjectInvalidCast()
70+
{
71+
varlist=PythonEngine.Eval("list");
72+
Assert.Throws<InvalidCastException>(()=>list.AsManagedObject(typeof(int)));
73+
}
6674
}
6775
}

‎src/runtime/delegatemanager.cs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -221,19 +221,17 @@ public void Dispose()
221221
publicobjectDispatch(ArrayListargs)
222222
{
223223
IntPtrgs=PythonEngine.AcquireLock();
224-
objectob=null;
224+
objectob;
225225

226226
try
227227
{
228228
ob=TrueDispatch(args);
229229
}
230-
catch(Exceptione)
230+
finally
231231
{
232232
PythonEngine.ReleaseLock(gs);
233-
throwe;
234233
}
235234

236-
PythonEngine.ReleaseLock(gs);
237235
returnob;
238236
}
239237

@@ -266,27 +264,15 @@ public object TrueDispatch(ArrayList args)
266264
returnnull;
267265
}
268266

269-
objectresult=null;
270-
if(!Converter.ToManaged(op,rtype,outresult,false))
267+
objectresult;
268+
if(!Converter.ToManaged(op,rtype,outresult,true))
271269
{
272270
Runtime.XDecref(op);
273-
thrownewConversionException($"could not convert Python result to{rtype}");
271+
thrownewPythonException();
274272
}
275273

276274
Runtime.XDecref(op);
277275
returnresult;
278276
}
279277
}
280-
281-
282-
publicclassConversionException:Exception
283-
{
284-
publicConversionException()
285-
{
286-
}
287-
288-
publicConversionException(stringmsg):base(msg)
289-
{
290-
}
291-
}
292278
}

‎src/runtime/pyobject.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ public static PyObject FromManagedObject(object ob)
134134
publicobjectAsManagedObject(Typet)
135135
{
136136
objectresult;
137-
if(!Converter.ToManaged(obj,t,outresult,false))
137+
if(!Converter.ToManaged(obj,t,outresult,true))
138138
{
139-
thrownewInvalidCastException("cannot convert object to target type");
139+
thrownewInvalidCastException("cannot convert object to target type",newPythonException());
140140
}
141141
returnresult;
142142
}
@@ -154,12 +154,7 @@ public T As<T>()
154154
{
155155
return(T)(thisasobject);
156156
}
157-
objectresult;
158-
if(!Converter.ToManaged(obj,typeof(T),outresult,false))
159-
{
160-
thrownewInvalidCastException("cannot convert object to target type");
161-
}
162-
return(T)result;
157+
return(T)AsManagedObject(typeof(T));
163158
}
164159

165160

‎src/tests/test_delegate.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
# TODO: Add test for ObjectDelegate
32

43
"""Test CLR delegate support."""
54

@@ -257,6 +256,26 @@ def always_so_negative():
257256
assertnotd()
258257
assertnotob.CallBoolDelegate(d)
259258

259+
deftest_object_delegate():
260+
"""Test object delegate."""
261+
fromPython.TestimportObjectDelegate
262+
263+
defcreate_object():
264+
returnDelegateTest()
265+
266+
d=ObjectDelegate(create_object)
267+
ob=DelegateTest()
268+
ob.CallObjectDelegate(d)
269+
270+
deftest_invalid_object_delegate():
271+
"""Test invalid object delegate with mismatched return type."""
272+
fromPython.TestimportObjectDelegate
273+
274+
d=ObjectDelegate(hello_func)
275+
ob=DelegateTest()
276+
withpytest.raises(TypeError):
277+
ob.CallObjectDelegate(d)
278+
260279
# test async delegates
261280

262281
# test multicast delegates

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp