@@ -63,7 +63,7 @@ internal void SetItem(IntPtr inst, IntPtr args) {
6363 }
6464
6565 internal bool NeedsDefaultArgs ( IntPtr args ) {
66- int pynargs = Runtime . PyTuple_Size ( args ) - 1 ;
66+ int pynargs = Runtime . PyTuple_Size ( args ) ;
6767 var methods = SetterBinder . GetMethods ( ) ;
6868 if ( methods . Length == 0 )
6969 return false ;
@@ -72,7 +72,7 @@ internal bool NeedsDefaultArgs(IntPtr args){
7272 ParameterInfo [ ] pi = mi . GetParameters ( ) ;
7373 // need to subtract one for the value
7474 int clrnargs = pi . Length - 1 ;
75- if ( pynargs == clrnargs )
75+ if ( pynargs == clrnargs || pynargs > clrnargs )
7676 return false ;
7777
7878 for ( int v = pynargs ; v < clrnargs ; v ++ ) {
@@ -82,34 +82,30 @@ internal bool NeedsDefaultArgs(IntPtr args){
8282 return true ;
8383 }
8484
85+ /// <summary>
86+ /// This will return default arguments a new instance of a tuple. The size
87+ /// of the tuple will indicate the number of default arguments.
88+ /// </summary>
89+ /// <param name="args">This is pointing to the tuple args passed in</param>
90+ /// <returns>a new instance of the tuple containing the default args</returns>
8591 internal IntPtr GetDefaultArgs ( IntPtr args ) {
86- //IntPtr real = Runtime.PyTuple_New(i + 1);
87- int pynargs = Runtime . PyTuple_Size ( args ) - 1 ;
92+
93+ // if we don't need default args return empty tuple
94+ if ( ! NeedsDefaultArgs ( args ) )
95+ return Runtime . PyTuple_New ( 0 ) ;
96+ int pynargs = Runtime . PyTuple_Size ( args ) ;
97+
98+ // Get the default arg tuple
8899 var methods = SetterBinder . GetMethods ( ) ;
89- IntPtr defaultArgs ;
90- if ( methods . Length == 0 ) {
91- defaultArgs = Runtime . PyTuple_New ( 0 ) ;
92- return defaultArgs ;
93- }
94100 MethodBase mi = methods [ 0 ] ;
95101 ParameterInfo [ ] pi = mi . GetParameters ( ) ;
96102 int clrnargs = pi . Length - 1 ;
97- if ( pynargs == clrnargs || pynargs > clrnargs ) {
98- defaultArgs = Runtime . PyTuple_New ( 0 ) ;
99- return defaultArgs ;
100- }
101-
102- defaultArgs = Runtime . PyTuple_New ( clrnargs - pynargs ) ;
103+ IntPtr defaultArgs = Runtime . PyTuple_New ( clrnargs - pynargs ) ;
103104 for ( int i = 0 ; i < ( clrnargs - pynargs ) ; i ++ ) {
104- // Here we own the reference to the Python value, and we
105- // give the ownership to the arg tuple.
106105 if ( pi [ i + pynargs ] . DefaultValue == DBNull . Value )
107106 continue ;
108107 else {
109108 IntPtr arg = Converter . ToPython ( pi [ i + pynargs ] . DefaultValue , pi [ i + pynargs ] . ParameterType ) ;
110- Type type = typeof ( String ) ;
111- Object arg2 ;
112- Converter . ToManaged ( arg , type , out arg2 , false ) ;
113109 Runtime . PyTuple_SetItem ( defaultArgs , i , arg ) ;
114110 }
115111 }
0 commit comments