Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
628 changes: 628 additions & 0 deletions src/embed_tests/EnumTests.cs

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions src/embed_tests/TestMethodBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,20 @@ public string VariableArgumentsMethod(params PyObject[] paramsParams)
return "VariableArgumentsMethod(PyObject[])";
}

// ----

public string MethodWithEnumParam(SomeEnu enumValue, string symbol)
{
return $"MethodWithEnumParam With Enum";
}

public string MethodWithEnumParam(PyObject pyObject, string symbol)
{
return $"MethodWithEnumParam With PyObject";
}

// ----

public string ConstructorMessage { get; set; }

public OverloadsTestClass(params CSharpModel[] paramsParams)
Expand Down Expand Up @@ -1117,6 +1131,26 @@ def get_instance():
Assert.AreEqual("OverloadsTestClass(PyObject[])", instance.GetAttr("ConstructorMessage").As<string>());
}

[Test]
public void EnumHasPrecedenceOverPyObject()
{
using var _ = Py.GIL();

var module = PyModule.FromString("EnumHasPrecedenceOverPyObject", @$"
from clr import AddReference
AddReference(""System"")
from Python.EmbeddingTest import *

class PythonModel(TestMethodBinder.CSharpModel):
pass

def call_method():
return TestMethodBinder.OverloadsTestClass().MethodWithEnumParam(TestMethodBinder.SomeEnu.A, ""Some string"")
");

var result = module.GetAttr("call_method").Invoke();
Assert.AreEqual("MethodWithEnumParam With Enum", result.As<string>());
}

// Used to test that we match this function with Py DateTime & Date Objects
public static int GetMonth(DateTime test)
Expand Down
4 changes: 2 additions & 2 deletions src/perf_tests/Python.PerformanceTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.*" />
<PackageReference Include="quantconnect.pythonnet" Version="2.0.44" GeneratePathProperty="true">
<PackageReference Include="quantconnect.pythonnet" Version="2.0.45" GeneratePathProperty="true">
<IncludeAssets>compile</IncludeAssets>
</PackageReference>
</ItemGroup>
Expand All @@ -25,7 +25,7 @@
</Target>

<Target Name="CopyBaseline" AfterTargets="Build">
<Copy SourceFiles="$(NuGetPackageRoot)quantconnect.pythonnet\2.0.44\lib\net9.0\Python.Runtime.dll" DestinationFolder="$(OutDir)baseline" />
<Copy SourceFiles="$(NuGetPackageRoot)quantconnect.pythonnet\2.0.45\lib\net9.0\Python.Runtime.dll" DestinationFolder="$(OutDir)baseline" />
</Target>

<Target Name="CopyNewBuild" AfterTargets="Build">
Expand Down
17 changes: 17 additions & 0 deletions src/runtime/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ namespace Python.Runtime
[SuppressUnmanagedCodeSecurity]
internal class Converter
{
/// <summary>
/// We use a cache of the enum values references so that we treat them as singletons in Python.
/// We just try to mimic Python enums behavior, since Python enum values are singletons,
/// so the `is` identity comparison operator works for C# enums as well.
/// </summary>

private static readonly Dictionary<object, PyObject> _enumCache = new();
private Converter()
{
}
Expand Down Expand Up @@ -226,6 +233,16 @@ internal static NewReference ToPython(object? value, Type type)
return resultlist.NewReferenceOrNull();
}

if (type.IsEnum)
{
if (!_enumCache.TryGetValue(value, out var cachedValue))
{
_enumCache[value] = cachedValue = CLRObject.GetReference(value, type).MoveToPyObject();
}

return cachedValue.NewReferenceOrNull();
}

// it the type is a python subclass of a managed type then return the
// underlying python object rather than construct a new wrapper object.
var pyderived = value as IPythonDerivedType;
Expand Down
11 changes: 11 additions & 0 deletions src/runtime/MethodBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,17 @@ internal static int ArgPrecedence(Type t, bool isOperatorMethod)
return 3000;
}

if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>))
{
// Nullable<T> is a special case, we treat it as the underlying type
return ArgPrecedence(Nullable.GetUnderlyingType(t), isOperatorMethod);
}

if (t.IsEnum)
{
return -2;
}

if (t.IsAssignableFrom(typeof(PyObject)) && !isOperatorMethod)
{
return -1;
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
[assembly: InternalsVisibleTo("Python.EmbeddingTest, PublicKey=00240000048000009400000006020000002400005253413100040000110000005ffd8f49fb44ab0641b3fd8d55e749f716e6dd901032295db641eb98ee46063cbe0d4a1d121ef0bc2af95f8a7438d7a80a3531316e6b75c2dae92fb05a99f03bf7e0c03980e1c3cfb74ba690aca2f3339ef329313bcc5dccced125a4ffdc4531dcef914602cd5878dc5fbb4d4c73ddfbc133f840231343e013762884d6143189")]
[assembly: InternalsVisibleTo("Python.Test, PublicKey=00240000048000009400000006020000002400005253413100040000110000005ffd8f49fb44ab0641b3fd8d55e749f716e6dd901032295db641eb98ee46063cbe0d4a1d121ef0bc2af95f8a7438d7a80a3531316e6b75c2dae92fb05a99f03bf7e0c03980e1c3cfb74ba690aca2f3339ef329313bcc5dccced125a4ffdc4531dcef914602cd5878dc5fbb4d4c73ddfbc133f840231343e013762884d6143189")]

[assembly: AssemblyVersion("2.0.44")]
[assembly: AssemblyFileVersion("2.0.44")]
[assembly: AssemblyVersion("2.0.45")]
[assembly: AssemblyFileVersion("2.0.45")]
2 changes: 1 addition & 1 deletion src/runtime/Python.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<RootNamespace>Python.Runtime</RootNamespace>
<AssemblyName>Python.Runtime</AssemblyName>
<PackageId>QuantConnect.pythonnet</PackageId>
<Version>2.0.44</Version>
<Version>2.0.45</Version>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<RepositoryUrl>https://github.com/pythonnet/pythonnet</RepositoryUrl>
Expand Down
Loading
Loading