| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c8ae358 commit a5f5dc2
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,6 +22,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][]. | |||
| 22 | 22 | attribute (#481) | |
| 23 | 23 | - Fixed conversion of 'float' and 'double' values (#486) | |
| 24 | 24 | - Fixed 'clrmethod' for python 2 (#492) | |
| 25 | + - Fixed double calling of constructor when deriving from .NET class (#495) | ||
| 25 | 26 | ||
| 26 | 27 | ||
| 27 | 28 | ## [2.3.0][] - 2017-03-11 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,4 @@ | |||
| 1 | - using System; | ||
| 1 | + using System; | ||
| 2 | 2 | using System.Collections.Generic; | |
| 3 | 3 | using System.Linq; | |
| 4 | 4 | using System.Reflection; | |
@@ -808,7 +808,6 @@ public static void InvokeCtor(IPythonDerivedType obj, string origCtorName, objec | |||
| 808 | 808 | obj, | |
| 809 | 809 | args); | |
| 810 | 810 | ||
| 811 | - var disposeList = new List<PyObject>(); | ||
| 812 | 811 | CLRObject self = null; | |
| 813 | 812 | IntPtr gs = Runtime.PyGILState_Ensure(); | |
| 814 | 813 | try | |
@@ -821,42 +820,9 @@ public static void InvokeCtor(IPythonDerivedType obj, string origCtorName, objec | |||
| 821 | 820 | // object to be collected. | |
| 822 | 821 | FieldInfo fi = obj.GetType().GetField("__pyobj__"); | |
| 823 | 822 | fi.SetValue(obj, self); | |
| 824 | - | ||
| 825 | - Runtime.XIncref(self.pyHandle); | ||
| 826 | - var pyself = new PyObject(self.pyHandle); | ||
| 827 | - disposeList.Add(pyself); | ||
| 828 | - | ||
| 829 | - Runtime.XIncref(Runtime.PyNone); | ||
| 830 | - var pynone = new PyObject(Runtime.PyNone); | ||
| 831 | - disposeList.Add(pynone); | ||
| 832 | - | ||
| 833 | - // call __init__ | ||
| 834 | - PyObject init = pyself.GetAttr("__init__", pynone); | ||
| 835 | - disposeList.Add(init); | ||
| 836 | - if (init.Handle != Runtime.PyNone) | ||
| 837 | - { | ||
| 838 | - // if __init__ hasn't been overridden then it will be a managed object | ||
| 839 | - ManagedType managedMethod = ManagedType.GetManagedObject(init.Handle); | ||
| 840 | - if (null == managedMethod) | ||
| 841 | - { | ||
| 842 | - var pyargs = new PyObject[args.Length]; | ||
| 843 | - for (var i = 0; i < args.Length; ++i) | ||
| 844 | - { | ||
| 845 | - pyargs[i] = new PyObject(Converter.ToPython(args[i], args[i]?.GetType())); | ||
| 846 | - disposeList.Add(pyargs[i]); | ||
| 847 | - } | ||
| 848 | - | ||
| 849 | - disposeList.Add(init.Invoke(pyargs)); | ||
| 850 | - } | ||
| 851 | - } | ||
| 852 | 823 | } | |
| 853 | 824 | finally | |
| 854 | 825 | { | |
| 855 | - foreach (PyObject x in disposeList) | ||
| 856 | - { | ||
| 857 | - x?.Dispose(); | ||
| 858 | - } | ||
| 859 | - | ||
| 860 | 826 | // Decrement the python object's reference count. | |
| 861 | 827 | // This doesn't actually destroy the object, it just sets the reference to this object | |
| 862 | 828 | // to be a weak reference and it will be destroyed when the C# object is destroyed. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,4 @@ | |||
| 1 | - using System; | ||
| 1 | + using System; | ||
| 2 | 2 | using System.Runtime.InteropServices; | |
| 3 | 3 | ||
| 4 | 4 | namespace Python.Runtime | |
@@ -157,23 +157,13 @@ public static IntPtr tp_call(IntPtr tp, IntPtr args, IntPtr kw) | |||
| 157 | 157 | return IntPtr.Zero; | |
| 158 | 158 | } | |
| 159 | 159 | ||
| 160 | - IntPtr py__init__ = Runtime.PyString_FromString("__init__"); | ||
| 161 | - IntPtr type = Runtime.PyObject_TYPE(obj); | ||
| 162 | - IntPtr init = Runtime._PyType_Lookup(type, py__init__); | ||
| 163 | - Runtime.XDecref(py__init__); | ||
| 160 | + var init = Runtime.PyObject_GetAttrString(obj, "__init__"); | ||
| 164 | 161 | Runtime.PyErr_Clear(); | |
| 165 | 162 | ||
| 166 | 163 | if (init != IntPtr.Zero) | |
| 167 | 164 | { | |
| 168 | - IntPtr bound = Runtime.GetBoundArgTuple(obj, args); | ||
| 169 | - if (bound == IntPtr.Zero) | ||
| 170 | - { | ||
| 171 | - Runtime.XDecref(obj); | ||
| 172 | - return IntPtr.Zero; | ||
| 173 | - } | ||
| 174 | - | ||
| 175 | - IntPtr result = Runtime.PyObject_Call(init, bound, kw); | ||
| 176 | - Runtime.XDecref(bound); | ||
| 165 | + IntPtr result = Runtime.PyObject_Call(init, args, kw); | ||
| 166 | + Runtime.XDecref(init); | ||
| 177 | 167 | ||
| 178 | 168 | if (result == IntPtr.Zero) | |
| 179 | 169 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -378,29 +378,6 @@ internal static void CheckExceptionOccurred() | |||
| 378 | 378 | } | |
| 379 | 379 | } | |
| 380 | 380 | ||
| 381 | - internal static IntPtr GetBoundArgTuple(IntPtr obj, IntPtr args) | ||
| 382 | - { | ||
| 383 | - if (PyObject_TYPE(args) != PyTupleType) | ||
| 384 | - { | ||
| 385 | - Exceptions.SetError(Exceptions.TypeError, "tuple expected"); | ||
| 386 | - return IntPtr.Zero; | ||
| 387 | - } | ||
| 388 | - int size = PyTuple_Size(args); | ||
| 389 | - IntPtr items = PyTuple_New(size + 1); | ||
| 390 | - PyTuple_SetItem(items, 0, obj); | ||
| 391 | - XIncref(obj); | ||
| 392 | - | ||
| 393 | - for (var i = 0; i < size; i++) | ||
| 394 | - { | ||
| 395 | - IntPtr item = PyTuple_GetItem(args, i); | ||
| 396 | - XIncref(item); | ||
| 397 | - PyTuple_SetItem(items, i + 1, item); | ||
| 398 | - } | ||
| 399 | - | ||
| 400 | - return items; | ||
| 401 | - } | ||
| 402 | - | ||
| 403 | - | ||
| 404 | 381 | internal static IntPtr ExtendTuple(IntPtr t, params IntPtr[] args) | |
| 405 | 382 | { | |
| 406 | 383 | int size = PyTuple_Size(t); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,12 +15,12 @@ | |||
| 15 | 15 | from ._compat import range | |
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | - def interface_test_class_fixture(): | ||
| 18 | + def interface_test_class_fixture(subnamespace): | ||
| 19 | 19 | """Delay creation of class until test starts.""" | |
| 20 | 20 | ||
| 21 | 21 | class InterfaceTestClass(IInterfaceTest): | |
| 22 | 22 | """class that implements the test interface""" | |
| 23 | - __namespace__ = "Python.Test" | ||
| 23 | + __namespace__ = "Python.Test." + subnamespace | ||
| 24 | 24 | ||
| 25 | 25 | def foo(self): | |
| 26 | 26 | return "InterfaceTestClass" | |
@@ -31,12 +31,12 @@ def bar(self, x, i): | |||
| 31 | 31 | return InterfaceTestClass | |
| 32 | 32 | ||
| 33 | 33 | ||
| 34 | - def derived_class_fixture(): | ||
| 34 | + def derived_class_fixture(subnamespace): | ||
| 35 | 35 | """Delay creation of class until test starts.""" | |
| 36 | 36 | ||
| 37 | 37 | class DerivedClass(SubClassTest): | |
| 38 | 38 | """class that derives from a class deriving from IInterfaceTest""" | |
| 39 | - __namespace__ = "Python.Test" | ||
| 39 | + __namespace__ = "Python.Test." + subnamespace | ||
| 40 | 40 | ||
| 41 | 41 | def foo(self): | |
| 42 | 42 | return "DerivedClass" | |
@@ -60,12 +60,12 @@ def return_list(self): | |||
| 60 | 60 | return DerivedClass | |
| 61 | 61 | ||
| 62 | 62 | ||
| 63 | - def derived_event_test_class_fixture(): | ||
| 63 | + def derived_event_test_class_fixture(subnamespace): | ||
| 64 | 64 | """Delay creation of class until test starts.""" | |
| 65 | 65 | ||
| 66 | 66 | class DerivedEventTest(IInterfaceTest): | |
| 67 | 67 | """class that implements IInterfaceTest.TestEvent""" | |
| 68 | - __namespace__ = "Python.Test" | ||
| 68 | + __namespace__ = "Python.Test." + subnamespace | ||
| 69 | 69 | ||
| 70 | 70 | def __init__(self): | |
| 71 | 71 | self.event_handlers = [] | |
@@ -99,7 +99,7 @@ def test_base_class(): | |||
| 99 | 99 | ||
| 100 | 100 | def test_interface(): | |
| 101 | 101 | """Test python classes can derive from C# interfaces""" | |
| 102 | - InterfaceTestClass = interface_test_class_fixture() | ||
| 102 | + InterfaceTestClass = interface_test_class_fixture(test_interface.__name__) | ||
| 103 | 103 | ob = InterfaceTestClass() | |
| 104 | 104 | assert ob.foo() == "InterfaceTestClass" | |
| 105 | 105 | assert FunctionsTest.test_foo(ob) == "InterfaceTestClass" | |
@@ -112,7 +112,7 @@ def test_interface(): | |||
| 112 | 112 | ||
| 113 | 113 | def test_derived_class(): | |
| 114 | 114 | """Test python class derived from managed type""" | |
| 115 | - DerivedClass = derived_class_fixture() | ||
| 115 | + DerivedClass = derived_class_fixture(test_derived_class.__name__) | ||
| 116 | 116 | ob = DerivedClass() | |
| 117 | 117 | assert ob.foo() == "DerivedClass" | |
| 118 | 118 | assert ob.base_foo() == "foo" | |
@@ -128,10 +128,9 @@ def test_derived_class(): | |||
| 128 | 128 | assert id(x) == id(ob) | |
| 129 | 129 | ||
| 130 | 130 | ||
| 131 | - @pytest.mark.skip(reason="FIXME: test randomly pass/fails") | ||
| 132 | 131 | def test_create_instance(): | |
| 133 | 132 | """Test derived instances can be created from managed code""" | |
| 134 | - DerivedClass = derived_class_fixture() | ||
| 133 | + DerivedClass = derived_class_fixture(test_create_instance.__name__) | ||
| 135 | 134 | ob = FunctionsTest.create_instance(DerivedClass) | |
| 136 | 135 | assert ob.foo() == "DerivedClass" | |
| 137 | 136 | assert FunctionsTest.test_foo(ob) == "DerivedClass" | |
@@ -142,7 +141,7 @@ def test_create_instance(): | |||
| 142 | 141 | x = FunctionsTest.pass_through(ob) | |
| 143 | 142 | assert id(x) == id(ob) | |
| 144 | 143 | ||
| 145 | - InterfaceTestClass = interface_test_class_fixture() | ||
| 144 | + InterfaceTestClass = interface_test_class_fixture(test_create_instance.__name__) | ||
| 146 | 145 | ob2 = FunctionsTest.create_instance(InterfaceTestClass) | |
| 147 | 146 | assert ob2.foo() == "InterfaceTestClass" | |
| 148 | 147 | assert FunctionsTest.test_foo(ob2) == "InterfaceTestClass" | |
@@ -153,7 +152,6 @@ def test_create_instance(): | |||
| 153 | 152 | assert id(y) == id(ob2) | |
| 154 | 153 | ||
| 155 | 154 | ||
| 156 | - @pytest.mark.skip(reason="FIXME: test randomly pass/fails") | ||
| 157 | 155 | def test_events(): | |
| 158 | 156 | class EventHandler(object): | |
| 159 | 157 | def handler(self, x, args): | |
@@ -166,12 +164,12 @@ def handler(self, x, args): | |||
| 166 | 164 | assert FunctionsTest.test_event(x, 1) == 1 | |
| 167 | 165 | assert event_handler.value == 1 | |
| 168 | 166 | ||
| 169 | - InterfaceTestClass = interface_test_class_fixture() | ||
| 167 | + InterfaceTestClass = interface_test_class_fixture(test_events.__name__) | ||
| 170 | 168 | i = InterfaceTestClass() | |
| 171 | 169 | with pytest.raises(System.NotImplementedException): | |
| 172 | 170 | FunctionsTest.test_event(i, 2) | |
| 173 | 171 | ||
| 174 | - DerivedEventTest = derived_event_test_class_fixture() | ||
| 172 | + DerivedEventTest = derived_event_test_class_fixture(test_events.__name__) | ||
| 175 | 173 | d = DerivedEventTest() | |
| 176 | 174 | d.add_TestEvent(event_handler.handler) | |
| 177 | 175 | assert FunctionsTest.test_event(d, 3) == 3 | |
@@ -190,3 +188,59 @@ def test_isinstance_check(): | |||
| 190 | 188 | for x in b: | |
| 191 | 189 | assert isinstance(x, System.Object) | |
| 192 | 190 | assert isinstance(x, System.String) | |
| 191 | + | ||
| 192 | + def test_namespace_and_init(): | ||
| 193 | + calls = [] | ||
| 194 | + class TestX(System.Object): | ||
| 195 | + __namespace__ = "test_clr_subclass_with_init_args" | ||
| 196 | + def __init__(self, *args, **kwargs): | ||
| 197 | + calls.append((args, kwargs)) | ||
| 198 | + t = TestX(1,2,3,foo="bar") | ||
| 199 | + assert len(calls) == 1 | ||
| 200 | + assert calls[0][0] == (1,2,3) | ||
| 201 | + assert calls[0][1] == {"foo":"bar"} | ||
| 202 | + | ||
| 203 | + def test_namespace_and_argless_init(): | ||
| 204 | + calls = [] | ||
| 205 | + class TestX(System.Object): | ||
| 206 | + __namespace__ = "test_clr_subclass_without_init_args" | ||
| 207 | + def __init__(self): | ||
| 208 | + calls.append(True) | ||
| 209 | + t = TestX() | ||
| 210 | + assert len(calls) == 1 | ||
| 211 | + assert calls[0] == True | ||
| 212 | + | ||
| 213 | + | ||
| 214 | + def test_namespace_and_no_init(): | ||
| 215 | + class TestX(System.Object): | ||
| 216 | + __namespace__ = "test_clr_subclass_without_init" | ||
| 217 | + q = 1 | ||
| 218 | + t = TestX() | ||
| 219 | + assert t.q == 1 | ||
| 220 | + | ||
| 221 | + def test_construction_from_clr(): | ||
| 222 | + import clr | ||
| 223 | + calls = [] | ||
| 224 | + class TestX(System.Object): | ||
| 225 | + __namespace__ = "test_clr_subclass_init_from_clr" | ||
| 226 | + @clr.clrmethod(None, [int, str]) | ||
| 227 | + def __init__(self, i, s): | ||
| 228 | + calls.append((i, s)) | ||
| 229 | + | ||
| 230 | + # Construct a TestX from Python | ||
| 231 | + t = TestX(1, "foo") | ||
| 232 | + assert len(calls) == 1 | ||
| 233 | + assert calls[0][0] == 1 | ||
| 234 | + assert calls[0][1] == "foo" | ||
| 235 | + | ||
| 236 | + # Reset calls and construct a TestX from CLR | ||
| 237 | + calls = [] | ||
| 238 | + tp = t.GetType() | ||
| 239 | + t2 = tp.GetConstructors()[0].Invoke(None) | ||
| 240 | + assert len(calls) == 0 | ||
| 241 | + | ||
| 242 | + # The object has only been constructed, now it needs to be initialized as well | ||
| 243 | + tp.GetMethod("__init__").Invoke(t2, [1, "foo"]) | ||
| 244 | + assert len(calls) == 1 | ||
| 245 | + assert calls[0][0] == 1 | ||
| 246 | + assert calls[0][1] == "foo" | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments