Skip to content

Commit 98828a1

Browse files
committed
Reduce enum operators overloads
1 parent 57c2299 commit 98828a1

File tree

2 files changed

+18
-206
lines changed

2 files changed

+18
-206
lines changed

src/embed_tests/EnumTests.cs

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -544,89 +544,5 @@ def are_same7():
544544
Assert.IsFalse(module.InvokeMethod("are_same6").As<bool>());
545545
Assert.IsFalse(module.InvokeMethod("are_same7").As<bool>());
546546
}
547-
548-
private PyModule GetCSharpObjectsComparisonTestModule(string @operator)
549-
{
550-
return PyModule.FromString("GetCSharpObjectsComparisonTestModule", $@"
551-
from clr import AddReference
552-
AddReference(""Python.EmbeddingTest"")
553-
554-
from Python.EmbeddingTest import *
555-
556-
enum_value = {nameof(EnumTests)}.{nameof(VerticalDirection)}.{VerticalDirection.Up}
557-
558-
def compare_with_none1():
559-
return enum_value {@operator} None
560-
561-
def compare_with_none2():
562-
return None {@operator} enum_value
563-
564-
def compare_with_csharp_object1(csharp_object):
565-
return enum_value {@operator} csharp_object
566-
567-
def compare_with_csharp_object2(csharp_object):
568-
return csharp_object {@operator} enum_value
569-
");
570-
}
571-
572-
[TestCase("==", false)]
573-
[TestCase("!=", true)]
574-
public void EqualityComparisonWithNull(string @operator, bool expectedResult)
575-
{
576-
using var _ = Py.GIL();
577-
using var module = GetCSharpObjectsComparisonTestModule(@operator);
578-
579-
Assert.AreEqual(expectedResult, module.InvokeMethod("compare_with_none1").As<bool>());
580-
Assert.AreEqual(expectedResult, module.InvokeMethod("compare_with_none2").As<bool>());
581-
582-
using var pyNull = ((TestClass)null).ToPython();
583-
Assert.AreEqual(expectedResult, module.InvokeMethod("compare_with_csharp_object1", pyNull).As<bool>());
584-
Assert.AreEqual(expectedResult, module.InvokeMethod("compare_with_csharp_object2", pyNull).As<bool>());
585-
}
586-
587-
[Test]
588-
public void SortingComparisonWithNullThrows([Values("<", "<=", ">", ">=")] string @operator)
589-
{
590-
using var _ = Py.GIL();
591-
using var module = GetCSharpObjectsComparisonTestModule(@operator);
592-
593-
using var pyNull = ((TestClass)null).ToPython();
594-
595-
var exception = Assert.Throws<PythonException>(() => module.InvokeMethod("compare_with_csharp_object1", pyNull));
596-
Assert.IsTrue(exception.Message.Contains("Cannot compare"));
597-
Assert.IsTrue(exception.Message.Contains("with null"));
598-
}
599-
600-
private static IEnumerable<TestCaseData> ComparisonWithNonEnumObjectsTestCases
601-
{
602-
get
603-
{
604-
foreach (var op in new[] { "==", "!=" })
605-
{
606-
yield return new TestCaseData(op, new[] { "No method matched to compare" });
607-
}
608-
609-
foreach (var op in new[] { "<", "<=", ">", ">=" })
610-
{
611-
yield return new TestCaseData(op, new[] { "Cannot compare", "with null" });
612-
}
613-
}
614-
}
615-
616-
[Test]
617-
public void ComparisonOperatorsWithNonEnumObjectsThrows([Values("==", "!=", "<", "<=", ">", ">=")] string @operator)
618-
{
619-
using var _ = Py.GIL();
620-
using var module = GetCSharpObjectsComparisonTestModule(@operator);
621-
622-
using var pyCSharpObject = new TestClass().ToPython();
623-
624-
var exception = Assert.Throws<PythonException>(() => module.InvokeMethod("compare_with_csharp_object1", pyCSharpObject));
625-
Assert.IsTrue(exception.Message.Contains("No method matched"), $"Expected exception message to contain 'No method matched' but got: {exception.Message}");
626-
}
627-
628-
public class TestClass
629-
{
630-
}
631547
}
632548
}

