Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support pythonic manipulation of managed enums #101

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
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
80 changes: 80 additions & 0 deletions src/embed_tests/ClassManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,86 @@ def call(instance):
}

#endregion

public enum TestEnum
{
FirstEnumValue,
SecondEnumValue,
ThirdEnumValue
}

[Test]
public void EnumPythonOperationsCanBePerformedOnManagedEnum()
{
using (Py.GIL())
{
var module = PyModule.FromString("EnumPythonOperationsCanBePerformedOnManagedEnum", $@"
from clr import AddReference
AddReference(""Python.EmbeddingTest"")

from Python.EmbeddingTest import *

def get_enum_values():
return [x for x in ClassManagerTests.TestEnum]

def count_enum_values():
return len(ClassManagerTests.TestEnum)

def is_enum_value_defined(value):
return value in ClassManagerTests.TestEnum
");

using var pyEnumValues = module.InvokeMethod("get_enum_values");
var enumValues = pyEnumValues.As<List<TestEnum>>();

var expectedEnumValues = Enum.GetValues<TestEnum>();
CollectionAssert.AreEquivalent(expectedEnumValues, enumValues);

using var pyEnumCount = module.InvokeMethod("count_enum_values");
var enumCount = pyEnumCount.As<int>();
Assert.AreEqual(expectedEnumValues.Length, enumCount);

var validEnumValues = expectedEnumValues
.SelectMany(x => new object[] { x, (int)x, Enum.GetName(x.GetType(), x) })
.Select(x => (x, true));
var invalidEnumValues = new object[] { 5, "INVALID_ENUM_VALUE" }.Select(x => (x, false));

foreach (var (enumValue, isValid) in validEnumValues.Concat(invalidEnumValues))
{
using var pyEnumValue = enumValue.ToPython();
using var pyIsDefined = module.InvokeMethod("is_enum_value_defined", pyEnumValue);
var isDefined = pyIsDefined.As<bool>();
Assert.AreEqual(isValid, isDefined, $"Failed for {enumValue} ({enumValue.GetType()})");
}
}
}

[Test]
public void EnumInterableOperationsNotSupportedForManagedNonEnumTypes()
{
using (Py.GIL())
{
var module = PyModule.FromString("EnumInterableOperationsNotSupportedForManagedNonEnumTypes", $@"
from clr import AddReference
AddReference(""Python.EmbeddingTest"")

from Python.EmbeddingTest import *

def get_enum_values():
return [x for x in ClassManagerTests]

def count_enum_values():
return len(ClassManagerTests)

def is_enum_value_defined():
return 1 in ClassManagerTests
");

Assert.Throws<PythonException>(() => module.InvokeMethod("get_enum_values"));
Assert.Throws<PythonException>(() => module.InvokeMethod("count_enum_values"));
Assert.Throws<PythonException>(() => module.InvokeMethod("is_enum_value_defined"));
}
}
}

public class NestedTestParent
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.42" GeneratePathProperty="true">
<PackageReference Include="quantconnect.pythonnet" Version="2.0.43" 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.42\lib\net9.0\Python.Runtime.dll" DestinationFolder="$(OutDir)baseline" />
<Copy SourceFiles="$(NuGetPackageRoot)quantconnect.pythonnet\2.0.43\lib\net9.0\Python.Runtime.dll" DestinationFolder="$(OutDir)baseline" />
</Target>

<Target Name="CopyNewBuild" AfterTargets="Build">
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.42")]
[assembly: AssemblyFileVersion("2.0.42")]
[assembly: AssemblyVersion("2.0.43")]
[assembly: AssemblyFileVersion("2.0.43")]
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.42</Version>
<Version>2.0.43</Version>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<RepositoryUrl>https://github.com/pythonnet/pythonnet</RepositoryUrl>
Expand Down
74 changes: 74 additions & 0 deletions src/runtime/Types/MetaType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,5 +359,79 @@ public static NewReference __subclasscheck__(BorrowedReference tp, BorrowedRefer
{
return DoInstanceCheck(tp, args, true);
}

/// <summary>
/// Standard iteration support Enums. This allows natural interation
/// over the available values an Enum defines.
/// </summary>
public static NewReference tp_iter(BorrowedReference tp)
{
if (!TryGetEnumType(tp, out var type))
{
return default;
}
var values = Enum.GetValues(type);
return new Iterator(values.GetEnumerator(), type).Alloc();
}

/// <summary>
/// Implements __len__ for Enum types.
/// </summary>
public static int mp_length(BorrowedReference tp)
{
if (!TryGetEnumType(tp, out var type))
{
return -1;
}
return Enum.GetValues(type).Length;
}

/// <summary>
/// Implements __contains__ for Enum types.
/// </summary>
public static int sq_contains(BorrowedReference tp, BorrowedReference v)
{
if (!TryGetEnumType(tp, out var type))
{
return -1;
}

if (!Converter.ToManaged(v, type, out var enumValue, false) &&
!Converter.ToManaged(v, typeof(int), out enumValue, false) &&
!Converter.ToManaged(v, typeof(string), out enumValue, false))
{
Exceptions.SetError(Exceptions.TypeError,
$"invalid parameter type for sq_contains: should be {Converter.GetTypeByAlias(v)}, found {type}");
return -1;
}

return Enum.IsDefined(type, enumValue) ? 1 : 0;
}

private static bool TryGetEnumType(BorrowedReference tp, out Type type)
{
type = null;
var cb = GetManagedObject(tp) as ClassBase;
if (cb == null)
{
Exceptions.SetError(Exceptions.TypeError, "invalid object");
return false;
}

if (!cb.type.Valid)
{
Exceptions.SetError(Exceptions.TypeError, "invalid type");
return false;
}

if (!cb.type.Value.IsEnum)
{
Exceptions.SetError(Exceptions.TypeError, "uniterable type");
return false;
}

type = cb.type.Value;
return true;
}
}
}
Loading