Skip to content

Commit 2fc9ea8

Browse files
committed
Reflect PR #10 Runtime DecimalType
1 parent 9720b52 commit 2fc9ea8

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/runtime/converter.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ internal static Type GetTypeByAlias(IntPtr op)
9999
if (op == Runtime.PyBoolType)
100100
return boolType;
101101

102+
if (op == Runtime.PyDecimalType)
103+
return decimalType;
104+
102105
return null;
103106
}
104107

@@ -119,12 +122,15 @@ internal static IntPtr GetPythonTypeByAlias(Type op)
119122
if (op == doubleType)
120123
return Runtime.PyFloatType;
121124

122-
if (op == singleType || op == decimalType)
125+
if (op == singleType)
123126
return Runtime.PyFloatType;
124127

125128
if (op == boolType)
126129
return Runtime.PyBoolType;
127130

131+
if (op == decimalType)
132+
return Runtime.PyDecimalType;
133+
128134
return IntPtr.Zero;
129135
}
130136

src/runtime/methodbinder.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ internal static int GetPrecedence(MethodBase mi)
216216
val += ArgPrecedence(pi[i].ParameterType);
217217
}
218218

219+
var info = mi as MethodInfo;
220+
if (info != null)
221+
{
222+
val += ArgPrecedence(info.ReturnType);
223+
val += mi.DeclaringType == mi.ReflectedType ? 0 : 3000;
224+
}
225+
219226
return val;
220227
}
221228

@@ -757,7 +764,6 @@ static Type TryComputeClrArgumentType(Type parameterType, IntPtr argument, bool
757764
{
758765
parameterType = underlyingType;
759766
}
760-
761767
// this takes care of enum values
762768
TypeCode parameterTypeCode = Type.GetTypeCode(parameterType);
763769
TypeCode clrTypeCode = Type.GetTypeCode(clrtype);
@@ -771,6 +777,13 @@ static Type TryComputeClrArgumentType(Type parameterType, IntPtr argument, bool
771777
Exceptions.RaiseTypeError($"Expected {parameterTypeCode}, got {clrTypeCode}");
772778
}
773779

780+
// accepts non-decimal numbers in decimal parameters
781+
if (parameterType == typeof(decimal))
782+
{
783+
clrtype = parameterType;
784+
typematch = Converter.ToManaged(argument, clrtype, out _, false);
785+
}
786+
774787
// this takes care of implicit conversions
775788
var opImplicit = parameterType.GetMethod("op_Implicit", new[] { clrtype });
776789
if (opImplicit != null)

src/runtime/runtime.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,14 @@ private static void InitPyMembers()
277277
() => PyFloatType = IntPtr.Zero);
278278
XDecref(op);
279279

280+
IntPtr decimalMod = PyImport_ImportModule("decimal");
281+
IntPtr decimalCtor = PyObject_GetAttrString(decimalMod, "Decimal");
282+
op = PyObject_CallObject(decimalCtor, IntPtr.Zero);
283+
PyDecimalType = PyObject_Type(op);
284+
XDecref(op);
285+
XDecref(decimalMod);
286+
XDecref(decimalCtor);
287+
280288
PyClassType = IntPtr.Zero;
281289
PyInstanceType = IntPtr.Zero;
282290

@@ -557,6 +565,7 @@ private static void MoveClrInstancesOnwershipToPython()
557565
internal static IntPtr PyBoolType;
558566
internal static IntPtr PyNoneType;
559567
internal static IntPtr PyTypeType;
568+
internal static IntPtr PyDecimalType;
560569

561570
internal static IntPtr Py_NoSiteFlag;
562571

0 commit comments

Comments
 (0)