1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Diagnostics ;
4
+ using System . Linq ;
4
5
using System . Reflection ;
5
- using System . Runtime . InteropServices ;
6
6
using System . Text ;
7
7
8
8
namespace Python . Runtime
@@ -51,6 +51,8 @@ static OperatorMethod()
51
51
[ "op_OnesComplement" ] = new SlotDefinition ( "__invert__" , TypeOffset . nb_invert ) ,
52
52
[ "op_UnaryNegation" ] = new SlotDefinition ( "__neg__" , TypeOffset . nb_negative ) ,
53
53
[ "op_UnaryPlus" ] = new SlotDefinition ( "__pos__" , TypeOffset . nb_positive ) ,
54
+
55
+ [ "__int__" ] = new SlotDefinition ( "__int__" , TypeOffset . nb_int ) ,
54
56
} ;
55
57
ComparisonOpMap = new Dictionary < string , string >
56
58
{
@@ -97,14 +99,11 @@ public static bool IsComparisonOp(MethodBase method)
97
99
/// </summary>
98
100
public static void FixupSlots ( BorrowedReference pyType , Type clrType )
99
101
{
100
- const BindingFlags flags = BindingFlags . Public | BindingFlags . Static ;
101
102
Debug . Assert ( _opType != null ) ;
102
103
103
- var staticMethods =
104
- clrType . IsEnum ? typeof ( EnumOps < > ) . MakeGenericType ( clrType ) . GetMethods ( flags )
105
- : clrType . GetMethods ( flags ) ;
104
+ var operatorCandidates = GetOperatorCandidates ( clrType ) ;
106
105
107
- foreach ( var method in staticMethods )
106
+ foreach ( var method in operatorCandidates )
108
107
{
109
108
// We only want to override slots for operators excluding
110
109
// comparison operators, which are handled by ClassBase.tp_richcompare.
@@ -124,6 +123,18 @@ public static void FixupSlots(BorrowedReference pyType, Type clrType)
124
123
}
125
124
}
126
125
126
+ static IEnumerable < MethodInfo > GetOperatorCandidates ( Type clrType )
127
+ {
128
+ const BindingFlags flags = BindingFlags . Public | BindingFlags . Static ;
129
+ if ( clrType . IsEnum )
130
+ {
131
+ return typeof ( EnumOps < > ) . MakeGenericType ( clrType ) . GetMethods ( flags )
132
+ . Concat ( typeof ( FlagEnumOps < > ) . MakeGenericType ( clrType ) . GetMethods ( flags ) ) ;
133
+ }
134
+
135
+ return clrType . GetMethods ( flags ) ;
136
+ }
137
+
127
138
public static string GetPyMethodName ( string clrName )
128
139
{
129
140
if ( OpMethodMap . ContainsKey ( clrName ) )
0 commit comments