Skip to content

Commit e7ee950

Browse files
committed
Make EnumObject derive ClassObject for constructor handling
1 parent 1aa5b8b commit e7ee950

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/embed_tests/EnumTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,30 @@ public void ThrowsOnNullComparisonOperators([Values("<", "<=", ">", ">=")] strin
621621
Assert.Throws<PythonException>(() => module.InvokeMethod("compare_with_csharp_object2", pyNull));
622622
}
623623

624+
[TestCase(VerticalDirection.Down)]
625+
[TestCase(VerticalDirection.Flat)]
626+
[TestCase(VerticalDirection.Up)]
627+
public void CanInstantiateEnumFromInt(VerticalDirection expectedEnumValue)
628+
{
629+
using var _ = Py.GIL();
630+
using var module = PyModule.FromString("CanInstantiateEnumFromInt", $@"
631+
from clr import AddReference
632+
AddReference(""Python.EmbeddingTest"")
633+
634+
from Python.EmbeddingTest import *
635+
636+
def get_enum(int_value):
637+
return {nameof(EnumTests)}.{nameof(VerticalDirection)}(int_value)
638+
639+
");
640+
641+
using var pyEnumIntValue = ((int)expectedEnumValue).ToPython();
642+
PyObject pyEnumValue = null;
643+
Assert.DoesNotThrow(() => pyEnumValue = module.InvokeMethod("get_enum", pyEnumIntValue));
644+
var enumValue = pyEnumValue.As<VerticalDirection>();
645+
Assert.AreEqual(expectedEnumValue, enumValue);
646+
}
647+
624648
public class TestClass
625649
{
626650
}

src/runtime/Types/EnumObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Python.Runtime
77
/// Managed class that provides the implementation for reflected enum types.
88
/// </summary>
99
[Serializable]
10-
internal class EnumObject : ClassBase
10+
internal class EnumObject : ClassObject
1111
{
1212
internal EnumObject(Type type) : base(type)
1313
{

0 commit comments

Comments
 (0)