| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 66d755f commit 566898a
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -126,6 +126,12 @@ internal static IntPtr ToPython<T>(T value) | |||
| 126 | 126 | ||
| 127 | 127 | internal static IntPtr ToPython(Object value, Type type) | |
| 128 | 128 | { | |
| 129 | + if(value is PyObject) | ||
| 130 | + { | ||
| 131 | + IntPtr handle = ((PyObject)value).Handle; | ||
| 132 | + Runtime.XIncref(handle); | ||
| 133 | + return handle; | ||
| 134 | + } | ||
| 129 | 135 | IntPtr result = IntPtr.Zero; | |
| 130 | 136 | ||
| 131 | 137 | // Null always converts to None in Python. | |
@@ -265,6 +271,13 @@ internal static bool ToManaged(IntPtr value, Type type, | |||
| 265 | 271 | internal static bool ToManagedValue(IntPtr value, Type obType, | |
| 266 | 272 | out Object result, bool setError) | |
| 267 | 273 | { | |
| 274 | + if (obType == typeof(PyObject)) | ||
| 275 | + { | ||
| 276 | + Runtime.XIncref(value); // PyObject() assumes ownership | ||
| 277 | + result = new PyObject(value); | ||
| 278 | + return true; | ||
| 279 | + } | ||
| 280 | + | ||
| 268 | 281 | // Common case: if the Python value is a wrapped managed object | |
| 269 | 282 | // instance, just return the wrapped object. | |
| 270 | 283 | ManagedType mt = ManagedType.GetManagedObject(value); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -127,6 +127,16 @@ protected virtual void Dispose(bool disposing) | |||
| 127 | 127 | } | |
| 128 | 128 | } | |
| 129 | 129 | ||
| 130 | + public long RefCount | ||
| 131 | + { | ||
| 132 | + get | ||
| 133 | + { | ||
| 134 | + if(!disposed) | ||
| 135 | + return Runtime.Refcount(obj); | ||
| 136 | + return -1; | ||
| 137 | + } | ||
| 138 | + } | ||
| 139 | + | ||
| 130 | 140 | public void Dispose() | |
| 131 | 141 | { | |
| 132 | 142 | Dispose(true); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,4 +28,18 @@ public string Call_simpleDefaultArg_WithEmptyArgs(string moduleName) | |||
| 28 | 28 | } | |
| 29 | 29 | } | |
| 30 | 30 | } | |
| 31 | + | ||
| 32 | + //========================================================================== | ||
| 33 | + // Tests calling from Python into C# and back into Python using a PyObject. | ||
| 34 | + // SelfCallbackTest should be inherited by a Python class. | ||
| 35 | + // Used in test_class.py / testCallback | ||
| 36 | + //========================================================================== | ||
| 37 | + public class SelfCallbackTest | ||
| 38 | + { | ||
| 39 | + public void Callback(Runtime.PyObject self) | ||
| 40 | + { | ||
| 41 | + using (Runtime.Py.GIL()) | ||
| 42 | + ((dynamic)self).PyCallback(self); | ||
| 43 | + } | ||
| 44 | + } | ||
| 31 | 45 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -250,6 +250,21 @@ def testComparisons(self): | |||
| 250 | 250 | c2 = ClassTest() | |
| 251 | 251 | self.assertRaises(TypeError, lambda: c1 < c2) | |
| 252 | 252 | ||
| 253 | + def testSelfCallback(self): | ||
| 254 | + """ Test calling back and forth between this and a c# baseclass.""" | ||
| 255 | + class CallbackUser(Test.SelfCallbackTest): | ||
| 256 | + def DoCallback(self): | ||
| 257 | + self.PyCallbackWasCalled = False | ||
| 258 | + self.SameReference = False | ||
| 259 | + return self.Callback(self) | ||
| 260 | + def PyCallback(self, self2): | ||
| 261 | + self.PyCallbackWasCalled = True | ||
| 262 | + self.SameReference = self == self2 | ||
| 263 | + | ||
| 264 | + testobj = CallbackUser() | ||
| 265 | + testobj.DoCallback() | ||
| 266 | + self.assertTrue(testobj.PyCallbackWasCalled) | ||
| 267 | + self.assertTrue(testobj.SameReference) | ||
| 253 | 268 | ||
| 254 | 269 | class ClassicClass: | |
| 255 | 270 | def kind(self): | |
| Back | FazBrowse Home | New Git URL |
0 commit comments