11using System ;
22using System . Collections ;
3- using System . Collections . Generic ;
43using System . Globalization ;
5- using System . Reflection ;
64using System . Runtime . InteropServices ;
75using System . Security ;
86using System . ComponentModel ;
@@ -56,13 +54,13 @@ static Converter()
5654
5755 IntPtr dateTimeMod = Runtime . PyImport_ImportModule ( "datetime" ) ;
5856 if ( dateTimeMod == null ) throw new PythonException ( ) ;
59-
57+
6058 decimalCtor = Runtime . PyObject_GetAttrString ( decimalMod , "Decimal" ) ;
6159 if ( decimalCtor == null ) throw new PythonException ( ) ;
62-
60+
6361 dateTimeCtor = Runtime . PyObject_GetAttrString ( dateTimeMod , "datetime" ) ;
6462 if ( dateTimeCtor == null ) throw new PythonException ( ) ;
65-
63+
6664 timeSpanCtor = Runtime . PyObject_GetAttrString ( dateTimeMod , "timedelta" ) ;
6765 if ( timeSpanCtor == null ) throw new PythonException ( ) ;
6866
@@ -225,7 +223,10 @@ internal static IntPtr ToPython(object value, Type type)
225223
226224 IntPtr timeSpanArgs = Runtime . PyTuple_New ( 1 ) ;
227225 Runtime . PyTuple_SetItem ( timeSpanArgs , 0 , Runtime . PyFloat_FromDouble ( timespan . TotalDays ) ) ;
228- return Runtime . PyObject_CallObject ( timeSpanCtor , timeSpanArgs ) ;
226+ var returnTimeSpan = Runtime . PyObject_CallObject ( timeSpanCtor , timeSpanArgs ) ;
227+ // clean up
228+ Runtime . XDecref ( timeSpanArgs ) ;
229+ return returnTimeSpan ;
229230 }
230231 return CLRObject . GetInstHandle ( value , type ) ;
231232
@@ -284,12 +285,14 @@ internal static IntPtr ToPython(object value, Type type)
284285 IntPtr d2p = Runtime . PyString_FromString ( d2s ) ;
285286 IntPtr decimalArgs = Runtime . PyTuple_New ( 1 ) ;
286287 Runtime . PyTuple_SetItem ( decimalArgs , 0 , d2p ) ;
287-
288- return Runtime . PyObject_CallObject ( decimalCtor , decimalArgs ) ;
288+ var returnDecimal = Runtime . PyObject_CallObject ( decimalCtor , decimalArgs ) ;
289+ // clean up
290+ Runtime . XDecref ( decimalArgs ) ;
291+ return returnDecimal ;
289292
290293 case TypeCode . DateTime :
291294 var datetime = ( DateTime ) value ;
292-
295+
293296 IntPtr dateTimeArgs = Runtime . PyTuple_New ( 8 ) ;
294297 Runtime . PyTuple_SetItem ( dateTimeArgs , 0 , Runtime . PyInt_FromInt32 ( datetime . Year ) ) ;
295298 Runtime . PyTuple_SetItem ( dateTimeArgs , 1 , Runtime . PyInt_FromInt32 ( datetime . Month ) ) ;
@@ -299,8 +302,10 @@ internal static IntPtr ToPython(object value, Type type)
299302 Runtime . PyTuple_SetItem ( dateTimeArgs , 5 , Runtime . PyInt_FromInt32 ( datetime . Second ) ) ;
300303 Runtime . PyTuple_SetItem ( dateTimeArgs , 6 , Runtime . PyInt_FromInt32 ( datetime . Millisecond ) ) ;
301304 Runtime . PyTuple_SetItem ( dateTimeArgs , 7 , TzInfo ( datetime . Kind ) ) ;
302-
303- return Runtime . PyObject_CallObject ( dateTimeCtor , dateTimeArgs ) ;
305+ var returnDateTime = Runtime . PyObject_CallObject ( dateTimeCtor , dateTimeArgs ) ;
306+ // clean up
307+ Runtime . XDecref ( dateTimeArgs ) ;
308+ return returnDateTime ;
304309
305310 default :
306311 if ( value is IEnumerable )
@@ -330,7 +335,9 @@ private static IntPtr TzInfo(DateTimeKind kind)
330335 IntPtr tzInfoArgs = Runtime . PyTuple_New ( 2 ) ;
331336 Runtime . PyTuple_SetItem ( tzInfoArgs , 0 , Runtime . PyFloat_FromDouble ( offset . Hours ) ) ;
332337 Runtime . PyTuple_SetItem ( tzInfoArgs , 1 , Runtime . PyFloat_FromDouble ( offset . Minutes ) ) ;
333- return Runtime . PyObject_CallObject ( tzInfoCtor , tzInfoArgs ) ;
338+ var returnValue = Runtime . PyObject_CallObject ( tzInfoCtor , tzInfoArgs ) ;
339+ Runtime . XDecref ( tzInfoArgs ) ;
340+ return returnValue ;
334341 }
335342
336343
0 commit comments