@@ -22,9 +22,10 @@ namespace Python.Runtime {
2222 internal class ClassObject : ClassBase {
2323
2424 ConstructorBinder binder ;
25+ ConstructorInfo [ ] ctors ;
2526
2627 internal ClassObject ( Type tp ) : base ( tp ) {
27- ConstructorInfo [ ] ctors = type . GetConstructors ( ) ;
28+ ctors = type . GetConstructors ( ) ;
2829 binder = new ConstructorBinder ( ) ;
2930
3031 for ( int i = 0 ; i < ctors . Length ; i ++ ) {
@@ -125,28 +126,49 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw) {
125126
126127
127128 //====================================================================
128- // Implementation of [] semantics for reflected types. This mainly
129- // exists to implement the Array[int] syntax for creating arrays.
129+ // Implementation of [] semantics for reflected types. This exists
130+ // both to implement the Array[int] syntax for creating arrays and
131+ // to support generic name overload resolution using [].
130132 //====================================================================
131133
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 ;
134+ public override IntPtr type_subscript ( IntPtr idx ) {
135+
136+ // If this type is the Array type, the [<type>] means we need to
137+ // construct and return an array type of the given element type.
138+
139+ if ( ( this . type ) == typeof ( Array ) ) {
140+ if ( Runtime . PyTuple_Check ( idx ) ) {
141+ return Exceptions . RaiseTypeError ( "type expected" ) ;
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+ ClassBase o = ClassManager . GetClass ( a . GetType ( ) ) ;
150+ Runtime . Incref ( o . pyHandle ) ;
151+ return o . pyHandle ;
152+ }
153+
154+ // If there are generics in our namespace with the same base name
155+ // as the current type, then [<type>] means the caller wants to
156+ // bind the generic type matching the given type parameters.
157+
158+ Type [ ] types = Runtime . PythonArgsToTypeArray ( idx ) ;
159+ if ( types == null ) {
160+ return Exceptions . RaiseTypeError ( "type(s) expected" ) ;
161+ }
162+
163+ string gname = this . type . FullName + "`" + types . Length . ToString ( ) ;
164+ Type gtype = AssemblyManager . LookupType ( gname ) ;
165+ if ( gtype != null ) {
166+ GenericType g = ClassManager . GetClass ( gtype ) as GenericType ;
167+ return g . type_subscript ( idx ) ;
168+ Runtime . Incref ( g . pyHandle ) ;
169+ return g . pyHandle ;
170+ }
171+ return Exceptions . RaiseTypeError ( "unsubscriptable object" ) ;
150172 }
151173
152174
@@ -205,7 +227,7 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v) {
205227
206228 if ( cls . indexer == null || ! cls . indexer . CanSet ) {
207229 Exceptions . SetError ( Exceptions . TypeError ,
208- "object doesn't support item assignment x "
230+ "object doesn't support item assignment"
209231 ) ;
210232 return - 1 ;
211233 }
0 commit comments