Skip to content

QuantConnect Update #1385

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

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
062a0bc
Reflect PR #1 Support for Decimal
C-SELLERS Feb 3, 2021
5ed5a96
Reflect PR#8 MISSING CONVERTER.CS L516-528 Changes
C-SELLERS Feb 3, 2021
9b5445e
Reflect PR #14
C-SELLERS Feb 4, 2021
709643a
Reflect PR #15
C-SELLERS Feb 4, 2021
a481700
Reflect PR #19
C-SELLERS Feb 4, 2021
de0328c
Reflect PR #25
C-SELLERS Feb 4, 2021
481f4d0
Reflect PR #34
C-SELLERS Feb 4, 2021
863530d
Reflect PR #35
C-SELLERS Feb 4, 2021
5839d21
Implement List Conversion, Reflect PR #37 Tests
C-SELLERS Feb 4, 2021
c362816
Reflect PR #38 Partial: Assembly Manager Improvements
C-SELLERS Feb 4, 2021
cde9b51
Reflect PR #38
C-SELLERS Feb 4, 2021
101624e
Reflect PR #42 KeyValuePairEnumerableObject
C-SELLERS Feb 4, 2021
56a4bcf
Reflect PR #10 Runtime DecimalType
C-SELLERS Feb 4, 2021
508db2e
Add TimeDelta and DateTime tests
C-SELLERS Feb 5, 2021
ebbafad
Fix DecimalConversion test for float conversion
C-SELLERS Feb 5, 2021
53375ce
Converter mod tweaks
C-SELLERS Feb 6, 2021
c8fdbcb
Adjust a few broken PyTests
C-SELLERS Feb 6, 2021
af30873
Use _pydecimal to not interfere with Lean/decimal.py
C-SELLERS Feb 9, 2021
bf1755d
Add MethodBinder tests
C-SELLERS Feb 9, 2021
58d5df0
MethodBinder implicit resolution
Martin-Molinero Feb 4, 2021
0c94228
Fix bad cherry pick
C-SELLERS Feb 10, 2021
44e089a
Refactoring precedence resolution
Martin-Molinero Feb 5, 2021
108eacf
Deal with operator binding
C-SELLERS Feb 10, 2021
6379568
Fix `TestNoOverloadException` unit test
C-SELLERS Feb 10, 2021
9f2796a
Fix for DomainReload tests
C-SELLERS Feb 10, 2021
b0aca5c
Add InEquality Operator Test
C-SELLERS Feb 11, 2021
0f5f0ba
Dont PyObjects precedence in Operator methods
C-SELLERS Feb 11, 2021
ed6ab18
Revert "Merge pull request #1240 from danabr/auto-cast-ret-val-to-int…
C-SELLERS Feb 12, 2021
d87584b
Fix Primitive Conversion to Int
C-SELLERS Feb 15, 2021
f59335f
Post rebase fix
C-SELLERS Feb 15, 2021
bd94e49
Add PrimitiveIntConversion test
C-SELLERS Feb 16, 2021
cd06d10
Add test for interface derived classes
C-SELLERS Feb 16, 2021
aeb20c0
Add to Authors.md
C-SELLERS Feb 16, 2021
ae34f30
Load in current directory into Python Path
C-SELLERS Feb 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reflect PR#8 MISSING CONVERTER.CS L516-528 Changes
  • Loading branch information
C-SELLERS committed Feb 15, 2021
commit 5ed5a96e6a3467eb9381f724fcb62a1c5945c7c5
114 changes: 113 additions & 1 deletion src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ private Converter()
private static Type flagsType;
private static Type boolType;
private static Type typeType;
private static IntPtr decimalCtor;
private static IntPtr dateTimeCtor;
private static IntPtr timeSpanCtor;
private static IntPtr tzInfoCtor;

static Converter()
{
Expand All @@ -45,6 +49,38 @@ static Converter()
flagsType = typeof(FlagsAttribute);
boolType = typeof(Boolean);
typeType = typeof(Type);


IntPtr decimalMod = Runtime.PyImport_ImportModule("decimal");
if (decimalMod == null) throw new PythonException();

IntPtr dateTimeMod = Runtime.PyImport_ImportModule("datetime");
if (dateTimeMod == null) throw new PythonException();

decimalCtor = Runtime.PyObject_GetAttrString(decimalMod, "Decimal");
if (decimalCtor == null) throw new PythonException();

dateTimeCtor = Runtime.PyObject_GetAttrString(dateTimeMod, "datetime");
if (dateTimeCtor == null) throw new PythonException();

timeSpanCtor = Runtime.PyObject_GetAttrString(dateTimeMod, "timedelta");
if (timeSpanCtor == null) throw new PythonException();

IntPtr tzInfoMod = PythonEngine.ModuleFromString("custom_tzinfo",
"from datetime import timedelta, tzinfo\n" +
"class GMT(tzinfo):\n" +
" def __init__(self, hours, minutes):\n" +
" self.hours = hours\n" +
" self.minutes = minutes\n" +
" def utcoffset(self, dt):\n" +
" return timedelta(hours=self.hours, minutes=self.minutes)\n" +
" def tzname(self, dt):\n" +
" return \"GMT {0:00}:{1:00}\".format(self.hours, self.minutes)\n" +
" def dst (self, dt):\n" +
" return timedelta(0)\n").Handle;

tzInfoCtor = Runtime.PyObject_GetAttrString(tzInfoMod, "GMT");
if (tzInfoCtor == null) throw new PythonException();
}


