From efad01cf2efc4f8355ca0834ea347dd7a48458d2 Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 6 Jan 2022 01:24:15 -0800 Subject: [PATCH] provide `__int__` instance method on .NET enum types to support int(Enum.Member) (#1661) implements https://github.com/pythonnet/pythonnet/issues/1585 --- src/runtime/classmanager.cs | 14 ++++++++++++-- src/runtime/native/ITypeOffsets.cs | 1 + src/runtime/native/TypeOffset.cs | 1 + src/runtime/operatormethod.cs | 23 +++++++++++++++++------ src/runtime/opshelper.cs | 14 +++++++++++++- src/testing/enumtest.cs | 7 +++++-- tests/test_enum.py | 9 +++++++++ 7 files changed, 58 insertions(+), 11 deletions(-) diff --git a/src/runtime/classmanager.cs b/src/runtime/classmanager.cs index f8e108f..647cec3 100644 --- a/src/runtime/classmanager.cs +++ b/src/runtime/classmanager.cs @@ -346,8 +346,7 @@ namespace Python.Runtime } } - // 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)) @@ -355,6 +354,17 @@ namespace Python.Runtime 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/native/ITypeOffsets.cs b/src/runtime/native/ITypeOffsets.cs index 0829e5b..2c4fdf5 100644 --- a/src/runtime/native/ITypeOffsets.cs +++ b/src/runtime/native/ITypeOffsets.cs @@ -21,6 +21,7 @@ namespace Python.Runtime.Native int nb_multiply { get; } int nb_true_divide { get; } int nb_and { get; } + int nb_int { get; } int nb_or { get; } int nb_xor { get; } int nb_lshift { get; } diff --git a/src/runtime/native/TypeOffset.cs b/src/runtime/native/TypeOffset.cs index 3f97b0f..a1bae82 100644 --- a/src/runtime/native/TypeOffset.cs +++ b/src/runtime/native/TypeOffset.cs @@ -30,6 +30,7 @@ namespace Python.Runtime internal static int nb_and { get; private set; } internal static int nb_or { get; private set; } internal static int nb_xor { get; private set; } + internal static int nb_int { get; private set; } internal static int nb_lshift { get; private set; } internal static int nb_rshift { get; private set; } internal static int nb_remainder { get; private set; } diff --git a/src/runtime/operatormethod.cs b/src/runtime/operatormethod.cs index 035198f..abe6ded 100644 --- a/src/runtime/operatormethod.cs +++ b/src/runtime/operatormethod.cs @@ -1,8 +1,8 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Linq; using System.Reflection; -using System.Runtime.InteropServices; using System.Text; namespace Python.Runtime @@ -51,6 +51,8 @@ namespace Python.Runtime ["op_OnesComplement"] = new SlotDefinition("__invert__", TypeOffset.nb_invert), ["op_UnaryNegation"] = new SlotDefinition("__neg__", TypeOffset.nb_negative), ["op_UnaryPlus"] = new SlotDefinition("__pos__", TypeOffset.nb_positive), + + ["__int__"] = new SlotDefinition("__int__", TypeOffset.nb_int), }; ComparisonOpMap = new Dictionary { @@ -97,14 +99,11 @@ namespace Python.Runtime /// public static void FixupSlots(BorrowedReference pyType, Type clrType) { - const BindingFlags flags = BindingFlags.Public | BindingFlags.Static; Debug.Assert(_opType != null); - var staticMethods = - clrType.IsEnum ? typeof(EnumOps<>).MakeGenericType(clrType).GetMethods(flags) - : clrType.GetMethods(flags); + var operatorCandidates = GetOperatorCandidates(clrType); - foreach (var method in staticMethods) + foreach (var method in operatorCandidates) { // We only want to override slots for operators excluding // comparison operators, which are handled by ClassBase.tp_richcompare. @@ -124,6 +123,18 @@ namespace Python.Runtime } } + static IEnumerable GetOperatorCandidates(Type clrType) + { + const BindingFlags flags = BindingFlags.Public | BindingFlags.Static; + if (clrType.IsEnum) + { + return typeof(EnumOps<>).MakeGenericType(clrType).GetMethods(flags) + .Concat(typeof(FlagEnumOps<>).MakeGenericType(clrType).GetMethods(flags)); + } + + return clrType.GetMethods(flags); + } + public static string GetPyMethodName(string clrName) { if (OpMethodMap.ContainsKey(clrName)) diff --git a/src/runtime/opshelper.cs b/src/runtime/opshelper.cs index 59f7704..ab623f3 100644 --- a/src/runtime/opshelper.cs +++ b/src/runtime/opshelper.cs @@ -38,7 +38,7 @@ namespace Python.Runtime 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 @@ namespace Python.Runtime }); } } + + [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 de5d8f5..f7d0733 100644 --- a/src/testing/enumtest.cs +++ b/src/testing/enumtest.cs @@ -72,7 +72,9 @@ namespace Python.Test Two, Three, Four, - Five + Five, + Max = long.MaxValue, + Min = long.MinValue, } public enum ULongEnum : ulong @@ -82,7 +84,8 @@ namespace Python.Test Two, Three, Four, - Five + Five, + Max = ulong.MaxValue, } [Flags] diff --git a/tests/test_enum.py b/tests/test_enum.py index 1f0711a..b2eb056 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