8000 Move the type subscript implementation into ClassBase. · pythonnet/pythonnet@04cd4da · GitHub
[go: up one dir, main page]

Skip to content

Commit 04cd4da

Browse files
committed
Move the type subscript implementation into ClassBase.
This fixes issues #157 and #128 by allowing the use of the type subscript operator on all types instead of just generic types, preventing the shadowing of generic types by a non-generic one like System.Action or System.EventHandler.
1 parent 6d91403 commit 04cd4da

File tree

3 files changed

+38
-56
lines changed

3 files changed

+38
-56
lines changed

src/runtime/classbase.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,21 @@ public static int tp_init(IntPtr ob, IntPtr args, IntPtr kw) {
5151
//====================================================================
5252

5353
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");
5569
}
5670

5771
//====================================================================

src/runtime/generictype.cs

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -44,57 +44,6 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw) {
4444
"object is not callable");
4545
return IntPtr.Zero;
4646
}
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-
9847
}
9948

10049
}

src/runtime/genericutil.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,33 @@ public static List<string> GetGenericBaseNames(string ns) {
8181
// xxx
8282
/ 6DB6 /====================================================================
8383

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) {
85105
Dictionary<string, List<string>> nsmap = null;
86-
mapping.TryGetValue(t.Namespace, out nsmap);
106+
mapping.TryGetValue(ns, out nsmap);
87107
if (nsmap == null) {
88108
return null;
89109
}
90110

91-
string basename = t.Name;
92111
int tick = basename.IndexOf("`");
93112
if (tick > -1) {
94113
basename = basename.Substring(0, tick);
@@ -102,7 +121,7 @@ public static List<Type> GenericsForType(Type t) {
102121

103122
List<Type> result = new List<Type>();
104123
foreach (string name in names) {
105-
string qname = t.Namespace + "." + name;
124+
string qname = ns + "." + name;
106125
Type o = AssemblyManager.LookupType(qname);
107126
if (o != null) {
108127
result.Add(o);

0 commit comments

Comments
 (0)
0