Skip to content

Commit 2b4f326

Browse files
authored
Get rid of FallbackInequalityResolver (#1080)
1 parent b17d7e7 commit 2b4f326

2 files changed

Lines changed: 10 additions & 55 deletions

File tree

Src/IronPython/Runtime/Operations/InstanceOps.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,6 @@ public static string Format(IFormattable formattable, string format) {
316316

317317
#region Comparison and Hashing
318318

319-
public static bool EqualsMethod(object x, object y) {
320-
return x.Equals(y);
321-
}
322-
323-
public static bool NotEqualsMethod(object x, object y) {
324-
return !x.Equals(y);
325-
}
326-
327319
// Structural Equality and Hashing Helpers
328320

329321
public static int StructuralHashMethod(CodeContext/*!*/ context, IStructuralEquatable x) {

Src/IronPython/Runtime/Types/PythonTypeInfo.cs

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -689,9 +689,6 @@ private class ProtectedMemberResolver : MemberResolver {
689689
new OneOffResolver("__getitem__", GetItemResolver),
690690
new OneOffResolver("__setitem__", SetItemResolver),
691691

692-
// Runs after operator resolver to map __ne__ -> !__eq__
693-
new OneOffResolver("__ne__", FallbackInequalityResolver),
694-
695692
// Runs after the operator resolver to map IComparable
696693
new ComparisonResolver(typeof(IComparable), "Comparable"),
697694

@@ -805,7 +802,7 @@ private class ProtectedMemberResolver : MemberResolver {
805802
&& type != typeof(Complex)) {
806803
MethodInfo tostr = type.GetMethod("ToString", ReflectionUtils.EmptyTypes);
807804
if (tostr != null && tostr.DeclaringType != typeof(object)) {
808-
return GetInstanceOpsMethod(type, "ToStringMethod");
805+
return GetInstanceOpsMethod(type, nameof(InstanceOps.ToStringMethod));
809806
}
810807
}
811808

@@ -833,7 +830,7 @@ private class ProtectedMemberResolver : MemberResolver {
833830
}
834831

835832
// no override, pick up the default fancy .NET __repr__
836-
return binder.GetBaseInstanceMethod(type, "FancyRepr");
833+
return binder.GetBaseInstanceMethod(type, nameof(InstanceOps.FancyRepr));
837834
}
838835

839836
return MemberGroup.EmptyGroup;
@@ -846,7 +843,7 @@ private class ProtectedMemberResolver : MemberResolver {
846843
string methodName = "__reduce_ex__";
847844

848845
if (!TypeOverridesMethod(binder, type, methodName)) {
849-
return GetInstanceOpsMethod(type, "SerializeReduce");
846+
return GetInstanceOpsMethod(type, nameof(InstanceOps.SerializeReduce));
850847
}
851848
}
852849

@@ -893,7 +890,7 @@ private static bool TypeOverridesMethod(MemberBinder/*!*/ binder, Type/*!*/ typ
893890
}
894891
}
895892

896-
return GetInstanceOpsMethod(type, "StructuralHashMethod");
893+
return GetInstanceOpsMethod(type, nameof(InstanceOps.StructuralHashMethod));
897894
}
898895

899896
// otherwise we'll pick up __hash__ from ObjectOps which will call .NET's .GetHashCode therefore
@@ -965,7 +962,7 @@ internal static MemberGroup GetExtensionMemberGroup(Type type, MemberInfo[] news
965962
/// </summary>
966963
private static MemberGroup/*!*/ NextResolver(MemberBinder/*!*/ binder, Type/*!*/ type) {
967964
if (typeof(IEnumerator).IsAssignableFrom(type)) {
968-
return GetInstanceOpsMethod(type, "NextMethod");
965+
return GetInstanceOpsMethod(type, nameof(InstanceOps.NextMethod));
969966
}
970967

971968
return MemberGroup.EmptyGroup;
@@ -977,7 +974,7 @@ internal static MemberGroup GetExtensionMemberGroup(Type type, MemberInfo[] news
977974
private static MemberGroup/*!*/ LengthResolver(MemberBinder/*!*/ binder, Type/*!*/ type) {
978975
if (!type.IsDefined(typeof(DontMapICollectionToLenAttribute), true)) {
979976
if (binder.GetInterfaces(type).Contains(typeof(ICollection))) {
980-
return GetInstanceOpsMethod(type, "LengthMethod");
977+
return GetInstanceOpsMethod(type, nameof(InstanceOps.LengthMethod));
981978
}
982979

983980
foreach (Type t in binder.GetInterfaces(type)) {
@@ -1031,40 +1028,6 @@ internal static MemberGroup GetExtensionMemberGroup(Type type, MemberInfo[] news
10311028
return MemberGroup.EmptyGroup;
10321029
}
10331030

1034-
/// <summary>
1035-
/// Looks for an Equals overload defined on the type and if one is present binds __ne__ to an
1036-
/// InstanceOps helper.
1037-
/// </summary>
1038-
private static MemberGroup/*!*/ FallbackInequalityResolver(MemberBinder/*!*/ binder, Type/*!*/ type) {
1039-
// if object defines __eq__ then we can call the reverse version
1040-
if (IncludeOperatorMethod(type, PythonOperationKind.NotEqual)) {
1041-
foreach (Type curType in binder.GetContributingTypes(type)) {
1042-
MemberGroup mg = binder.GetMember(curType, "Equals");
1043-
1044-
foreach (MemberTracker mt in mg) {
1045-
if (mt.MemberType != TrackerTypes.Method || mt.DeclaringType == typeof(object)) {
1046-
continue;
1047-
}
1048-
1049-
MethodTracker method = (MethodTracker)mt;
1050-
if ((method.Method.Attributes & MethodAttributes.NewSlot) != 0 ||
1051-
PythonHiddenAttribute.IsHidden(method.Method)) {
1052-
continue;
1053-
}
1054-
1055-
ParameterInfo[] pis = method.Method.GetParameters();
1056-
if (pis.Length == 1) {
1057-
if (pis[0].ParameterType == typeof(object)) {
1058-
return new MemberGroup(MethodTracker.FromMemberInfo(typeof(InstanceOps).GetMethod(nameof(InstanceOps.NotEqualsMethod)), curType));
1059-
}
1060-
}
1061-
}
1062-
}
1063-
}
1064-
1065-
return MemberGroup.EmptyGroup;
1066-
}
1067-
10681031
private static MemberGroup/*!*/ AllResolver(MemberBinder/*!*/ binder, Type/*!*/ type) {
10691032
// static types are like modules and define __all__.
10701033
if (type.IsAbstract && type.IsSealed) {
@@ -1083,7 +1046,7 @@ internal static MemberGroup GetExtensionMemberGroup(Type type, MemberInfo[] news
10831046
if (res == MemberGroup.EmptyGroup &&
10841047
!typeof(IPythonObject).IsAssignableFrom(type) &&
10851048
typeof(IDynamicMetaObjectProvider).IsAssignableFrom(type)) {
1086-
res = GetInstanceOpsMethod(type, "DynamicDir");
1049+
res = GetInstanceOpsMethod(type, nameof(InstanceOps.DynamicDir));
10871050
}
10881051

10891052
return res;
@@ -1126,23 +1089,23 @@ internal override bool TrySetValue(CodeContext context, object instance, PythonT
11261089

11271090
private static MemberGroup/*!*/ EnterResolver(MemberBinder/*!*/ binder, Type/*!*/ type) {
11281091
if (!type.IsDefined(typeof(DontMapIDisposableToContextManagerAttribute), true) && typeof(IDisposable).IsAssignableFrom(type)) {
1129-
return GetInstanceOpsMethod(type, "EnterMethod");
1092+
return GetInstanceOpsMethod(type, nameof(InstanceOps.EnterMethod));
11301093
}
11311094

11321095
return MemberGroup.EmptyGroup;
11331096
}
11341097

11351098
private static MemberGroup/*!*/ ExitResolver(MemberBinder/*!*/ binder, Type/*!*/ type) {
11361099
if (!type.IsDefined(typeof(DontMapIDisposableToContextManagerAttribute), true) && typeof(IDisposable).IsAssignableFrom(type)) {
1137-
return GetInstanceOpsMethod(type, "ExitMethod");
1100+
return GetInstanceOpsMethod(type, nameof(InstanceOps.ExitMethod));
11381101
}
11391102

11401103
return MemberGroup.EmptyGroup;
11411104
}
11421105

11431106
private static MemberGroup/*!*/ FormatResolver(MemberBinder/*!*/ binder, Type/*!*/ type) {
11441107
if (typeof(IFormattable).IsAssignableFrom(type)) {
1145-
return GetInstanceOpsMethod(type, "Format");
1108+
return GetInstanceOpsMethod(type, nameof(InstanceOps.Format));
11461109
}
11471110

11481111
return MemberGroup.EmptyGroup;

0 commit comments

Comments
 (0)