Skip to content

Commit b9d601c

Browse files
committed
Minor improvements
1 parent 1869c7c commit b9d601c

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

src/runtime/Types/EnumObject.cs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ namespace Python.Runtime
99
[Serializable]
1010
internal class EnumObject : ClassBase
1111
{
12-
private bool _isUnsigned;
13-
1412
internal EnumObject(Type type) : base(type)
1513
{
16-
_isUnsigned = type.GetEnumUnderlyingType() == typeof(UInt64);
1714
}
1815

1916
/// <summary>
@@ -24,7 +21,6 @@ public static NewReference tp_richcompare(BorrowedReference ob, BorrowedReferenc
2421
object rightInstance;
2522
CLRObject leftClrObject;
2623
int comparisonResult;
27-
EnumObject leftClass;
2824

2925
switch (op)
3026
{
@@ -50,9 +46,8 @@ public static NewReference tp_richcompare(BorrowedReference ob, BorrowedReferenc
5046
return new NewReference(pyfalse);
5147
}
5248

53-
leftClass = (EnumObject)GetManagedObject(Runtime.PyObject_TYPE(ob))!;
5449
if (rightInstance != null &&
55-
TryCompare(leftClrObject.inst as Enum, rightInstance, leftClass._isUnsigned, out comparisonResult) &&
50+
TryCompare(leftClrObject.inst as Enum, rightInstance, out comparisonResult) &&
5651
comparisonResult == 0)
5752
{
5853
return new NewReference(pytrue);
@@ -76,10 +71,9 @@ public static NewReference tp_richcompare(BorrowedReference ob, BorrowedReferenc
7671
return Exceptions.RaiseTypeError($"Cannot compare {leftClrObject.inst.GetType()} to None");
7772
}
7873

79-
leftClass = (EnumObject)GetManagedObject(Runtime.PyObject_TYPE(ob))!;
8074
try
8175
{
82-
if (!TryCompare(leftClrObject.inst as Enum, rightInstance, leftClass._isUnsigned, out comparisonResult))
76+
if (!TryCompare(leftClrObject.inst as Enum, rightInstance, out comparisonResult))
8377
{
8478
return Exceptions.RaiseTypeError($"Cannot compare {leftClrObject.inst.GetType()} with {rightInstance.GetType()}");
8579
}
@@ -100,47 +94,54 @@ public static NewReference tp_richcompare(BorrowedReference ob, BorrowedReferenc
10094
/// Tries comparing the give enum to the right operand by converting it to the appropriate type if possible
10195
/// </summary>
10296
/// <returns>True if the right operand was converted to a supported type and the comparison was performed successfully</returns>
103-
private static bool TryCompare(Enum left, object right, bool isUnsigned, out int result)
97+
private static bool TryCompare(Enum left, object right, out int result)
10498
{
10599
result = int.MinValue;
106100
var conversionSuccessful = true;
101+
var leftType = left.GetType();
102+
var rightType = right.GetType();
107103
// Same enum comparison:
108-
if (left.GetType() == right.GetType())
104+
if (leftType == rightType)
109105
{
110106
result = left.CompareTo(right);
111107
}
112108
// Comparison with other enum types
113-
else if (right.GetType().IsEnum)
114-
{
115-
result = Compare(left, right as Enum, isUnsigned);
116-
}
117-
else if (right is double rightDouble)
118-
{
119-
result = Compare(left, rightDouble, isUnsigned);
120-
}
121-
else if (right is long rightLong)
122-
{
123-
result = Compare(left, rightLong, isUnsigned);
124-
}
125-
else if (right is ulong rightULong)
126-
{
127-
result = Compare(left, rightULong, isUnsigned);
128-
}
129-
else if (right is int rightInt)
130-
{
131-
result = Compare(left, rightInt, isUnsigned);
132-
}
133-
else if (right is uint rightUInt)
109+
else if (rightType.IsEnum)
134110
{
135-
result = Compare(left, rightUInt, isUnsigned);
111+
var leftIsUnsigned = leftType.GetEnumUnderlyingType() == typeof(UInt64);
112+
result = Compare(left, right as Enum, leftIsUnsigned);
136113
}
137114
else if (right is string rightString)
138115
{
139116
result = left.ToString().CompareTo(rightString);
140117
}
141118
else
142119
{
143-
conversionSuccessful = false;
120+
var leftIsUnsigned = leftType.GetEnumUnderlyingType() == typeof(UInt64);
121+
if (right is double rightDouble)
122+
{
123+
result = Compare(left, rightDouble, leftIsUnsigned);
124+
}
125+
else if (right is long rightLong)
126+
{
127+
result = Compare(left, rightLong, leftIsUnsigned);
128+
}
129+
else if (right is ulong rightULong)
130+
{
131+
result = Compare(left, rightULong, leftIsUnsigned);
132+
}
133+
else if (right is int rightInt)
134+
{
135+
result = Compare(left, rightInt, leftIsUnsigned);
136+
}
137+
else if (right is uint rightUInt)
138+
{
139+
result = Compare(left, rightUInt, leftIsUnsigned);
140+
}
141+
else
142+
{
143+
conversionSuccessful = false;
144+
}
144145
}
145146

146147
return conversionSuccessful;

0 commit comments

Comments
 (0)