@@ -63,23 +63,26 @@ public static IntPtr mp_subscript(IntPtr tp, IntPtr idx)
63
63
PyObject Singature {
64
64
get {
65
65
var infos = this . info == null ? this . m . info : new [ ] { this . info } ;
66
+ var type = infos . Select ( i => i . DeclaringType )
67
+ . OrderByDescending ( t => t , new TypeSpecificityComparer ( ) )
68
+ . First ( ) ;
69
+ infos = infos . Where ( info => info . DeclaringType == type ) . ToArray ( ) ;
66
70
// this is a primitive version
67
71
// the overload with the maximum number of parameters should be used
68
- var primary = infos . First ( ) ;
72
+ var primary = infos . OrderByDescending ( i => i . GetParameters ( ) . Length ) . First ( ) ;
69
73
var primaryParameters = primary . GetParameters ( ) ;
70
74
PyObject signatureClass = Runtime . InspectModule . GetAttr ( "Signature" ) ;
71
75
var primaryReturn = primary . ReturnParameter ;
72
- if ( infos . Any ( i => i . GetParameters ( ) . Length != primaryParameters . Length
73
- || i . ReturnParameter ? . ParameterType != primaryReturn ? . ParameterType ) ) {
74
- return signatureClass . Invoke ( ) ;
75
- }
76
76
77
77
var parameters = new PyList ( ) ;
78
78
var parameterClass = primaryParameters . Length > 0 ? Runtime . InspectModule . GetAttr ( "Parameter" ) : null ;
79
79
var positionalOrKeyword = primaryParameters . Length > 0 ? parameterClass . GetAttr ( "POSITIONAL_OR_KEYWORD" ) : null ;
80
80
for ( int i = 0 ; i < primaryParameters . Length ; i ++ ) {
81
81
var parameter = primaryParameters [ i ] ;
82
- var alternatives = infos . Select ( info => info . GetParameters ( ) [ i ] ) ;
82
+ var alternatives = infos . Select ( info => {
83
+ ParameterInfo [ ] altParamters = info . GetParameters ( ) ;
84
+ return i < altParamters . Length ? altParamters [ i ] : null ;
85
+ } ) . Where ( p => p != null ) ;
83
86
var defaultValue = alternatives
84
87
. Select ( alternative => alternative . DefaultValue != DBNull . Value ? alternative . DefaultValue . ToPython ( ) : null )
85
88
. FirstOrDefault ( v => v != null ) ?? parameterClass . GetAttr ( "empty" ) ;
@@ -102,6 +105,15 @@ PyObject Singature {
102
105
}
103
106
}
104
107
108
+ struct TypeSpecificityComparer : IComparer < Type > {
109
+ public int Compare ( Type a , Type b ) {
110
+ if ( a == b ) return 0 ;
111
+ if ( a . IsSubclassOf ( b ) ) return 1 ;
112
+ if ( b . IsSubclassOf ( a ) ) return - 1 ;
113
+ throw new NotSupportedException ( ) ;
114
+ }
115
+ }
116
+
105
117
/// <summary>
106
118
/// MethodBinding __getattribute__ implementation.
107
119
/// </summary>
@@ -130,6 +142,11 @@ public static IntPtr tp_getattro(IntPtr ob, IntPtr key)
130
142
case "__signature__" :
131
143
var sig = self . Singature ;
132
144
return sig . Handle ;
145
+ case "__name__" :
146
+ var pyName = self . m . GetName ( ) ;
147
+ return pyName == IntPtr . Zero
148
+ ? IntPtr . Zero
149
+ : Runtime . SelfIncRef ( pyName ) ;
133
150
default :
134
151
return Runtime . PyObject_GenericGetAttr ( ob , key ) ;
135
152
}
0 commit comments