diff --git a/src/runtime/classmanager.cs b/src/runtime/classmanager.cs index bfc07874f..c0932d06c 100644 --- a/src/runtime/classmanager.cs +++ b/src/runtime/classmanager.cs @@ -345,8 +345,8 @@ private static ClassInfo GetClassInfo(Type type) } } - // only [Flags] enums support bitwise operations - if (type.IsEnum && type.IsFlagsEnum()) + + if (type.IsEnum) { var opsImpl = typeof(EnumOps<>).MakeGenericType(type); foreach (var op in opsImpl.GetMethods(OpsHelper.BindingFlags)) @@ -354,6 +354,17 @@ private static ClassInfo GetClassInfo(Type type) local.Add(op.Name); } info = info.Concat(opsImpl.GetMethods(OpsHelper.BindingFlags)).ToArray(); + + // only [Flags] enums support bitwise operations + if (type.IsFlagsEnum()) + { + opsImpl = typeof(FlagEnumOps<>).MakeGenericType(type); + foreach (var op in opsImpl.GetMethods(OpsHelper.BindingFlags)) + { + local.Add(op.Name); + } + info = info.Concat(opsImpl.GetMethods(OpsHelper.BindingFlags)).ToArray(); + } } // Now again to filter w/o losing overloaded member info diff --git a/src/runtime/operatormethod.cs b/src/runtime/operatormethod.cs index aad3f013f..b506c255e 100644 --- a/src/runtime/operatormethod.cs +++ b/src/runtime/operatormethod.cs @@ -101,7 +101,7 @@ public static void FixupSlots(BorrowedReference pyType, Type clrType) Debug.Assert(_opType != null); var staticMethods = - clrType.IsEnum ? typeof(EnumOps<>).MakeGenericType(clrType).GetMethods(flags) + clrType.IsEnum ? typeof(FlagEnumOps<>).MakeGenericType(clrType).GetMethods(flags) : clrType.GetMethods(flags); foreach (var method in staticMethods) diff --git a/src/runtime/opshelper.cs b/src/runtime/opshelper.cs index 59f7704b7..ab623f3de 100644 --- a/src/runtime/opshelper.cs +++ b/src/runtime/opshelper.cs @@ -38,7 +38,7 @@ public static Expression EnumUnderlyingValue(Expression enumValue) internal class OpsAttribute: Attribute { } [Ops] - internal static class EnumOps where T : Enum + internal static class FlagEnumOps where T : Enum { static readonly Func and = BinaryOp(Expression.And); static readonly Func or = BinaryOp(Expression.Or); @@ -74,4 +74,16 @@ static Func UnaryOp(Func op) }); } } + + [Ops] + internal static class EnumOps where T : Enum + { + [ForbidPythonThreads] +#pragma warning disable IDE1006 // Naming Styles - must match Python + public static PyInt __int__(T value) +#pragma warning restore IDE1006 // Naming Styles + => typeof(T).GetEnumUnderlyingType() == typeof(UInt64) + ? new PyInt(Convert.ToUInt64(value)) + : new PyInt(Convert.ToInt64(value)); + } } diff --git a/src/testing/enumtest.cs b/src/testing/enumtest.cs index de5d8f5ee..f7d07339f 100644 --- a/src/testing/enumtest.cs +++ b/src/testing/enumtest.cs @@ -72,7 +72,9 @@ public enum LongEnum : long Two, Three, Four, - Five + Five, + Max = long.MaxValue, + Min = long.MinValue, } public enum ULongEnum : ulong @@ -82,7 +84,8 @@ public enum ULongEnum : ulong Two, Three, Four, - Five + Five, + Max = ulong.MaxValue, } [Flags] diff --git a/tests/test_enum.py b/tests/test_enum.py index 1f0711a94..b2eb0569f 100644 --- a/tests/test_enum.py +++ b/tests/test_enum.py @@ -87,6 +87,15 @@ def test_ulong_enum(): assert Test.ULongEnum.Two == Test.ULongEnum(2) +def test_long_enum_to_int(): + assert int(Test.LongEnum.Max) == 9223372036854775807 + assert int(Test.LongEnum.Min) == -9223372036854775808 + + +def test_ulong_enum_to_int(): + assert int(Test.ULongEnum.Max) == 18446744073709551615 + + def test_instantiate_enum_fails(): """Test that instantiation of an enum class fails.""" from System import DayOfWeek