| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6d91403 commit 04cd4da
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -51,7 +51,21 @@ public static int tp_init(IntPtr ob, IntPtr args, IntPtr kw) { | |||
| 51 | 51 | //==================================================================== | |
| 52 | 52 | ||
| 53 | 53 | public virtual IntPtr type_subscript(IntPtr idx) { | |
| 54 | - return Exceptions.RaiseTypeError("unsubscriptable object"); | ||
| 54 | + Type[] types = Runtime.PythonArgsToTypeArray(idx); | ||
| 55 | + if (types == null) { | ||
| 56 | + return Exceptions.RaiseTypeError("type(s) expected"); | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + Type target = GenericUtil.GenericForType(this.type, types.Length); | ||
| 60 | + | ||
| 61 | + if (target != null) { | ||
| 62 | + Type t = target.MakeGenericType(types); | ||
| 63 | + ManagedType c = (ManagedType)ClassManager.GetClass(t); | ||
| 64 | + Runtime.Incref(c.pyHandle); | ||
| 65 | + return c.pyHandle; | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + return Exceptions.RaiseTypeError("no type matches params"); | ||
| 55 | 69 | } | |
| 56 | 70 | ||
| 57 | 71 | //==================================================================== | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -44,57 +44,6 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw) { | |||
| 44 | 44 | "object is not callable"); | |
| 45 | 45 | return IntPtr.Zero; | |
| 46 | 46 | } | |
| 47 | - | ||
| 48 | - //==================================================================== | ||
| 49 | - // Implements subscript syntax for reflected generic types. A closed | ||
| 50 | - // type is created by binding the generic type via subscript syntax: | ||
| 51 | - // inst = List[str]() | ||
| 52 | - //==================================================================== | ||
| 53 | - | ||
| 54 | - public override IntPtr type_subscript(IntPtr idx) { | ||
| 55 | - Type[] types = Runtime.PythonArgsToTypeArray(idx); | ||
| 56 | - if (types == null) { | ||
| 57 | - return Exceptions.RaiseTypeError("type(s) expected"); | ||
| 58 | - } | ||
| 59 | - if (!this.type.IsGenericTypeDefinition) { | ||
| 60 | - return Exceptions.RaiseTypeError( | ||
| 61 | - "type is not a generic type definition" | ||
| 62 | - ); | ||
| 63 | - } | ||
| 64 | - | ||
| 65 | - // This is a little tricky, because an instance of GenericType | ||
| 66 | - // may represent either a specific generic type, or act as an | ||
| 67 | - // alias for one or more generic types with the same base name. | ||
| 68 | - | ||
| 69 | - if (this.type.ContainsGenericParameters) { | ||
| 70 | - Type[] args = this.type.GetGenericArguments(); | ||
| 71 | - Type target = null; | ||
| 72 | - | ||
| 73 | - if (args.Length == types.Length) { | ||
| 74 | - target = this.type; | ||
| 75 | - } | ||
| 76 | - else { | ||
| 77 | - foreach (Type t in | ||
| 78 | - GenericUtil.GenericsForType(this.type)) { | ||
| 79 | - if (t.GetGenericArguments().Length == types.Length) { | ||
| 80 | - target = t; | ||
| 81 | - break; | ||
| 82 | - } | ||
| 83 | - } | ||
| 84 | - } | ||
| 85 | - | ||
| 86 | - if (target != null) { | ||
| 87 | - Type t = target.MakeGenericType(types); | ||
| 88 | - ManagedType c = (ManagedType)ClassManager.GetClass(t); | ||
| 89 | - Runtime.Incref(c.pyHandle); | ||
| 90 | - return c.pyHandle; | ||
| 91 | - } | ||
| 92 | - return Exceptions.RaiseTypeError("no type matches params"); | ||
| 93 | - } | ||
| 94 | - | ||
| 95 | - return Exceptions.RaiseTypeError("unsubscriptable object"); | ||
| 96 | - } | ||
| 97 | - | ||
| 98 | 47 | } | |
| 99 | 48 | ||
| 100 | 49 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -81,14 +81,33 @@ public static List<string> GetGenericBaseNames(string ns) { | |||
| 81 | 81 | // xxx | |
| 82 | 82 | //==================================================================== | |
| 83 | 83 | ||
| 84 | - public static List<Type> GenericsForType(Type t) { | ||
| 84 | + public static Type GenericForType(Type t, int paramCount) | ||
| 85 | + { | ||
| 86 | + return GenericByName(t.Namespace, t.Name, paramCount); | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + public static Type GenericByName(string ns, string name, int paramCount) | ||
| 90 | + { | ||
| 91 | + foreach (Type t in GenericsByName(ns, name)) | ||
| 92 | + { | ||
| 93 | + if (t.GetGenericArguments().Length == paramCount) | ||
| 94 | + return t; | ||
| 95 | + } | ||
| 96 | + return null; | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + public static List<Type> GenericsForType(Type t) | ||
| 100 | + { | ||
| 101 | + return GenericsByName(t.Namespace, t.Name); | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + public static List<Type> GenericsByName(string ns, string basename) { | ||
| 85 | 105 | Dictionary<string, List<string>> nsmap = null; | |
| 86 | - mapping.TryGetValue(t.Namespace, out nsmap); | ||
| 106 | + mapping.TryGetValue(ns, out nsmap); | ||
| 87 | 107 | if (nsmap == null) { | |
| 88 | 108 | return null; | |
| 89 | 109 | } | |
| 90 | 110 | ||
| 91 | - string basename = t.Name; | ||
| 92 | 111 | int tick = basename.IndexOf("`"); | |
| 93 | 112 | if (tick > -1) { | |
| 94 | 113 | basename = basename.Substring(0, tick); | |
@@ -102,7 +121,7 @@ public static List<Type> GenericsForType(Type t) { | |||
| 102 | 121 | ||
| 103 | 122 | List<Type> result = new List<Type>(); | |
| 104 | 123 | foreach (string name in names) { | |
| 105 | - string qname = t.Namespace + "." + name; | ||
| 124 | + string qname = ns + "." + name; | ||
| 106 | 125 | Type o = AssemblyManager.LookupType(qname); | |
| 107 | 126 | if (o != null) { | |
| 108 | 127 | result.Add(o); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments