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 all commits
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
2 changes: 2 additions & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- Christoph Gohlke ([@cgohlke](https://github.com/cgohlke))
- Christopher Bremner ([@chrisjbremner](https://github.com/chrisjbremner))
- Christopher Pow ([@christopherpow](https://github.com/christopherpow))
- Colton Sellers ([@C-SELLERS](https://github.com/C-SELLERS))
- Daniel Abrahamsson ([@danabr](https://github.com/danabr))
- Daniel Fernandez ([@fdanny](https://github.com/fdanny))
- Daniel Santana ([@dgsantana](https://github.com/dgsantana))
Expand All @@ -52,6 +53,7 @@
- Luke Stratman ([@lstratman](https://github.com/lstratman))
- Konstantin Posudevskiy ([@konstantin-posudevskiy](https://github.com/konstantin-posudevskiy))
- Matthias Dittrich ([@matthid](https://github.com/matthid))
- Martin Molinero ([@Martin-Molinero](https://github.com/Martin-Molinero))
- Meinrad Recheis ([@henon](https://github.com/henon))
- Mohamed Koubaa ([@koubaa](https://github.com/koubaa))
- Patrick Stewart ([@patstew](https://github.com/patstew))
Expand Down
5 changes: 0 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
details about the cause of the failure
- `clr.AddReference` no longer adds ".dll" implicitly
- `PyIter(PyObject)` constructor replaced with static `PyIter.GetIter(PyObject)` method
- BREAKING: Return values from .NET methods that return an interface are now automatically
wrapped in that interface. This is a breaking change for users that rely on being
able to access members that are part of the implementation class, but not the
interface. Use the new __implementation__ or __raw_implementation__ properties to
if you need to "downcast" to the implementation class.
- BREAKING: Parameters marked with `ParameterAttributes.Out` are no longer returned in addition
to the regular method return value (unless they are passed with `ref` or `out` keyword).
- BREAKING: Drop support for the long-deprecated CLR.* prefix.
Expand Down
62 changes: 62 additions & 0 deletions src/embed_tests/TestConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,56 @@ public void Dispose()
PythonEngine.Shutdown();
}

[Test]
public void ConvertListRoundTrip()
{
var list = new List<Type> { typeof(decimal), typeof(int) };
var py = list.ToPython();
object result;
var converted = Converter.ToManaged(py.Handle, typeof(List<Type>), out result, false);

Assert.IsTrue(converted);
Assert.AreEqual(result, list);
}

[Test]
public void ConvertPyListToArray()
{
var array = new List<Type> { typeof(decimal), typeof(int) };
var py = array.ToPython();
object result;
var converted = Converter.ToManaged(py.Handle, typeof(Type[]), out result, false);

Assert.IsTrue(converted);
Assert.AreEqual(result, array);
}

[Test]
public void ConvertTimeSpanRoundTrip()
{
var timespan = new TimeSpan(0, 1, 0, 0);
var pyTimedelta = timespan.ToPython();

object result;
var converted = Converter.ToManaged(pyTimedelta.Handle, typeof(TimeSpan), out result, false);

Assert.IsTrue(converted);
Assert.AreEqual(result, timespan);
}

[Test]
public void ConvertDateTimeRoundTrip()
{
var datetime = new DateTime(2000, 1, 1);
var pyDatetime = datetime.ToPython();

object result;
var converted = Converter.ToManaged(pyDatetime.Handle, typeof(DateTime), out result, false);

Assert.IsTrue(converted);
Assert.AreEqual(result, datetime);
}

[Test]
public void TestConvertSingleToManaged(
[Values(float.PositiveInfinity, float.NegativeInfinity, float.MinValue, float.MaxValue, float.NaN,
Expand Down Expand Up @@ -136,5 +186,17 @@ public void RawPyObjectProxy()
var proxiedHandle = pyObjectProxy.GetAttr("Handle").As<IntPtr>();
Assert.AreEqual(pyObject.Handle, proxiedHandle);
}

[Test]
public void PrimitiveIntConversion()
{
decimal value = 10;
var pyValue = value.ToPython();

// Try to convert python value to int
var testInt = pyValue.As<int>();
Assert.AreEqual(testInt , 10);
}

}
}
76 changes: 76 additions & 0 deletions src/embed_tests/TestInterfaceClasses.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using NUnit.Framework;
using Python.Runtime;

namespace Python.EmbeddingTest
{
public class TestInterfaceClasses
{
public string testCode = @"
from clr import AddReference
AddReference(""System"")
AddReference(""Python.EmbeddingTest"")
from Python.EmbeddingTest import *

testModule = TestInterfaceClasses.GetInstance()
print(testModule.Child.ChildBool)

";

[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}

[Test]
public void TestInterfaceDerivedClassMembers()
{
// This test gets an instance of the CSharpTestModule in Python
// and then attempts to access it's member "Child"'s bool that is
// not defined in the interface.
PythonEngine.Exec(testCode);
}

public interface IInterface
{
bool InterfaceBool { get; set; }
}

public class Parent : IInterface
{
public bool InterfaceBool { get; set; }
public bool ParentBool { get; set; }
}

public class Child : Parent
{
public bool ChildBool { get; set; }
}

public class CSharpTestModule
{
public IInterface Child;

public CSharpTestModule()
{
Child = new Child
{
ChildBool = true,
ParentBool = true,
InterfaceBool = true
};
}
}

public static CSharpTestModule GetInstance()
{
return new CSharpTestModule();
}
}
}
121 changes: 121 additions & 0 deletions src/embed_tests/TestMethodBinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System;

using NUnit.Framework;

using Python.Runtime;

namespace Python.EmbeddingTest
{
public class TestMethodBinder
{
private static dynamic module;
private static string testModule = @"
from clr import AddReference
AddReference(""System"")
AddReference(""Python.EmbeddingTest"")
from Python.EmbeddingTest import *
class PythonModel(TestMethodBinder.CSharpModel):
def TestA(self):
return self.OnlyString(TestMethodBinder.TestImplicitConversion())
def TestB(self):
return self.OnlyClass('input string')
def TestC(self):
return self.InvokeModel('input string')
def TestD(self):
return self.InvokeModel(TestMethodBinder.TestImplicitConversion())
def TestE(self, array):
return array.Length == 2";


[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
module = PythonEngine.ModuleFromString("module", testModule).GetAttr("PythonModel").Invoke();
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}

[Test]
public void ImplicitConversionToString()
{
var data = (string)module.TestA();
// we assert implicit conversion took place
Assert.AreEqual("OnlyString impl: implicit to string", data);
}

[Test]
public void ImplicitConversionToClass()
{
var data = (string)module.TestB();
// we assert implicit conversion took place
Assert.AreEqual("OnlyClass impl", data);
}

[Test]
public void WillAvoidUsingImplicitConversionIfPossible_String()
{
var data = (string)module.TestC();
// we assert no implicit conversion took place
Assert.AreEqual("string impl: input string", data);
}

[Test]
public void WillAvoidUsingImplicitConversionIfPossible_Class()
{
var data = (string)module.TestD();
// we assert no implicit conversion took place
Assert.AreEqual("TestImplicitConversion impl", data);

}

[Test]
public void ArrayLength()
{
var array = new[] { "pepe", "pinocho" };
var data = (bool)module.TestE(array);

// Assert it is true
Assert.AreEqual(true, data);
}

public class CSharpModel
{
public virtual string OnlyClass(TestImplicitConversion data)
{
return "OnlyClass impl";
}

public virtual string OnlyString(string data)
{
return "OnlyString impl: " + data;
}

public virtual string InvokeModel(string data)
{
return "string impl: " + data;
}

public virtual string InvokeModel(TestImplicitConversion data)
{
return "TestImplicitConversion impl";
}
}

public class TestImplicitConversion
{
public static implicit operator string(TestImplicitConversion symbol)
{
return "implicit to string";
}
public static implicit operator TestImplicitConversion(string symbol)
{
return new TestImplicitConversion();
}
}
}
}
23 changes: 23 additions & 0 deletions src/embed_tests/TestOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,29 @@ public void SymmetricalOperatorOverloads()
");
}

[Test]
public void OperatorInequality()
{
string name = string.Format("{0}.{1}",
typeof(OperableObject).DeclaringType.Name,
typeof(OperableObject).Name);
string module = MethodBase.GetCurrentMethod().DeclaringType.Namespace;

PythonEngine.Exec($@"
from {module} import *
cls = {name}
b = cls(10)
a = cls(2)


c = a <= b
assert c == (a.Num <= b.Num)

c = a >= b
assert c == (a.Num >= b.Num)
");
}

[Test]
public void OperatorOverloadMissingArgument()
{
Expand Down
7 changes: 1 addition & 6 deletions src/runtime/arrayobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,8 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType,
public new static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
{
var obj = (CLRObject)GetManagedObject(ob);
var arrObj = (ArrayObject)GetManagedObjectType(ob);
if (!arrObj.type.Valid)
{
return Exceptions.RaiseTypeError(arrObj.type.DeletedMessage);
}
var items = obj.inst as Array;
Type itemType = arrObj.type.Value.GetElementType();
Type itemType = obj.inst.GetType().GetElementType();
int rank = items.Rank;
int index;
object value;
Expand Down
Loading