src/runtime/Util/OpsHelper.cs

Lines changed: 18 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -418,56 +418,6 @@ public static bool op_GreaterThanOrEqual(double a, T b)
418418

419419
#endregion
420420

421-
#region Same type comparison operators
422-
423-
public static bool op_Equality(T a, T b)
424-
{
425-
return a.Equals(b);
426-
}
427-
428-
public static bool op_Inequality(T a, T b)
429-
{
430-
return !a.Equals(b);
431-
}
432-
433-
public static bool op_LessThan(T a, T b)
434-
{
435-
if (IsUnsigned)
436-
{
437-
return Convert.ToUInt64(a) < Convert.ToUInt64(b);
438-
}
439-
return Convert.ToInt64(a) < Convert.ToInt64(b);
440-
}
441-
442-
public static bool op_GreaterThan(T a, T b)
443-
{
444-
if (IsUnsigned)
445-
{
446-
return Convert.ToUInt64(a) > Convert.ToUInt64(b);
447-
}
448-
return Convert.ToInt64(a) > Convert.ToInt64(b);
449-
}
450-
451-
public static bool op_LessThanOrEqual(T a, T b)
452-
{
453-
if (IsUnsigned)
454-
{
455-
return Convert.ToUInt64(a) <= Convert.ToUInt64(b);
456-
}
457-
return Convert.ToInt64(a) <= Convert.ToInt64(b);
458-
}
459-
460-
public static bool op_GreaterThanOrEqual(T a, T b)
461-
{
462-
if (IsUnsigned)
463-
{
464-
return Convert.ToUInt64(a) >= Convert.ToUInt64(b);
465-
}
466-
return Convert.ToInt64(a) >= Convert.ToInt64(b);
467-
}
468-
469-
#endregion
470-
471421
#region String comparison operators
472422
public static bool op_Equality(T a, string b)
473423
{
@@ -490,138 +440,84 @@ public static bool op_Inequality(string a, T b)
490440

491441
#endregion
492442

493-
#region Other Enum comparison operators
494-
495-
private static bool IsEnum(object b, out Type type, out Type underlyingType, bool throwOnNull = false)
496-
{
497-
type = null;
498-
underlyingType = null;
499-
if (b == null)
500-
{
501-
if (throwOnNull)
502-
{
503-
using (Py.GIL())
504-
{
505-
Exceptions.RaiseTypeError($"Cannot compare {typeof(T).Name} with null");
506-
PythonException.ThrowLastAsClrException();
507-
}
508-
}
509-
return false;
510-
}
511-
512-
type = b.GetType();
513-
if (type.IsEnum)
514-
{
515-
underlyingType = type.GetEnumUnderlyingType();
516-
return true;
517-
}
518-
using var _ = Py.GIL();
519-
Exceptions.RaiseTypeError($"No method matched to compare {typeof(T).Name} and {type.Name}");
520-
PythonException.ThrowLastAsClrException();
521-
522-
return false;
523-
}
443+
#region Enum comparison operators
524444

525-
public static bool op_Equality(T a, object b)
445+
public static bool op_Equality(T a, Enum b)
526446
{
527-
if (!IsEnum(b, out var bType, out var underlyingType))
528-
{
529-
return false;
530-
}
531-
if (underlyingType == typeof(UInt64))
447+
if (b.GetType().GetEnumUnderlyingType() == typeof(UInt64))
532448
{
533449
return op_Equality(a, Convert.ToUInt64(b));
534450
}
535451
return op_Equality(a, Convert.ToInt64(b));
536452
}
537453

538-
public static bool op_Equality(object a, T b)
454+
public static bool op_Equality(Enum a, T b)
539455
{
540456
return op_Equality(b, a);
541457
}
542458

543-
public static bool op_Inequality(T a, object b)
459+
public static bool op_Inequality(T a, Enum b)
544460
{
545461
return !op_Equality(a, b);
546462
}
547463

548-
public static bool op_Inequality(object a, T b)
464+
public static bool op_Inequality(Enum a, T b)
549465
{
550466
return !op_Equality(b, a);
551467
}
552468

553-
public static bool op_LessThan(T a, object b)
469+
public static bool op_LessThan(T a, Enum b)
554470
{
555-
if (!IsEnum(b, out var bType, out var underlyingType, throwOnNull: true))
556-
{
557-
// False although it means nothing: an exception will be raised
558-
return false;
559-
}
560-
if (underlyingType == typeof(UInt64))
471+
if (b.GetType().GetEnumUnderlyingType() == typeof(UInt64))
561472
{
562473
return op_LessThan(a, Convert.ToUInt64(b));
563474
}
564475
return op_LessThan(a, Convert.ToInt64(b));
565476
}
566477

567-
public static bool op_LessThan(object a, T b)
478+
public static bool op_LessThan(Enum a, T b)
568479
{
569480
return op_GreaterThan(b, a);
570481
}
571482

572-
public static bool op_GreaterThan(T a, object b)
483+
public static bool op_GreaterThan(T a, Enum b)
573484
{
574-
if (!IsEnum(b, out var bType, out var underlyingType, throwOnNull: true))
575-
{
576-
// False although it means nothing: an exception will be raised
577-
return false;
578-
}
579-
if (underlyingType == typeof(UInt64))
485+
if (b.GetType().GetEnumUnderlyingType() == typeof(UInt64))
580486
{
581487
return op_GreaterThan(a, Convert.ToUInt64(b));
582488
}
583489
return op_GreaterThan(a, Convert.ToInt64(b));
584490
}
585491

586-
public static bool op_GreaterThan(object a, T b)
492+
public static bool op_GreaterThan(Enum a, T b)
587493
{
588494
return op_LessThan(b, a);
589495
}
590496

591-
public static bool op_LessThanOrEqual(T a, object b)
497+
public static bool op_LessThanOrEqual(T a, Enum b)
592498
{
593-
if (!IsEnum(b, out var bType, out var underlyingType, throwOnNull: true))
594-
{
595-
// False although it means nothing: an exception will be raised
596-
return false;
597-
}
598-
if (underlyingType == typeof(UInt64))
499+
if (b.GetType().GetEnumUnderlyingType() == typeof(UInt64))
599500
{
600501
return op_LessThanOrEqual(a, Convert.ToUInt64(b));
601502
}
602503
return op_LessThanOrEqual(a, Convert.ToInt64(b));
603504
}
604505

605-
public static bool op_LessThanOrEqual(object a, T b)
506+
public static bool op_LessThanOrEqual(Enum a, T b)
606507
{
607508
return op_GreaterThanOrEqual(b, a);
608509
}
609510

610-
public static bool op_GreaterThanOrEqual(T a, object b)
511+
public static bool op_GreaterThanOrEqual(T a, Enum b)
611512
{
612-
if (!IsEnum(b, out var bType, out var underlyingType, throwOnNull: true))
613-
{
614-
// False although it means nothing: an exception will be raised
615-
return false;
616-
}
617-
if (underlyingType == typeof(UInt64))
513+
if (b.GetType().GetEnumUnderlyingType() == typeof(UInt64))
618514
{
619515
return op_GreaterThanOrEqual(a, Convert.ToUInt64(b));
620516
}
621517
return op_GreaterThanOrEqual(a, Convert.ToInt64(b));
622518
}
623519

624-
public static bool op_GreaterThanOrEqual(object a, T b)
520+
public static bool op_GreaterThanOrEqual(Enum a, T b)
625521
{
626522
return op_LessThanOrEqual(b, a);
627523
}

0 commit comments

Comments
 (0)