Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitefad01c

Browse files
authored
provide__int__ instance method on .NET enum types to support int(Enum.Member) (#1661)
implements#1585
1 parent7e5cc29 commitefad01c

File tree

7 files changed

+58
-11
lines changed

7 files changed

+58
-11
lines changed

‎src/runtime/classmanager.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,25 @@ private static ClassInfo GetClassInfo(Type type, ClassBase impl)
346346
}
347347
}
348348

349-
// only [Flags] enums support bitwise operations
350-
if(type.IsEnum&&type.IsFlagsEnum())
349+
if(type.IsEnum)
351350
{
352351
varopsImpl=typeof(EnumOps<>).MakeGenericType(type);
353352
foreach(varopinopsImpl.GetMethods(OpsHelper.BindingFlags))
354353
{
355354
local.Add(op.Name);
356355
}
357356
info=info.Concat(opsImpl.GetMethods(OpsHelper.BindingFlags)).ToArray();
357+
358+
// only [Flags] enums support bitwise operations
359+
if(type.IsFlagsEnum())
360+
{
361+
opsImpl=typeof(FlagEnumOps<>).MakeGenericType(type);
362+
foreach(varopinopsImpl.GetMethods(OpsHelper.BindingFlags))
363+
{
364+
local.Add(op.Name);
365+
}
366+
info=info.Concat(opsImpl.GetMethods(OpsHelper.BindingFlags)).ToArray();
367+
}
358368
}
359369

360370
// Now again to filter w/o losing overloaded member info

‎src/runtime/native/ITypeOffsets.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ interface ITypeOffsets
2121
intnb_multiply{get;}
2222
intnb_true_divide{get;}
2323
intnb_and{get;}
24+
intnb_int{get;}
2425
intnb_or{get;}
2526
intnb_xor{get;}
2627
intnb_lshift{get;}

‎src/runtime/native/TypeOffset.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ static partial class TypeOffset
3030
internalstaticintnb_and{get;privateset;}
3131
internalstaticintnb_or{get;privateset;}
3232
internalstaticintnb_xor{get;privateset;}
33+
internalstaticintnb_int{get;privateset;}
3334
internalstaticintnb_lshift{get;privateset;}
3435
internalstaticintnb_rshift{get;privateset;}
3536
internalstaticintnb_remainder{get;privateset;}

‎src/runtime/operatormethod.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
usingSystem;
22
usingSystem.Collections.Generic;
33
usingSystem.Diagnostics;
4+
usingSystem.Linq;
45
usingSystem.Reflection;
5-
usingSystem.Runtime.InteropServices;
66
usingSystem.Text;
77

88
namespacePython.Runtime
@@ -51,6 +51,8 @@ static OperatorMethod()
5151
["op_OnesComplement"]=newSlotDefinition("__invert__",TypeOffset.nb_invert),
5252
["op_UnaryNegation"]=newSlotDefinition("__neg__",TypeOffset.nb_negative),
5353
["op_UnaryPlus"]=newSlotDefinition("__pos__",TypeOffset.nb_positive),
54+
55+
["__int__"]=newSlotDefinition("__int__",TypeOffset.nb_int),
5456
};
5557
ComparisonOpMap=newDictionary<string,string>
5658
{
@@ -97,14 +99,11 @@ public static bool IsComparisonOp(MethodBase method)
9799
/// </summary>
98100
publicstaticvoidFixupSlots(BorrowedReferencepyType,TypeclrType)
99101
{
100-
constBindingFlagsflags=BindingFlags.Public|BindingFlags.Static;
101102
Debug.Assert(_opType!=null);
102103

103-
varstaticMethods=
104-
clrType.IsEnum?typeof(EnumOps<>).MakeGenericType(clrType).GetMethods(flags)
105-
:clrType.GetMethods(flags);
104+
varoperatorCandidates=GetOperatorCandidates(clrType);
106105

107-
foreach(varmethodinstaticMethods)
106+
foreach(varmethodinoperatorCandidates)
108107
{
109108
// We only want to override slots for operators excluding
110109
// comparison operators, which are handled by ClassBase.tp_richcompare.
@@ -124,6 +123,18 @@ public static void FixupSlots(BorrowedReference pyType, Type clrType)
124123
}
125124
}
126125

126+
staticIEnumerable<MethodInfo>GetOperatorCandidates(TypeclrType)
127+
{
128+
constBindingFlagsflags=BindingFlags.Public|BindingFlags.Static;
129+
if(clrType.IsEnum)
130+
{
131+
returntypeof(EnumOps<>).MakeGenericType(clrType).GetMethods(flags)
132+
.Concat(typeof(FlagEnumOps<>).MakeGenericType(clrType).GetMethods(flags));
133+
}
134+
135+
returnclrType.GetMethods(flags);
136+
}
137+
127138
publicstaticstringGetPyMethodName(stringclrName)
128139
{
129140
if(OpMethodMap.ContainsKey(clrName))

‎src/runtime/opshelper.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static Expression EnumUnderlyingValue(Expression enumValue)
3838
internalclassOpsAttribute:Attribute{}
3939

4040
[Ops]
41-
internalstaticclassEnumOps<T>whereT:Enum
41+
internalstaticclassFlagEnumOps<T>whereT:Enum
4242
{
4343
staticreadonlyFunc<T,T,T>and=BinaryOp(Expression.And);
4444
staticreadonlyFunc<T,T,T>or=BinaryOp(Expression.Or);
@@ -74,4 +74,16 @@ static Func<T, T> UnaryOp(Func<Expression, UnaryExpression> op)
7474
});
7575
}
7676
}
77+
78+
[Ops]
79+
internalstaticclassEnumOps<T>whereT:Enum
80+
{
81+
[ForbidPythonThreads]
82+
#pragma warning disableIDE1006// Naming Styles - must match Python
83+
publicstaticPyInt__int__(Tvalue)
84+
#pragma warning restoreIDE1006// Naming Styles
85+
=>typeof(T).GetEnumUnderlyingType()==typeof(UInt64)
86+
?newPyInt(Convert.ToUInt64(value))
87+
:newPyInt(Convert.ToInt64(value));
88+
}
7789
}

‎src/testing/enumtest.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ public enum LongEnum : long
7272
Two,
7373
Three,
7474
Four,
75-
Five
75+
Five,
76+
Max=long.MaxValue,
77+
Min=long.MinValue,
7678
}
7779

7880
publicenumULongEnum:ulong
@@ -82,7 +84,8 @@ public enum ULongEnum : ulong
8284
Two,
8385
Three,
8486
Four,
85-
Five
87+
Five,
88+
Max=ulong.MaxValue,
8689
}
8790

8891
[Flags]

‎tests/test_enum.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ def test_ulong_enum():
8787
assertTest.ULongEnum.Two==Test.ULongEnum(2)
8888

8989

90+
deftest_long_enum_to_int():
91+
assertint(Test.LongEnum.Max)==9223372036854775807
92+
assertint(Test.LongEnum.Min)==-9223372036854775808
93+
94+
95+
deftest_ulong_enum_to_int():
96+
assertint(Test.ULongEnum.Max)==18446744073709551615
97+
98+
9099
deftest_instantiate_enum_fails():
91100
"""Test that instantiation of an enum class fails."""
92101
fromSystemimportDayOfWeek

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp