8000 Method overload with decimal parameter accepts non-decimal numbers by AlexCatarino · Pull Request #10 · QuantConnect/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

Method overload with decimal parameter accepts non-decimal numbers #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

## Contributors

- Alexandre Catarino([@AlexCatarino](https://github.com/AlexCatarino))
- Arvid JB ([@ArvidJB](https://github.com/ArvidJB))
- Bradley Friedman ([@leith-bartrich](https://github.com/leith-bartrich))
- Callum Noble ([@callumnoble](https://github.com/callumnoble))
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
- Fixed `clr.GetClrType` when iterating over `System` members (#607)
- Fixed `LockRecursionException` when loading assemblies (#627)
- Fixed errors breaking .NET Remoting on method invoke (#276)
- Fixed missing information on 'No method matches given arguments' by adding the method name


## [2.3.0][] - 2017-03-11
Expand Down
5 changes: 4 additions & 1 deletion src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ internal static Type GetTypeByAlias(IntPtr op)
if (op == Runtime.PyBoolType)
return boolType;

if (op == Runtime.PyDecimalType)
return decimalType;

return null;
}

Expand Down Expand Up @@ -136,7 +139,7 @@ internal static IntPtr GetPythonTypeByAlias(Type op)
return Runtime.PyBoolType;

if (op == decimalType)
return Runtime.PyFloatType;
return Runtime.PyDecimalType;

return IntPtr.Zero;
}
Expand Down
20 changes: 19 additions & 1 deletion src/runtime/methodbinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,16 @@ internal static int GetPrecedence(MethodBase mi)
val += ArgPrecedence(pi[i].ParameterType);
}

var info = mi as MethodInfo;
if (info != null)
{
val += ArgPrecedence(info.ReturnType);
val += mi.DeclaringType == mi.ReflectedType ? 0 : 3000;
}

return val;
}

/// <summary>
/// Return a precedence value for a particular Type object.
/// </summary>
Expand All @@ -200,6 +207,11 @@ internal static int ArgPrecedence(Type t)
return 3000;
}

if (t.IsAssignableFrom(typeof(PyObject)))
{
return -1;
}

TypeCode tc = Type.GetTypeCode(t);
// TODO: Clean up
switch (tc)
Expand Down Expand Up @@ -408,6 +420,12 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
typematch = true;
clrtype = pi[n].ParameterType;
}
// accepts non-decimal numbers in decimal parameters
if (underlyingType == typeof(decimal))
{
clrtype = pi[n].ParameterType;
typematch = Converter.ToManaged(op, clrtype, out arg, false);
}
// this takes care of implicit conversions
var opImplicit = pi[n].ParameterType.GetMethod("op_Implicit", new[] { clrtype });
if (opImplicit != null)
Expand Down
9 changes: 9 additions & 0 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,14 @@ internal static void Initialize()
PyFloatType = PyObject_Type(op);
XDecref(op);

IntPtr decimalMod = PyImport_ImportModule("decimal");
IntPtr decimalCtor = PyObject_GetAttrString(decimalMod, "Decimal");
op = PyObject_CallObject(decimalCtor, IntPtr.Zero);
PyDecimalType = PyObject_Type(op);
XDecref(op);
XDecref(decimalMod);
XDecref(decimalCtor);

#if PYTHON3
PyClassType = IntPtr.Zero;
PyInstanceType = IntPtr.Zero;
Expand Down Expand Up @@ -392,6 +400,7 @@ internal static int AtExit()
internal static IntPtr PyBoolType;
internal static IntPtr PyNoneType;
internal static IntPtr PyTypeType;
internal static IntPtr PyDecimalType;

#if PYTHON3
internal static IntPtr PyBytesType;
Expand Down
0