| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1dd36ae commit a74664c
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -43,8 +43,9 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw) | |||
| 43 | 43 | public static IntPtr mp_subscript(IntPtr ob, IntPtr idx) | |
| 44 | 44 | { | |
| 45 | 45 | var obj = (CLRObject)GetManagedObject(ob); | |
| 46 | + var arrObj = (ArrayObject)GetManagedObjectType(ob); | ||
| 46 | 47 | var items = obj.inst as Array; | |
| 47 | - Type itemType = obj.inst.GetType().GetElementType(); | ||
| 48 | + Type itemType = arrObj.type.GetElementType(); | ||
| 48 | 49 | int rank = items.Rank; | |
| 49 | 50 | int index; | |
| 50 | 51 | object value; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -179,6 +179,15 @@ internal static IntPtr ToPython(object value, Type type) | |||
| 179 | 179 | return CLRObject.GetInstHandle(value, ifaceObj.pyHandle); | |
| 180 | 180 | } | |
| 181 | 181 | ||
| 182 | + // We need to special case interface array handling to ensure we | ||
| 183 | + // produce the correct type. Value may be an array of some concrete | ||
| 184 | + // type (FooImpl[]), but we want access to go via the interface type | ||
| 185 | + // (IFoo[]). | ||
| 186 | + if (type.IsArray && type.GetElementType().IsInterface) | ||
| 187 | + { | ||
| 188 | + return CLRObject.GetInstHandle(value, type); | ||
| 189 | + } | ||
| 190 | + | ||
| 182 | 191 | // it the type is a python subclass of a managed type then return the | |
| 183 | 192 | // underlying python object rather than construct a new wrapper object. | |
| 184 | 193 | var pyderived = value as IPythonDerivedType; | |
@@ -188,7 +197,6 @@ internal static IntPtr ToPython(object value, Type type) | |||
| 188 | 197 | return ClassDerivedObject.ToPython(pyderived); | |
| 189 | 198 | } | |
| 190 | 199 | ||
| 191 | - | ||
| 192 | 200 | // hmm - from Python, we almost never care what the declared | |
| 193 | 201 | // type is. we'd rather have the object bound to the actual | |
| 194 | 202 | // implementing class. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -45,6 +45,25 @@ internal static ManagedType GetManagedObject(IntPtr ob) | |||
| 45 | 45 | return null; | |
| 46 | 46 | } | |
| 47 | 47 | ||
| 48 | + /// <summary> | ||
| 49 | + /// Given a Python object, return the associated managed object type or null. | ||
| 50 | + /// </summary> | ||
| 51 | + internal static ManagedType GetManagedObjectType(IntPtr ob) | ||
| 52 | + { | ||
| 53 | + if (ob != IntPtr.Zero) | ||
| 54 | + { | ||
| 55 | + IntPtr tp = Runtime.PyObject_TYPE(ob); | ||
| 56 | + var flags = Util.ReadCLong(tp, TypeOffset.tp_flags); | ||
| 57 | + if ((flags & TypeFlags.Managed) != 0) | ||
| 58 | + { | ||
| 59 | + tp = Marshal.ReadIntPtr(tp, TypeOffset.magic()); | ||
| 60 | + var gc = (GCHandle)tp; | ||
| 61 | + return (ManagedType)gc.Target; | ||
| 62 | + } | ||
| 63 | + } | ||
| 64 | + return null; | ||
| 65 | + } | ||
| 66 | + | ||
| 48 | 67 | ||
| 49 | 68 | internal static ManagedType GetManagedObjectErr(IntPtr ob) | |
| 50 | 69 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -58,6 +58,11 @@ public ISayHello1 GetNoSayHello(out ISayHello2 hello2) | |||
| 58 | 58 | return null; | |
| 59 | 59 | } | |
| 60 | 60 | ||
| 61 | + public ISayHello1 [] GetISayHello1Array() | ||
| 62 | + { | ||
| 63 | + return new[] { this }; | ||
| 64 | + } | ||
| 65 | + | ||
| 61 | 66 | public interface IPublic | |
| 62 | 67 | { | |
| 63 | 68 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -99,3 +99,11 @@ def test_null_interface_object_returned(): | |||
| 99 | 99 | hello1, hello2 = ob.GetNoSayHello(None) | |
| 100 | 100 | assert hello1 is None | |
| 101 | 101 | assert hello2 is None | |
| 102 | + | ||
| 103 | + def test_interface_array_returned(): | ||
| 104 | + """Test interface type used for methods returning interface arrays""" | ||
| 105 | + from Python.Test import InterfaceTest | ||
| 106 | + | ||
| 107 | + ob = InterfaceTest() | ||
| 108 | + hellos = ob.GetISayHello1Array() | ||
| 109 | + assert type(hellos[0]).__name__ == 'ISayHello1' | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments