@@ -12,30 +12,35 @@ internal static class OperatorMethod
12
12
// Maps the compiled method name in .NET CIL (e.g. op_Addition) to
13
13
// the equivalent Python operator (e.g. __add__) as well as the offset
14
14
// that identifies that operator's slot (e.g. nb_add) in heap space.
15
- public static Dictionary < string , Tuple < string , int > > OpMethodMap { get ; private set ; }
16
-
15
+ public static Dictionary < string , SlotDefinition > OpMethodMap { get ; private set ; }
16
+ public readonly struct SlotDefinition
17
+ {
18
+ public SlotDefinition ( string methodName , int typeOffset )
19
+ {
20
+ MethodName = methodName ;
21
+ TypeOffset = typeOffset ;
22
+ }
23
+ public string MethodName { get ; }
24
+ public int TypeOffset { get ; }
25
+ }
17
26
private static PyObject _opType ;
18
27
19
28
static OperatorMethod ( )
20
29
{
21
30
// TODO: Rich compare, inplace operator support
22
- OpMethodMap = new Dictionary < string , Tuple < string , int > >
31
+ OpMethodMap = new Dictionary < string , SlotDefinition >
23
32
{
24
- [ "op_Addition" ] = Tuple . Create ( "__add__" , TypeOffset . nb_add ) ,
25
- [ "op_Subtraction" ] = Tuple . Create ( "__sub__" , TypeOffset . nb_subtract ) ,
26
- [ "op_Multiply" ] = Tuple . Create ( "__mul__" , TypeOffset . nb_multiply ) ,
27
- #if PYTHON2
28
- [ "op_Division" ] = Tuple . Create ( "__div__" , TypeOffset . nb_divide ) ,
29
- #else
30
- [ "op_Division" ] = Tuple . Create ( "__truediv__" , TypeOffset . nb_true_divide ) ,
31
- #endif
32
- [ "op_BitwiseAnd" ] = Tuple . Create ( "__and__" , TypeOffset . nb_and ) ,
33
- [ "op_BitwiseOr" ] = Tuple . Create ( "__or__" , TypeOffset . nb_or ) ,
34
- [ "op_ExclusiveOr" ] = Tuple . Create ( "__xor__" , TypeOffset . nb_xor ) ,
35
- [ "op_LeftShift" ] = Tuple . Create ( "__lshift__" , TypeOffset . nb_lshift ) ,
36
- [ "op_RightShift" ] = Tuple . Create ( "__rshift__" , TypeOffset . nb_rshift ) ,
37
- [ "op_Modulus" ] = Tuple . Create ( "__mod__" , TypeOffset . nb_remainder ) ,
38
- [ "op_OneComplement" ] = Tuple . Create ( "__invert__" , TypeOffset . nb_invert )
33
+ [ "op_Addition" ] = new SlotDefinition ( "__add__" , TypeOffset . nb_add ) ,
34
+ [ "op_Subtraction" ] = new SlotDefinition ( "__sub__" , TypeOffset . nb_subtract ) ,
35
+ [ "op_Multiply" ] = new SlotDefinition ( "__mul__" , TypeOffset . nb_multiply ) ,
36
+ [ "op_Division" ] = new SlotDefinition ( "__truediv__" , TypeOffset . nb_true_divide ) ,
37
+ [ "op_BitwiseAnd" ] = new SlotDefinition ( "__and__" , TypeOffset . nb_and ) ,
38
+ [ "op_BitwiseOr" ] = new SlotDefinition ( "__or__" , TypeOffset . nb_or ) ,
39
+ [ "op_ExclusiveOr" ] = new SlotDefinition ( "__xor__" , TypeOffset . nb_xor ) ,
40
+ [ "op_LeftShift" ] = new SlotDefinition ( "__lshift__" , TypeOffset . nb_lshift ) ,
41
+ [ "op_RightShift" ] = new SlotDefinition ( "__rshift__" , TypeOffset . nb_rshift ) ,
42
+ [ "op_Modulus" ] = new SlotDefinition ( "__mod__" , TypeOffset . nb_remainder ) ,
43
+ [ "op_OneComplement" ] = new SlotDefinition ( "__invert__" , TypeOffset . nb_invert )
39
44
} ;
40
45
}
41
46
@@ -53,11 +58,6 @@ public static void Shutdown()
53
58
}
54
59
}
55
60
56
- public static bool IsOperatorMethod ( string methodName )
57
- {
58
- return OpMethodMap . ContainsKey ( methodName ) ;
59
- }
60
-
61
61
public static bool IsOperatorMethod ( MethodBase method )
62
62
{
63
63
if ( ! method . IsSpecialName )
@@ -82,7 +82,7 @@ public static void FixupSlots(IntPtr pyType, Type clrType)
82
82
{
83
83
continue ;
84
84
}
85
- int offset = OpMethodMap [ method . Name ] . Item2 ;
85
+ int offset = OpMethodMap [ method . Name ] . TypeOffset ;
86
86
// Copy the default implementation of e.g. the nb_add slot,
87
87
// which simply calls __add__ on the type.
88
88
IntPtr func = Marshal . ReadIntPtr ( _opType . Handle , offset ) ;
@@ -97,7 +97,7 @@ public static void FixupSlots(IntPtr pyType, Type clrType)
97
97
98
98
public static string GetPyMethodName ( string clrName )
99
99
{
100
- return OpMethodMap [ clrName ] . Item1 ;
100
+ return OpMethodMap [ clrName ] . MethodName ;
101
101
}
102
102
103
103
private static string GenerateDummyCode ( )
@@ -106,7 +106,7 @@ private static string GenerateDummyCode()
106
106
sb . AppendLine ( "class OperatorMethod(object):" ) ;
107
107
foreach ( var item in OpMethodMap . Values )
108
108
{
109
- string def = string . Format ( " def {0}(self, other): pass" , item . Item1 ) ;
109
+ string def = string . Format ( " def {0}(self, other): pass" , item . MethodName ) ;
110
110
sb . AppendLine ( def ) ;
111
111
}
112
112
return sb . ToString ( ) ;
0 commit comments