Expand Down Expand Up @@ -208,6 +244,14 @@ internal static IntPtr ToPython(object value, Type type)
switch (tc)
{
case TypeCode.Object:
if (value is TimeSpan)
{
var timespan = (TimeSpan)value;

IntPtr timeSpanArgs = Runtime.PyTuple_New(1);
Runtime.PyTuple_SetItem(timeSpanArgs, 0, Runtime.PyFloat_FromDouble(timespan.TotalDays));
return Runtime.PyObject_CallObject(timeSpanCtor, timeSpanArgs);
}
return CLRObject.GetInstHandle(value, type);

case TypeCode.String:
Expand Down Expand Up @@ -269,7 +313,23 @@ internal static IntPtr ToPython(object value, Type type)
IntPtr args = Runtime.PyTuple_New(1);
Runtime.PyTuple_SetItem(args, 0, d2p);

return Runtime.PyObject_CallObject(ctor, args);
return Runtime.PyObject_CallObject(decimalCtor, args);

case TypeCode.DateTime:
var datetime = (DateTime)value;

IntPtr dateTimeArgs = Runtime.PyTuple_New(8);
Runtime.PyTuple_SetItem(dateTimeArgs, 0, Runtime.PyInt_FromInt32(datetime.Year));
Runtime.PyTuple_SetItem(dateTimeArgs, 1, Runtime.PyInt_FromInt32(datetime.Month));
Runtime.PyTuple_SetItem(dateTimeArgs, 2, Runtime.PyInt_FromInt32(datetime.Day));
Runtime.PyTuple_SetItem(dateTimeArgs, 3, Runtime.PyInt_FromInt32(datetime.Hour));
Runtime.PyTuple_SetItem(dateTimeArgs, 4, Runtime.PyInt_FromInt32(datetime.Minute));
Runtime.PyTuple_SetItem(dateTimeArgs, 5, Runtime.PyInt_FromInt32(datetime.Second));
Runtime.PyTuple_SetItem(dateTimeArgs, 6, Runtime.PyInt_FromInt32(datetime.Millisecond));
Runtime.PyTuple_SetItem(dateTimeArgs, 7, TzInfo(datetime.Kind));

return Runtime.PyObject_CallObject(dateTimeCtor, dateTimeArgs);


default:
if (value is IEnumerable)
Expand All @@ -292,6 +352,15 @@ internal static IntPtr ToPython(object value, Type type)
}
}

private static IntPtr TzInfo(DateTimeKind kind)
{
if (kind == DateTimeKind.Unspecified) return Runtime.PyNone;
var offset = kind == DateTimeKind.Local ? DateTimeOffset.Now.Offset : TimeSpan.Zero;
IntPtr tzInfoArgs = Runtime.PyTuple_New(2);
Runtime.PyTuple_SetItem(tzInfoArgs, 0, Runtime.PyFloat_FromDouble(offset.Hours));
Runtime.PyTuple_SetItem(tzInfoArgs, 1, Runtime.PyFloat_FromDouble(offset.Minutes));
return Runtime.PyObject_CallObject(tzInfoCtor, tzInfoArgs);
}

/// <summary>
/// In a few situations, we don't have any advisory type information
Expand Down Expand Up @@ -507,6 +576,12 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
return false;
}

var underlyingType = Nullable.GetUnderlyingType(obType);
if (underlyingType != null)
{
return ToManagedValue(value, underlyingType, out result, setError);
}

TypeCode typeCode = Type.GetTypeCode(obType);
if (typeCode == TypeCode.Object)
{
Expand All @@ -533,6 +608,32 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo

switch (tc)
{
case TypeCode.Object:
if (obType == typeof(TimeSpan))
{
op = Runtime.PyObject_Str(value);
TimeSpan ts;
var arr = Runtime.GetManagedString(op).Split(',');
string sts = arr.Length == 1 ? arr[0] : arr[1];
if (!TimeSpan.TryParse(sts, out ts))
{
goto type_error;
}
Runtime.XDecref(op);

int days = 0;
if (arr.Length > 1)
{
if (!int.TryParse(arr[0].Split(' ')[0].Trim(), out days))
{
goto type_error;
}
}
result = ts.Add(TimeSpan.FromDays(days));
return true;
}
break;

case TypeCode.String:
string st = Runtime.GetManagedString(value);
if (st == null)
Expand Down Expand Up @@ -774,6 +875,17 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
Runtime.XDecref(op);
result = m;
return true;
case TypeCode.DateTime:
op = Runtime.PyObject_Str(value);
DateTime dt;
string sdt = Runtime.GetManagedString(op);
if (!DateTime.TryParse(sdt, out dt))
{
goto type_error;
}
Runtime.XDecref(op);
result = dt;
return true;
default:
goto type_error;
}
Expand Down
15 changes: 15 additions & 0 deletions src/runtime/methodbinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,13 @@ static Type TryComputeClrArgumentType(Type parameterType, IntPtr argument, bool
}
if (!typematch)
{
// this takes care of nullables
var underlyingType = Nullable.GetUnderlyingType(parameterType);
if (underlyingType != null)
{
parameterType = underlyingType;
}

// this takes care of enum values
TypeCode parameterTypeCode = Type.GetTypeCode(parameterType);
TypeCode clrTypeCode = Type.GetTypeCode(clrtype);
Expand All @@ -763,6 +770,14 @@ static Type TryComputeClrArgumentType(Type parameterType, IntPtr argument, bool
{
Exceptions.RaiseTypeError($"Expected {parameterTypeCode}, got {clrTypeCode}");
}

// this takes care of implicit conversions
var opImplicit = parameterType.GetMethod("op_Implicit", new[] { clrtype });
if (opImplicit != null)
{
typematch = opImplicit.ReturnType == parameterType;
clrtype = parameterType;
}
}
Runtime.XDecref(pyoptype);
if (!typematch)
Expand Down