| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent cac82a6 commit 107c8b9
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -59,9 +59,17 @@ def add(self, x, y): | |||
| 59 | 59 | } | |
| 60 | 60 | ||
| 61 | 61 | [Test] | |
| 62 | - public void InvokeNull() { | ||
| 62 | + public void InvokeNull() | ||
| 63 | + { | ||
| 63 | 64 | var list = PythonEngine.Eval("list"); | |
| 64 | 65 | Assert.Throws<ArgumentNullException>(() => list.Invoke(new PyObject[] {null})); | |
| 65 | 66 | } | |
| 67 | + | ||
| 68 | + [Test] | ||
| 69 | + public void AsManagedObjectInvalidCast() | ||
| 70 | + { | ||
| 71 | + var list = PythonEngine.Eval("list"); | ||
| 72 | + Assert.Throws<InvalidCastException>(() => list.AsManagedObject(typeof(int))); | ||
| 73 | + } | ||
| 66 | 74 | } | |
| 67 | 75 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -221,19 +221,17 @@ public void Dispose() | |||
| 221 | 221 | public object Dispatch(ArrayList args) | |
| 222 | 222 | { | |
| 223 | 223 | IntPtr gs = PythonEngine.AcquireLock(); | |
| 224 | - object ob = null; | ||
| 224 | + object ob; | ||
| 225 | 225 | ||
| 226 | 226 | try | |
| 227 | 227 | { | |
| 228 | 228 | ob = TrueDispatch(args); | |
| 229 | 229 | } | |
| 230 | - catch (Exception e) | ||
| 230 | + finally | ||
| 231 | 231 | { | |
| 232 | 232 | PythonEngine.ReleaseLock(gs); | |
| 233 | - throw e; | ||
| 234 | 233 | } | |
| 235 | 234 | ||
| 236 | - PythonEngine.ReleaseLock(gs); | ||
| 237 | 235 | return ob; | |
| 238 | 236 | } | |
| 239 | 237 | ||
@@ -266,27 +264,15 @@ public object TrueDispatch(ArrayList args) | |||
| 266 | 264 | return null; | |
| 267 | 265 | } | |
| 268 | 266 | ||
| 269 | - object result = null; | ||
| 270 | - if (!Converter.ToManaged(op, rtype, out result, false)) | ||
| 267 | + object result; | ||
| 268 | + if (!Converter.ToManaged(op, rtype, out result, true)) | ||
| 271 | 269 | { | |
| 272 | 270 | Runtime.XDecref(op); | |
| 273 | - throw new ConversionException($"could not convert Python result to {rtype}"); | ||
| 271 | + throw new PythonException(); | ||
| 274 | 272 | } | |
| 275 | 273 | ||
| 276 | 274 | Runtime.XDecref(op); | |
| 277 | 275 | return result; | |
| 278 | 276 | } | |
| 279 | 277 | } | |
| 280 | - | ||
| 281 | - | ||
| 282 | - public class ConversionException : Exception | ||
| 283 | - { | ||
| 284 | - public ConversionException() | ||
| 285 | - { | ||
| 286 | - } | ||
| 287 | - | ||
| 288 | - public ConversionException(string msg) : base(msg) | ||
| 289 | - { | ||
| 290 | - } | ||
| 291 | - } | ||
| 292 | 278 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -134,9 +134,9 @@ public static PyObject FromManagedObject(object ob) | |||
| 134 | 134 | public object AsManagedObject(Type t) | |
| 135 | 135 | { | |
| 136 | 136 | object result; | |
| 137 | - if (!Converter.ToManaged(obj, t, out result, false)) | ||
| 137 | + if (!Converter.ToManaged(obj, t, out result, true)) | ||
| 138 | 138 | { | |
| 139 | - throw new InvalidCastException("cannot convert object to target type"); | ||
| 139 | + throw new InvalidCastException("cannot convert object to target type", new PythonException()); | ||
| 140 | 140 | } | |
| 141 | 141 | return result; | |
| 142 | 142 | } | |
@@ -154,12 +154,7 @@ public T As<T>() | |||
| 154 | 154 | { | |
| 155 | 155 | return (T)(this as object); | |
| 156 | 156 | } | |
| 157 | - object result; | ||
| 158 | - if (!Converter.ToManaged(obj, typeof(T), out result, false)) | ||
| 159 | - { | ||
| 160 | - throw new InvalidCastException("cannot convert object to target type"); | ||
| 161 | - } | ||
| 162 | - return (T)result; | ||
| 157 | + return (T)AsManagedObject(typeof(T)); | ||
| 163 | 158 | } | |
| 164 | 159 | ||
| 165 | 160 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,4 @@ | |||
| 1 | 1 | # -*- coding: utf-8 -*- | |
| 2 | - # TODO: Add test for ObjectDelegate | ||
| 3 | 2 | ||
| 4 | 3 | """Test CLR delegate support.""" | |
| 5 | 4 | ||
@@ -257,6 +256,26 @@ def always_so_negative(): | |||
| 257 | 256 | assert not d() | |
| 258 | 257 | assert not ob.CallBoolDelegate(d) | |
| 259 | 258 | ||
| 259 | + def test_object_delegate(): | ||
| 260 | + """Test object delegate.""" | ||
| 261 | + from Python.Test import ObjectDelegate | ||
| 262 | + | ||
| 263 | + def create_object(): | ||
| 264 | + return DelegateTest() | ||
| 265 | + | ||
| 266 | + d = ObjectDelegate(create_object) | ||
| 267 | + ob = DelegateTest() | ||
| 268 | + ob.CallObjectDelegate(d) | ||
| 269 | + | ||
| 270 | + def test_invalid_object_delegate(): | ||
| 271 | + """Test invalid object delegate with mismatched return type.""" | ||
| 272 | + from Python.Test import ObjectDelegate | ||
| 273 | + | ||
| 274 | + d = ObjectDelegate(hello_func) | ||
| 275 | + ob = DelegateTest() | ||
| 276 | + with pytest.raises(TypeError): | ||
| 277 | + ob.CallObjectDelegate(d) | ||
| 278 | + | ||
| 260 | 279 | # test async delegates | |
| 261 | 280 | ||
| 262 | 281 | # test multicast delegates | |
| Back | FazBrowse Home | New Git URL |
0 commit comments