11using System ;
22using System . Collections ;
3- using System . Collections . Generic ;
43using System . Globalization ;
5- using System . Reflection ;
64using System . Runtime . InteropServices ;
75using System . Security ;
86
@@ -55,13 +53,13 @@ static Converter()
5553
5654 IntPtr dateTimeMod = Runtime . PyImport_ImportModule ( "datetime" ) ;
5755 if ( dateTimeMod == null ) throw new PythonException ( ) ;
58-
56+
5957 decimalCtor = Runtime . PyObject_GetAttrString ( decimalMod , "Decimal" ) ;
6058 if ( decimalCtor == null ) throw new PythonException ( ) ;
61-
59+
6260 dateTimeCtor = Runtime . PyObject_GetAttrString ( dateTimeMod , "datetime" ) ;
6361 if ( dateTimeCtor == null ) throw new PythonException ( ) ;
64-
62+
6563 timeSpanCtor = Runtime . PyObject_GetAttrString ( dateTimeMod , "timedelta" ) ;
6664 if ( timeSpanCtor == null ) throw new PythonException ( ) ;
6765
@@ -224,7 +222,10 @@ internal static IntPtr ToPython(object value, Type type)
224222
225223 IntPtr timeSpanArgs = Runtime . PyTuple_New ( 1 ) ;
226224 Runtime . PyTuple_SetItem ( timeSpanArgs , 0 , Runtime . PyFloat_FromDouble ( timespan . TotalDays ) ) ;
227- return Runtime . PyObject_CallObject ( timeSpanCtor , timeSpanArgs ) ;
225+ var returnTimeSpan = Runtime . PyObject_CallObject ( timeSpanCtor , timeSpanArgs ) ;
226+ // clean up
227+ Runtime . XDecref ( timeSpanArgs ) ;
228+ return returnTimeSpan ;
228229 }
229230 return CLRObject . GetInstHandle ( value , type ) ;
230231
@@ -283,12 +284,14 @@ internal static IntPtr ToPython(object value, Type type)
283284 IntPtr d2p = Runtime . PyString_FromString ( d2s ) ;
284285 IntPtr decimalArgs = Runtime . PyTuple_New ( 1 ) ;
285286 Runtime . PyTuple_SetItem ( decimalArgs , 0 , d2p ) ;
286-
287- return Runtime . PyObject_CallObject ( decimalCtor , decimalArgs ) ;
287+ var returnDecimal = Runtime . PyObject_CallObject ( decimalCtor , decimalArgs ) ;
288+ // clean up
289+ Runtime . XDecref ( decimalArgs ) ;
290+ return returnDecimal ;
288291
289292 case TypeCode . DateTime :
290293 var datetime = ( DateTime ) value ;
291-
294+
292295 IntPtr dateTimeArgs = Runtime . PyTuple_New ( 8 ) ;
293296 Runtime . PyTuple_SetItem ( dateTimeArgs , 0 , Runtime . PyInt_FromInt32 ( datetime . Year ) ) ;
294297 Runtime . PyTuple_SetItem ( dateTimeArgs , 1 , Runtime . PyInt_FromInt32 ( datetime . Month ) ) ;
@@ -298,8 +301,10 @@ internal static IntPtr ToPython(object value, Type type)
298301 Runtime . PyTuple_SetItem ( dateTimeArgs , 5 , Runtime . PyInt_FromInt32 ( datetime . Second ) ) ;
299302 Runtime . PyTuple_SetItem ( dateTimeArgs , 6 , Runtime . PyInt_FromInt32 ( datetime . Millisecond ) ) ;
300303 Runtime . PyTuple_SetItem ( dateTimeArgs , 7 , TzInfo ( datetime . Kind ) ) ;
301-
302- return Runtime . PyObject_CallObject ( dateTimeCtor , dateTimeArgs ) ;
304+ var returnDateTime = Runtime . PyObject_CallObject ( dateTimeCtor , dateTimeArgs ) ;
305+ // clean up
306+ Runtime . XDecref ( dateTimeArgs ) ;
307+ return returnDateTime ;
303308
304309 default :
305310 if ( value is IEnumerable )
@@ -329,7 +334,9 @@ private static IntPtr TzInfo(DateTimeKind kind)
329334 IntPtr tzInfoArgs = Runtime . PyTuple_New ( 2 ) ;
330335 Runtime . PyTuple_SetItem ( tzInfoArgs , 0 , Runtime . PyFloat_FromDouble ( offset . Hours ) ) ;
331336 Runtime . PyTuple_SetItem ( tzInfoArgs , 1 , Runtime . PyFloat_FromDouble ( offset . Minutes ) ) ;
332- return Runtime . PyObject_CallObject ( tzInfoCtor , tzInfoArgs ) ;
337+ var returnValue = Runtime . PyObject_CallObject ( tzInfoCtor , tzInfoArgs ) ;
338+ Runtime . XDecref ( tzInfoArgs ) ;
339+ return returnValue ;
333340 }
334341
335342
0 commit comments