@@ -31,21 +31,76 @@ public MethodBinding(MethodObject m, IntPtr target) : base() {
3131 this . m = m ;
3232 }
3333
34+ //====================================================================
35+ // Given a sequence of MethodInfo and a sequence of types, return the
36+ // MethodInfo that matches the signature represented by those types.
37+ //====================================================================
38+
39+ internal static MethodInfo MatchSignature ( MethodInfo [ ] mi , Type [ ] tp ) {
40+ int count = tp . Length ;
41+ for ( int i = 0 ; i < mi . Length ; i ++ ) {
42+ ParameterInfo [ ] pi = mi [ i ] . GetParameters ( ) ;
43+ if ( pi . Length != count ) {
44+ continue ;
45+ }
46+ for ( int n = 0 ; n < pi . Length ; n ++ ) {
47+ if ( tp [ n ] != pi [ n ] . ParameterType ) {
48+ break ;
49+ }
50+ if ( n == ( pi . Length - 1 ) ) {
51+ return mi [ i ] ;
52+ }
53+ }
54+ }
55+ return null ;
56+ }
57+
58+ //====================================================================
59+ // Given a sequence of MethodInfo and a sequence of type parameters,
60+ // return the MethodInfo that represents the matching closed generic.
61+ //====================================================================
3462
63+ internal static MethodInfo MatchParameters ( MethodInfo [ ] mi , Type [ ] tp ) {
64+ int count = tp . Length ;
65+ for ( int i = 0 ; i < mi . Length ; i ++ ) {
66+ if ( ! mi [ i ] . IsGenericMethodDefinition ) {
67+ continue ;
68+ }
69+ Type [ ] args = mi [ 0 ] . GetGenericArguments ( ) ;
70+ if ( args . Length != count ) {
71+ continue ;
72+ }
73+ return mi [ i ] . MakeGenericMethod ( tp ) ;
74+ }
75+ return null ;
76+ }
77+
3578 //====================================================================
3679 // Implement explicit overload selection using subscript syntax ([]).
3780 //====================================================================
3881
3982 public static IntPtr mp_subscript ( IntPtr tp , IntPtr idx ) {
4083 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- ) ;
84+
85+ // Note: if the type provides a non-generic method with N args
86+ // and a generic method that takes N params, then we always
87+ // prefer the non-generic version in doing overload selection.
88+
89+ Type [ ] types = Runtime . PythonArgsToTypeArray ( idx ) ;
90+ if ( types == null ) {
91+ return Exceptions . RaiseTypeError ( "type(s) expected" ) ;
92+ }
93+
94+ MethodInfo mi = MatchSignature ( self . m . info , types ) ;
95+ if ( mi == null ) {
96+ mi = MatchParameters ( self . m . info , types ) ;
97+ if ( mi == null ) {
98+ return Exceptions . RaiseTypeError ( "No match found" ) ;
99+ }
46100 }
101+
47102 MethodBinding mb = new MethodBinding ( self . m , self . target ) ;
48- mb . info = sig ;
103+ mb . info = mi ;
49104 Runtime . Incref ( mb . pyHandle ) ;
50105 return mb . pyHandle ;
51106 }
@@ -137,7 +192,6 @@ public static IntPtr tp_hash(IntPtr ob) {
137192 return new IntPtr ( x ) ;
138193 }
139194
140-
141195 //====================================================================
142196 // MethodBinding __repr__ implementation.
143197 //====================================================================
0 commit comments