| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e073a2e commit 8363502
11 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,7 +27,18 @@ internal override bool CanSubclass() { | |||
| 27 | 27 | return false; | |
| 28 | 28 | } | |
| 29 | 29 | ||
| 30 | - public static IntPtr tp_new(IntPtr ob, IntPtr args, IntPtr kw) { | ||
| 30 | + public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw) { | ||
| 31 | + ArrayObject self = GetManagedObject(tp) as ArrayObject; | ||
| 32 | + if (Runtime.PyTuple_Size(args) != 1) { | ||
| 33 | + return Exceptions.RaiseTypeError("array expects 1 argument"); | ||
| 34 | + } | ||
| 35 | + IntPtr op = Runtime.PyTuple_GetItem(args, 0); | ||
| 36 | + Object result; | ||
| 37 | + | ||
| 38 | + if (!Converter.ToManaged(op, self.type, out result, true)) { | ||
| 39 | + return IntPtr.Zero; | ||
| 40 | + } | ||
| 41 | + return CLRObject.GetInstHandle(result, tp); | ||
| 31 | 42 | string message = "cannot instantiate array wrapper"; | |
| 32 | 43 | return Exceptions.RaiseTypeError(message); | |
| 33 | 44 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -50,6 +50,16 @@ public static int tp_init(IntPtr ob, IntPtr args, IntPtr kw) { | |||
| 50 | 50 | return 0; | |
| 51 | 51 | } | |
| 52 | 52 | ||
| 53 | + | ||
| 54 | + //==================================================================== | ||
| 55 | + // Default implementation of [] semantics for reflected types. | ||
| 56 | + //==================================================================== | ||
| 57 | + | ||
| 58 | + public virtual IntPtr type_subscript(IntPtr ob, IntPtr idx) { | ||
| 59 | + return Exceptions.RaiseTypeError("unsubscriptable object"); | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + | ||
| 53 | 63 | //==================================================================== | |
| 54 | 64 | // Standard comparison implementation for instances of reflected types. | |
| 55 | 65 | //==================================================================== | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -124,6 +124,32 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw) { | |||
| 124 | 124 | } | |
| 125 | 125 | ||
| 126 | 126 | ||
| 127 | + //==================================================================== | ||
| 128 | + // Implementation of [] semantics for reflected types. This mainly | ||
| 129 | + // exists to implement the Array[int] syntax for creating arrays. | ||
| 130 | + //==================================================================== | ||
| 131 | + | ||
| 132 | + public override IntPtr type_subscript(IntPtr ob, IntPtr idx) { | ||
| 133 | + if ((this.type) != typeof(Array)) { | ||
| 134 | + return Exceptions.RaiseTypeError("unsubscriptable object"); | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + if (Runtime.PyTuple_Check(idx)) { | ||
| 138 | + return Exceptions.RaiseTypeError("expected single type"); | ||
| 139 | + } | ||
| 140 | + | ||
| 141 | + ClassBase c = GetManagedObject(idx) as ClassBase; | ||
| 142 | + Type t = (c != null) ? c.type : Converter.GetTypeByAlias(idx); | ||
| 143 | + if (t == null) { | ||
| 144 | + return Exceptions.RaiseTypeError("type expected"); | ||
| 145 | + } | ||
| 146 | + Array a = Array.CreateInstance(t, 0); | ||
| 147 | + c = ClassManager.GetClass(a.GetType()); | ||
| 148 | + Runtime.Incref(c.pyHandle); | ||
| 149 | + return c.pyHandle; | ||
| 150 | + } | ||
| 151 | + | ||
| 152 | + | ||
| 127 | 153 | //==================================================================== | |
| 128 | 154 | // Implements __getitem__ for reflected classes and value types. | |
| 129 | 155 | //==================================================================== | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,10 +8,10 @@ | |||
| 8 | 8 | // ========================================================================== | |
| 9 | 9 | ||
| 10 | 10 | //============================================================================ | |
| 11 | - // This file is a hand-maintained stub - it implements CLR.dll, which can be | ||
| 11 | + // This file is a hand-maintained stub - it implements clr.dll, which can be | ||
| 12 | 12 | // loaded by a standard CPython interpreter as an extension module. When it | |
| 13 | 13 | // is loaded, it bootstraps the managed runtime integration layer and defers | |
| 14 | - // to it do initialization and put the CLR module into sys.modules, etc. | ||
| 14 | + // to it do initialization and put the clr module into sys.modules, etc. | ||
| 15 | 15 | //============================================================================ | |
| 16 | 16 | ||
| 17 | 17 | .assembly extern mscorlib | |
@@ -23,7 +23,7 @@ | |||
| 23 | 23 | { | |
| 24 | 24 | .ver 1:0:0:0 | |
| 25 | 25 | } | |
| 26 | - .assembly CLR | ||
| 26 | + .assembly clr | ||
| 27 | 27 | { | |
| 28 | 28 | // --- The following custom attribute is added automatically, do not uncomment ------- | |
| 29 | 29 | // .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(bool, | |
@@ -32,7 +32,7 @@ | |||
| 32 | 32 | .ver 0:0:0:0 | |
| 33 | 33 | } | |
| 34 | 34 | ||
| 35 | - .module CLR.dll | ||
| 35 | + .module clr.dll | ||
| 36 | 36 | // MVID: {01BEB897-1638-4D9D-B01C-3638714A76B4} | |
| 37 | 37 | .imagebase 0x00400000 | |
| 38 | 38 | .subsystem 0x00000003 | |
@@ -43,30 +43,30 @@ | |||
| 43 | 43 | .data VT_01 = int32(0) | |
| 44 | 44 | ||
| 45 | 45 | ||
| 46 | - .class public auto ansi beforefieldinit CLRModule | ||
| 46 | + .class public auto ansi beforefieldinit clrModule | ||
| 47 | 47 | extends [mscorlib]System.Object | |
| 48 | 48 | { | |
| 49 | - } // end of class CLRModule | ||
| 49 | + } // end of class clrModule | ||
| 50 | 50 | ||
| 51 | 51 | ||
| 52 | 52 | ||
| 53 | - .class public auto ansi beforefieldinit CLRModule | ||
| 53 | + .class public auto ansi beforefieldinit clrModule | ||
| 54 | 54 | extends [mscorlib]System.Object | |
| 55 | 55 | { | |
| 56 | 56 | .method public hidebysig static void | |
| 57 | 57 | modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl) | |
| 58 | - initCLR() cil managed | ||
| 58 | + initclr() cil managed | ||
| 59 | 59 | { | |
| 60 | 60 | .vtentry 1:1 | |
| 61 | - .export [1] as initCLR | ||
| 61 | + .export [1] as initclr | ||
| 62 | 62 | ||
| 63 | 63 | // Code size 8 (0x8) | |
| 64 | 64 | .maxstack 0 | |
| 65 | 65 | IL_0000: call void [Python.Runtime]Python.Runtime.PythonEngine::InitExt() | |
| 66 | 66 | IL_0005: br.s IL_0007 | |
| 67 | 67 | ||
| 68 | 68 | IL_0007: ret | |
| 69 | - } // end of method CLRModule::initCLR | ||
| 69 | + } // end of method clrModule::initclr | ||
| 70 | 70 | ||
| 71 | 71 | .method public hidebysig specialname rtspecialname | |
| 72 | 72 | instance void .ctor() cil managed | |
@@ -76,8 +76,8 @@ | |||
| 76 | 76 | IL_0000: ldarg.0 | |
| 77 | 77 | IL_0001: call instance void [mscorlib]System.Object::.ctor() | |
| 78 | 78 | IL_0006: ret | |
| 79 | - } // end of method CLRModule::.ctor | ||
| 79 | + } // end of method clrModule::.ctor | ||
| 80 | 80 | ||
| 81 | - } // end of class CLRModule | ||
| 81 | + } // end of class clrModule | ||
| 82 | 82 | ||
| 83 | 83 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -221,11 +221,11 @@ public static int tp_setattro(IntPtr tp, IntPtr name, IntPtr value) { | |||
| 221 | 221 | // own mp_subscript | |
| 222 | 222 | //==================================================================== | |
| 223 | 223 | public static IntPtr mp_subscript(IntPtr tp, IntPtr idx) { | |
| 224 | - GenericType gt = GetManagedObject(tp) as GenericType; | ||
| 225 | - if (gt != null) { | ||
| 226 | - return GenericType.bind(tp, idx); | ||
| 227 | - } | ||
| 228 | - return Exceptions.RaiseTypeError("unsubscriptable object"); | ||
| 224 | + ClassBase cb = GetManagedObject(tp) as ClassBase; | ||
| 225 | + if (cb != null) { | ||
| 226 | + return cb.type_subscript(tp, idx); | ||
| 227 | + } | ||
| 228 | + return Exceptions.RaiseTypeError("unsubscriptable object"); | ||
| 229 | 229 | } | |
| 230 | 230 | ||
| 231 | 231 | //==================================================================== | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -114,6 +114,55 @@ internal static int ArgPrecedence(Type t) { | |||
| 114 | 114 | } | |
| 115 | 115 | ||
| 116 | 116 | ||
| 117 | + internal static MethodInfo MatchByTypeSig(MethodInfo[] msig, | ||
| 118 | + IntPtr psig) { | ||
| 119 | + IntPtr args = psig; | ||
| 120 | + bool free = false; | ||
| 121 | + | ||
| 122 | + if (!Runtime.PyTuple_Check(psig)) { | ||
| 123 | + args = Runtime.PyTuple_New(1); | ||
| 124 | + Runtime.Incref(psig); | ||
| 125 | + Runtime.PyTuple_SetItem(args, 0, psig); | ||
| 126 | + free = true; | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + int plen = Runtime.PyTuple_Size(args); | ||
| 130 | + MethodInfo match = null; | ||
| 131 | + | ||
| 132 | + // XXX: what about out args, etc.? | ||
| 133 | + | ||
| 134 | + for (int i = 0; i < msig.Length; i++) { | ||
| 135 | + ParameterInfo[] pi = msig[i].GetParameters(); | ||
| 136 | + if (pi.Length != plen) { | ||
| 137 | + continue; | ||
| 138 | + } | ||
| 139 | + for (int n = 0; n < pi.Length; n++) { | ||
| 140 | + IntPtr p = Runtime.PyTuple_GetItem(args, n); | ||
| 141 | + if (p == IntPtr.Zero) { | ||
| 142 | + Exceptions.Clear(); | ||
| 143 | + break; | ||
| 144 | + } | ||
| 145 | + ClassBase c = ManagedType.GetManagedObject(p) as ClassBase; | ||
| 146 | + Type t = (c != null) ? c.type : | ||
| 147 | + Converter.GetTypeByAlias(p); | ||
| 148 | + if (t == null) { | ||
| 149 | + break; | ||
| 150 | + } | ||
| 151 | + if (t != pi[n].ParameterType) { | ||
| 152 | + break; | ||
| 153 | + } | ||
| 154 | + } | ||
| 155 | + match = msig[i]; | ||
| 156 | + break; | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + if (free) { | ||
| 160 | + Runtime.Decref(args); | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + return match; | ||
| 164 | + } | ||
| 165 | + | ||
| 117 | 166 | ||
| 118 | 167 | //==================================================================== | |
| 119 | 168 | // Bind the given Python instance and arguments to a particular method | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,7 @@ | |||
| 8 | 8 | // ========================================================================== | |
| 9 | 9 | ||
| 10 | 10 | using System; | |
| 11 | + using System.Reflection; | ||
| 11 | 12 | ||
| 12 | 13 | namespace Python.Runtime { | |
| 13 | 14 | ||
@@ -19,16 +20,37 @@ namespace Python.Runtime { | |||
| 19 | 20 | ||
| 20 | 21 | internal class MethodBinding : ExtensionType { | |
| 21 | 22 | ||
| 23 | + MethodInfo info; | ||
| 22 | 24 | MethodObject m; | |
| 23 | 25 | IntPtr target; | |
| 24 | 26 | ||
| 25 | 27 | public MethodBinding(MethodObject m, IntPtr target) : base() { | |
| 26 | 28 | Runtime.Incref(target); | |
| 27 | 29 | this.target = target; | |
| 30 | + this.info = null; | ||
| 28 | 31 | this.m = m; | |
| 29 | 32 | } | |
| 30 | 33 | ||
| 31 | 34 | ||
| 35 | + //==================================================================== | ||
| 36 | + // Implement explicit overload selection using subscript syntax ([]). | ||
| 37 | + //==================================================================== | ||
| 38 | + | ||
| 39 | + public static IntPtr mp_subscript(IntPtr tp, IntPtr idx) { | ||
| 40 | + MethodBinding self = (MethodBinding)GetManagedObject(tp); | ||
| 41 | + MethodInfo sig = MethodBinder.MatchByTypeSig(self.m.info, idx); | ||
| 42 | + if (sig == null) { | ||
| 43 | + return Exceptions.RaiseTypeError( | ||
| 44 | + "No match found for signature" | ||
| 45 | + ); | ||
| 46 | + } | ||
| 47 | + MethodBinding mb = new MethodBinding(self.m, self.target); | ||
| 48 | + mb.info = sig; | ||
| 49 | + Runtime.Incref(mb.pyHandle); | ||
| 50 | + return mb.pyHandle; | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + | ||
| 32 | 54 | //==================================================================== | |
| 33 | 55 | // MethodBinding __getattribute__ implementation. | |
| 34 | 56 | //==================================================================== | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,6 +17,14 @@ namespace Python.Test { | |||
| 17 | 17 | // Supports CLR generics unit tests. | |
| 18 | 18 | //======================================================================== | |
| 19 | 19 | ||
| 20 | + public class GenericWrapper<T> { | ||
| 21 | + public T value; | ||
| 22 | + | ||
| 23 | + public GenericWrapper(T value) { | ||
| 24 | + this.value = value; | ||
| 25 | + } | ||
| 26 | + } | ||
| 27 | + | ||
| 20 | 28 | public class GenericTypeDefinition<T, U> { | |
| 21 | 29 | public T value1; | |
| 22 | 30 | public U value2; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments