| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 804cd51 commit 4e2a3bd
15 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,7 +29,18 @@ internal override bool CanSubclass() { | |||
| 29 | 29 | } | |
| 30 | 30 | ||
| 31 | 31 | [CallConvCdecl()] | |
| 32 | - public static IntPtr tp_new(IntPtr ob, IntPtr args, IntPtr kw) { | ||
| 32 | + public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw) { | ||
| 33 | + ArrayObject self = GetManagedObject(tp) as ArrayObject; | ||
| 34 | + if (Runtime.PyTuple_Size(args) != 1) { | ||
| 35 | + return Exceptions.RaiseTypeError("array expects 1 argument"); | ||
| 36 | + } | ||
| 37 | + IntPtr op = Runtime.PyTuple_GetItem(args, 0); | ||
| 38 | + Object result; | ||
| 39 | + | ||
| 40 | + if (!Converter.ToManaged(op, self.type, out result, true)) { | ||
| 41 | + return IntPtr.Zero; | ||
| 42 | + } | ||
| 43 | + return CLRObject.GetInstHandle(result, tp); | ||
| 33 | 44 | string message = "cannot instantiate array wrapper"; | |
| 34 | 45 | return Exceptions.RaiseTypeError(message); | |
| 35 | 46 | } | |
| 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 | |
|---|---|---|---|
@@ -126,6 +126,32 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw) { | |||
| 126 | 126 | } | |
| 127 | 127 | ||
| 128 | 128 | ||
| 129 | + //==================================================================== | ||
| 130 | + // Implementation of [] semantics for reflected types. This mainly | ||
| 131 | + // exists to implement the Array[int] syntax for creating arrays. | ||
| 132 | + //==================================================================== | ||
| 133 | + | ||
| 134 | + public override IntPtr type_subscript(IntPtr ob, IntPtr idx) { | ||
| 135 | + if ((this.type) != typeof(Array)) { | ||
| 136 | + return Exceptions.RaiseTypeError("unsubscriptable object"); | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + if (Runtime.PyTuple_Check(idx)) { | ||
| 140 | + return Exceptions.RaiseTypeError("expected single type"); | ||
| 141 | + } | ||
| 142 | + | ||
| 143 | + ClassBase c = GetManagedObject(idx) as ClassBase; | ||
| 144 | + Type t = (c != null) ? c.type : Converter.GetTypeByAlias(idx); | ||
| 145 | + if (t == null) { | ||
| 146 | + return Exceptions.RaiseTypeError("type expected"); | ||
| 147 | + } | ||
| 148 | + Array a = Array.CreateInstance(t, 0); | ||
| 149 | + c = ClassManager.GetClass(a.GetType()); | ||
| 150 | + Runtime.Incref(c.pyHandle); | ||
| 151 | + return c.pyHandle; | ||
| 152 | + } | ||
| 153 | + | ||
| 154 | + | ||
| 129 | 155 | //==================================================================== | |
| 130 | 156 | // Implements __getitem__ for reflected classes and value types. | |
| 131 | 157 | //==================================================================== | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -33,7 +33,12 @@ internal ConstructorBinder () : base() {} | |||
| 33 | 33 | // Python type to use when wrapping the result (may be a subclass). | |
| 34 | 34 | //==================================================================== | |
| 35 | 35 | ||
| 36 | - internal object InvokeRaw(IntPtr inst, IntPtr args, IntPtr kw) { | ||
| 36 | + internal object InvokeRaw(IntPtr inst, IntPtr args, IntPtr kw) { | ||
| 37 | + return this.InvokeRaw(inst, args, kw, null); | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + internal object InvokeRaw(IntPtr inst, IntPtr args, IntPtr kw, | ||
| 41 | + MethodBase info) { | ||
| 37 | 42 | Binding binding = this.Bind(inst, args, kw); | |
| 38 | 43 | Object result; | |
| 39 | 44 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,6 +28,7 @@ private Converter() {} | |||
| 28 | 28 | static NumberFormatInfo nfi; | |
| 29 | 29 | static Type objectType; | |
| 30 | 30 | static Type stringType; | |
| 31 | + static Type doubleType; | ||
| 31 | 32 | static Type int32Type; | |
| 32 | 33 | static Type int64Type; | |
| 33 | 34 | static Type flagsType; | |
@@ -40,12 +41,38 @@ static Converter () { | |||
| 40 | 41 | stringType = typeof(String); | |
| 41 | 42 | int32Type = typeof(Int32); | |
| 42 | 43 | int64Type = typeof(Int64); | |
| 44 | + doubleType = typeof(Double); | ||
| 43 | 45 | flagsType = typeof(FlagsAttribute); | |
| 44 | 46 | boolType = typeof(Boolean); | |
| 45 | 47 | typeType = typeof(Type); | |
| 46 | 48 | } | |
| 47 | 49 | ||
| 48 | 50 | ||
| 51 | + //==================================================================== | ||
| 52 | + // Given a builtin Python type, return the corresponding CLR type. | ||
| 53 | + //==================================================================== | ||
| 54 | + | ||
| 55 | + internal static Type GetTypeByAlias(IntPtr op) { | ||
| 56 | + if ((op == Runtime.PyStringType) || | ||
| 57 | + (op == Runtime.PyUnicodeType)) { | ||
| 58 | + return stringType; | ||
| 59 | + } | ||
| 60 | + else if (op == Runtime.PyIntType) { | ||
| 61 | + return int32Type; | ||
| 62 | + } | ||
| 63 | + else if (op == Runtime.PyLongType) { | ||
| 64 | + return int64Type; | ||
| 65 | + } | ||
| 66 | + else if (op == Runtime.PyFloatType) { | ||
| 67 | + return doubleType; | ||
| 68 | + } | ||
| 69 | + else if (op == Runtime.PyBoolType) { | ||
| 70 | + return boolType; | ||
| 71 | + } | ||
| 72 | + return null; | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + | ||
| 49 | 76 | //==================================================================== | |
| 50 | 77 | // Return a Python object for the given native object, converting | |
| 51 | 78 | // basic types (string, int, etc.) into equivalent Python objects. | |
@@ -177,7 +204,6 @@ internal static bool ToManaged(IntPtr value, Type type, | |||
| 177 | 204 | ||
| 178 | 205 | internal static bool ToManagedValue(IntPtr value, Type obType, | |
| 179 | 206 | out Object result, bool setError) { | |
| 180 | - | ||
| 181 | 207 | // Common case: if the Python value is a wrapped managed object | |
| 182 | 208 | // instance, just return the wrapped object. | |
| 183 | 209 | ManagedType mt = ManagedType.GetManagedObject(value); | |
@@ -199,6 +225,12 @@ internal static bool ToManagedValue(IntPtr value, Type obType, | |||
| 199 | 225 | mt = ManagedType.GetManagedObject(value); | |
| 200 | 226 | } | |
| 201 | 227 | } | |
| 228 | + IntPtr c = Exceptions.UnwrapExceptionClass(value); | ||
| 229 | + if ((c != IntPtr.Zero) && (c != value)) { | ||
| 230 | + value = c; | ||
| 231 | + Runtime.Decref(c); | ||
| 232 | + mt = ManagedType.GetManagedObject(value); | ||
| 233 | + } | ||
| 202 | 234 | } | |
| 203 | 235 | } | |
| 204 | 236 | ||
@@ -601,6 +633,8 @@ static bool ToArray(IntPtr value, Type obType, out Object result, | |||
| 601 | 633 | ||
| 602 | 634 | Array items = Array.CreateInstance(elementType, size); | |
| 603 | 635 | ||
| 636 | + // XXX - is there a better way to unwrap this if it is a real | ||
| 637 | + // array? | ||
| 604 | 638 | for (int i = 0; i < size; i++) { | |
| 605 | 639 | Object obj = null; | |
| 606 | 640 | IntPtr item = Runtime.PySequence_GetItem(value, i); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -192,6 +192,23 @@ internal static IntPtr GetExceptionInstanceWrapper(IntPtr real) { | |||
| 192 | 192 | return op; | |
| 193 | 193 | } | |
| 194 | 194 | ||
| 195 | + internal static IntPtr UnwrapExceptionClass(IntPtr op) { | ||
| 196 | + // In some cases its necessary to recognize an exception *class*, | ||
| 197 | + // and obtain the inner (wrapped) exception class. This method | ||
| 198 | + // returns the inner class if found, or a null pointer. | ||
| 199 | + | ||
| 200 | + IntPtr d = Runtime.PyObject_GetAttrString(op, "__dict__"); | ||
| 201 | + if (d == IntPtr.Zero) { | ||
| 202 | + Exceptions.Clear(); | ||
| 203 | + return IntPtr.Zero; | ||
| 204 | + } | ||
| 205 | + IntPtr c = Runtime.PyDict_GetItemString(d, "_class"); | ||
| 206 | + Runtime.Decref(d); | ||
| 207 | + if (c == IntPtr.Zero) { | ||
| 208 | + Exceptions.Clear(); | ||
| 209 | + } | ||
| 210 | + return c; | ||
| 211 | + } | ||
| 195 | 212 | ||
| 196 | 213 | ||
| 197 | 214 | /// <summary> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -46,6 +46,18 @@ internal static ManagedType GetManagedObject(IntPtr ob) { | |||
| 46 | 46 | GCHandle gc = (GCHandle)op; | |
| 47 | 47 | return (ManagedType)gc.Target; | |
| 48 | 48 | } | |
| 49 | + | ||
| 50 | + // In certain situations, we need to recognize a wrapped | ||
| 51 | + // exception class and be willing to unwrap the class :( | ||
| 52 | + | ||
| 53 | + if (Runtime.wrap_exceptions) { | ||
| 54 | + IntPtr e = Exceptions.UnwrapExceptionClass(ob); | ||
| 55 | + if ((e != IntPtr.Zero) && (e != ob)) { | ||
| 56 | + ManagedType m = GetManagedObject(e); | ||
| 57 | + Runtime.Decref(e); | ||
| 58 | + return m; | ||
| 59 | + } | ||
| 60 | + } | ||
| 49 | 61 | } | |
| 50 | 62 | return null; | |
| 51 | 63 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -220,6 +220,20 @@ public static int tp_setattro(IntPtr tp, IntPtr name, IntPtr value) { | |||
| 220 | 220 | return 0; | |
| 221 | 221 | } | |
| 222 | 222 | ||
| 223 | + //==================================================================== | ||
| 224 | + // The metatype has to implement [] semantics for generic types, so | ||
| 225 | + // here we just delegate to the generic type def implementation. Its | ||
| 226 | + // own mp_subscript | ||
| 227 | + //==================================================================== | ||
| 228 | + | ||
| 229 | + [CallConvCdecl()] | ||
| 230 | + public static IntPtr mp_subscript(IntPtr tp, IntPtr idx) { | ||
| 231 | + ClassBase cb = GetManagedObject(tp) as ClassBase; | ||
| 232 | + if (cb != null) { | ||
| 233 | + return cb.type_subscript(tp, idx); | ||
| 234 | + } | ||
| 235 | + return Exceptions.RaiseTypeError("unsubscriptable object"); | ||
| 236 | + } | ||
| 223 | 237 | ||
| 224 | 238 | //==================================================================== | |
| 225 | 239 | // Dealloc implementation. This is called when a Python type generated | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -114,6 +114,60 @@ 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 | + bool matched = true; | ||
| 140 | + for (int n = 0; n < pi.Length; n++) { | ||
| 141 | + IntPtr p = Runtime.PyTuple_GetItem(args, n); | ||
| 142 | + if (p == IntPtr.Zero) { | ||
| 143 | + Exceptions.Clear(); | ||
| 144 | + break; | ||
| 145 | + } | ||
| 146 | + ClassBase c = ManagedType.GetManagedObject(p) as ClassBase; | ||
| 147 | + Type t = (c != null) ? c.type : | ||
| 148 | + Converter.GetTypeByAlias(p); | ||
| 149 | + | ||
| 150 | + if (t == null) { | ||
| 151 | + break; | ||
| 152 | + } | ||
| 153 | + if (t != pi[n].ParameterType) { | ||
| 154 | + matched = false; | ||
| 155 | + break; | ||
| 156 | + } | ||
| 157 | + } | ||
| 158 | + if (matched) { | ||
| 159 | + match = msig[i]; | ||
| 160 | + break; | ||
| 161 | + } | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + if (free) { | ||
| 165 | + Runtime.Decref(args); | ||
| 166 | + } | ||
| 167 | + | ||
| 168 | + return match; | ||
| 169 | + } | ||
| 170 | + | ||
| 117 | 171 | ||
| 118 | 172 | //==================================================================== | |
| 119 | 173 | // Bind the given Python instance and arguments to a particular method | |
@@ -122,12 +176,26 @@ internal static int ArgPrecedence(Type t) { | |||
| 122 | 176 | //==================================================================== | |
| 123 | 177 | ||
| 124 | 178 | internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw) { | |
| 179 | + return this.Bind(inst, args, kw, null); | ||
| 180 | + } | ||
| 181 | + | ||
| 182 | + internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, | ||
| 183 | + MethodBase info) { | ||
| 125 | 184 | // loop to find match, return invoker w/ or /wo error | |
| 185 | + MethodBase[] _methods = null; | ||
| 126 | 186 | int nargs = Runtime.PyTuple_Size(args); | |
| 127 | 187 | object arg; | |
| 128 | 188 | ||
| 129 | - MethodBase[] _methods = GetMethods(); | ||
| 130 | - | ||
| 189 | + if (info != null) { | ||
| 190 | + _methods = (MethodBase[])Array.CreateInstance( | ||
| 191 | + typeof(MethodBase), 1 | ||
| 192 | + ); | ||
| 193 | + _methods.SetValue(info, 0); | ||
| 194 | + } | ||
| 195 | + else { | ||
| 196 | + _methods = GetMethods(); | ||
| 197 | + } | ||
| 198 | + | ||
| 131 | 199 | for (int i = 0; i < _methods.Length; i++) { | |
| 132 | 200 | MethodBase mi = _methods[i]; | |
| 133 | 201 | ParameterInfo[] pi = mi.GetParameters(); | |
@@ -171,9 +239,14 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw) { | |||
| 171 | 239 | return null; | |
| 172 | 240 | } | |
| 173 | 241 | ||
| 174 | - | ||
| 175 | 242 | internal virtual IntPtr Invoke(IntPtr inst, IntPtr args, IntPtr kw) { | |
| 176 | - Binding binding = this.Bind(inst, args, kw); | ||
| 243 | + return this.Invoke(inst, args, kw, null); | ||
| 244 | + | ||
| 245 | + } | ||
| 246 | + | ||
| 247 | + internal virtual IntPtr Invoke(IntPtr inst, IntPtr args, IntPtr kw, | ||
| 248 | + MethodBase info) { | ||
| 249 | + Binding binding = this.Bind(inst, args, kw, info); | ||
| 177 | 250 | Object result; | |
| 178 | 251 | ||
| 179 | 252 | if (binding == null) { | |
| 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,38 @@ 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 | + [CallConvCdecl()] | ||
| 40 | + public static IntPtr mp_subscript(IntPtr tp, IntPtr idx) { | ||
| 41 | + MethodBinding self = (MethodBinding)GetManagedObject(tp); | ||
| 42 | + MethodInfo sig = MethodBinder.MatchByTypeSig(self.m.info, idx); | ||
| 43 | + if (sig == null) { | ||
| 44 | + return Exceptions.RaiseTypeError( | ||
| 45 | + "No match found for signature" | ||
| 46 | + ); | ||
| 47 | + } | ||
| 48 | + MethodBinding mb = new MethodBinding(self.m, self.target); | ||
| 49 | + mb.info = sig; | ||
| 50 | + Runtime.Incref(mb.pyHandle); | ||
| 51 | + return mb.pyHandle; | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + | ||
| 32 | 55 | //==================================================================== | |
| 33 | 56 | // MethodBinding __getattribute__ implementation. | |
| 34 | 57 | //==================================================================== | |
@@ -77,12 +100,13 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw) { | |||
| 77 | 100 | IntPtr uargs = Runtime.PyTuple_GetSlice(args, 1, len); | |
| 78 | 101 | IntPtr inst = Runtime.PyTuple_GetItem(args, 0); | |
| 79 | 102 | Runtime.Incref(inst); | |
| 80 | - IntPtr r = self.m.Invoke(inst, uargs, kw); | ||
| 103 | + IntPtr r = self.m.Invoke(inst, uargs, kw, self.info); | ||
| 81 | 104 | Runtime.Decref(inst); | |
| 105 | + Runtime.Decref(uargs); | ||
| 82 | 106 | return r; | |
| 83 | 107 | } | |
| 84 | 108 | ||
| 85 | - return self.m.Invoke(self.target, args, kw); | ||
| 109 | + return self.m.Invoke(self.target, args, kw, self.info); | ||
| 86 | 110 | } | |
| 87 | 111 | ||
| 88 | 112 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments