Skip to content

Commit 570e0f8

Browse files
committed
Fixes decimal support
1 parent 6dad422 commit 570e0f8

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/runtime/converter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ internal static Type GetTypeByAlias(IntPtr op)
7575
if (op == Runtime.PyBoolType)
7676
return boolType;
7777

78+
if (op == Runtime.PyDecimalType)
79+
return decimalType;
80+
7881
return null;
7982
}
8083

@@ -105,7 +108,7 @@ internal static IntPtr GetPythonTypeByAlias(Type op)
105108
return Runtime.PyBoolType;
106109

107110
if (op == decimalType)
108-
return Runtime.PyFloatType;
111+
return Runtime.PyDecimalType;
109112

110113
return IntPtr.Zero;
111114
}

src/runtime/methodbinder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,12 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
420420
typematch = true;
421421
clrtype = pi[n].ParameterType;
422422
}
423+
// accepts non-decimal numbers in decimal parameters
424+
if (underlyingType == typeof(decimal))
425+
{
426+
clrtype = pi[n].ParameterType;
427+
typematch = Converter.ToManaged(op, clrtype, out arg, false);
428+
}
423429
// this takes care of implicit conversions
424430
var opImplicit = pi[n].ParameterType.GetMethod("op_Implicit", new[] { clrtype });
425431
if (opImplicit != null)

src/runtime/runtime.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,14 @@ internal static void Initialize()
296296
PyFloatType = PyObject_Type(op);
297297
XDecref(op);
298298

299+
IntPtr decimalMod = PyImport_ImportModule("decimal");
300+
IntPtr decimalCtor = PyObject_GetAttrString(decimalMod, "Decimal");
301+
op = PyObject_CallObject(decimalCtor, IntPtr.Zero);
302+
PyDecimalType = PyObject_Type(op);
303+
XDecref(op);
304+
XDecref(decimalMod);
305+
XDecref(decimalCtor);
306+
299307
#if PYTHON3
300308
PyClassType = IntPtr.Zero;
301309
PyInstanceType = IntPtr.Zero;
@@ -389,6 +397,7 @@ internal static int AtExit()
389397
internal static IntPtr PyBoolType;
390398
internal static IntPtr PyNoneType;
391399
internal static IntPtr PyTypeType;
400+
internal static IntPtr PyDecimalType;
392401

393402
#if PYTHON3
394403
internal static IntPtr PyBytesType;

0 commit comments

Comments
 (0)