From bccee2c46f4b0372f3513b4aac45624a395e656b Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Wed, 18 Oct 2023 15:14:04 +0200 Subject: [PATCH] Explicitly compare MaybeType objects by name --- src/runtime/ClassManager.cs | 5 +- src/runtime/StateSerialization/MaybeType.cs | 91 +++++++++++---------- 2 files changed, 53 insertions(+), 43 deletions(-) diff --git a/src/runtime/ClassManager.cs b/src/runtime/ClassManager.cs index d743bc006..a2692bbeb 100644 --- a/src/runtime/ClassManager.cs +++ b/src/runtime/ClassManager.cs @@ -3,7 +3,6 @@ using System.Diagnostics; using System.Linq; using System.Reflection; -using System.Runtime.InteropServices; using System.Security; using Python.Runtime.StateSerialization; @@ -33,7 +32,9 @@ internal class ClassManager BindingFlags.Public | BindingFlags.NonPublic; - internal static Dictionary cache = new(capacity: 128); + internal static Dictionary cache = new( + capacity: 128, comparer: new MaybeTypeComparer() + ); private static readonly Type dtype; private ClassManager() diff --git a/src/runtime/StateSerialization/MaybeType.cs b/src/runtime/StateSerialization/MaybeType.cs index 884b7edb0..b8f20db95 100644 --- a/src/runtime/StateSerialization/MaybeType.cs +++ b/src/runtime/StateSerialization/MaybeType.cs @@ -3,62 +3,71 @@ using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.IO; +using System.Collections.Generic; -namespace Python.Runtime +namespace Python.Runtime; + +[Serializable] +internal struct MaybeType : ISerializable { - [Serializable] - internal struct MaybeType : ISerializable - { - public static implicit operator MaybeType (Type ob) => new(ob); + public static implicit operator MaybeType (Type ob) => new(ob); - // The AssemblyQualifiedName of the serialized Type - const string SerializationName = "n"; - readonly string name; - readonly Type type; + // The AssemblyQualifiedName of the serialized Type + const string SerializationName = "n"; + readonly string name; + readonly Type type; - public string DeletedMessage + public string DeletedMessage + { + get { - get - { - return $"The .NET Type {name} no longer exists"; - } + return $"The .NET Type {name} no longer exists"; } + } - public Type Value + public Type Value + { + get { - get + if (type == null) { - if (type == null) - { - throw new SerializationException(DeletedMessage); - } - return type; + throw new SerializationException(DeletedMessage); } + return type; } + } - public string Name => name; - public bool Valid => type != null; + public string Name => name; + public bool Valid => type != null; - public override string ToString() - { - return (type != null ? type.ToString() : $"missing type: {name}"); - } + public override string ToString() + { + return (type != null ? type.ToString() : $"missing type: {name}"); + } - public MaybeType(Type tp) - { - type = tp; - name = tp.AssemblyQualifiedName; - } + public MaybeType(Type tp) + { + type = tp; + name = tp.AssemblyQualifiedName; + } - private MaybeType(SerializationInfo serializationInfo, StreamingContext context) - { - name = (string)serializationInfo.GetValue(SerializationName, typeof(string)); - type = Type.GetType(name, throwOnError:false); - } + private MaybeType(SerializationInfo serializationInfo, StreamingContext context) + { + name = (string)serializationInfo.GetValue(SerializationName, typeof(string)); + type = Type.GetType(name, throwOnError:false); + } - public void GetObjectData(SerializationInfo serializationInfo, StreamingContext context) - { - serializationInfo.AddValue(SerializationName, name); - } + public void GetObjectData(SerializationInfo serializationInfo, StreamingContext context) + { + serializationInfo.AddValue(SerializationName, name); } } + +[Serializable] +internal class MaybeTypeComparer : IEqualityComparer +{ + public bool Equals (MaybeType lhs, MaybeType rhs) => + lhs.Name == rhs.Name; + + public int GetHashCode(MaybeType t) => (t.Name ?? "").GetHashCode(); +}