@@ -30,6 +30,7 @@ private Converter()
3030 private static Type flagsType ;
3131 private static Type boolType ;
3232 private static Type typeType ;
33+ private static IntPtr decimalCtor ;
3334
3435 static Converter ( )
3536 {
@@ -45,6 +46,9 @@ static Converter()
4546 flagsType = typeof ( FlagsAttribute ) ;
4647 boolType = typeof ( Boolean ) ;
4748 typeType = typeof ( Type ) ;
49+
50+ IntPtr decimalMod = Runtime . PyImport_ImportModule ( "decimal" ) ;
51+ if ( decimalMod == null ) throw new PythonException ( ) ;
4852 }
4953
5054
@@ -100,6 +104,9 @@ internal static IntPtr GetPythonTypeByAlias(Type op)
100104 if ( op == boolType )
101105 return Runtime . PyBoolType ;
102106
107+ if ( op == decimalType )
108+ return Runtime . PyFloatType ;
109+
103110 return IntPtr . Zero ;
104111 }
105112
@@ -229,6 +236,14 @@ internal static IntPtr ToPython(object value, Type type)
229236 case TypeCode . UInt64 :
230237 return Runtime . PyLong_FromUnsignedLongLong ( ( ulong ) value ) ;
231238
239+ case TypeCode . Decimal :
240+ string d2s = ( ( decimal ) value ) . ToString ( nfi ) ;
241+ IntPtr d2p = Runtime . PyString_FromString ( d2s ) ;
242+ IntPtr decimalArgs = Runtime . PyTuple_New ( 1 ) ;
243+ Runtime . PyTuple_SetItem ( decimalArgs , 0 , d2p ) ;
244+
245+ return Runtime . PyObject_CallObject ( decimalCtor , decimalArgs ) ;
246+
232247 default :
233248 if ( value is IEnumerable )
234249 {
@@ -821,6 +836,18 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
821836 Runtime . XDecref ( op ) ;
822837 result = d ;
823838 return true ;
839+
840+ case TypeCode . Decimal :
841+ op = Runtime . PyObject_Str ( value ) ;
842+ decimal m ;
843+ string sm = Runtime . GetManagedString ( op ) ;
844+ if ( ! Decimal . TryParse ( sm , NumberStyles . Number , nfi , out m ) )
845+ {
846+ goto type_error ;
847+ }
848+ Runtime . XDecref ( op ) ;
849+ result = m ;
850+ return true ;
824851 }
825852
826853
0 commit comments