| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,4 @@ | |||
| 1 | + using System; | ||
| 1 | 2 | using System.Collections.Generic; | |
| 2 | 3 | ||
| 3 | 4 | namespace Python.Runtime | |
@@ -6,15 +7,16 @@ namespace Python.Runtime | |||
| 6 | 7 | /// Compares Python object wrappers by Python object references. | |
| 7 | 8 | /// <para>Similar to <see cref="object.ReferenceEquals"/> but for Python objects</para> | |
| 8 | 9 | /// </summary> | |
| 10 | + [Serializable] | ||
| 9 | 11 | public sealed class PythonReferenceComparer : IEqualityComparer<PyObject> | |
| 10 | 12 | { | |
| 11 | 13 | public static PythonReferenceComparer Instance { get; } = new PythonReferenceComparer(); | |
| 12 | 14 | public bool Equals(PyObject? x, PyObject? y) | |
| 13 | 15 | { | |
| 14 | - return x?.Handle == y?.Handle; | ||
| 16 | + return x?.rawPtr == y?.rawPtr; | ||
| 15 | 17 | } | |
| 16 | 18 | ||
| 17 | - public int GetHashCode(PyObject obj) => obj.Handle.GetHashCode(); | ||
| 19 | + public int GetHashCode(PyObject obj) => obj.rawPtr.GetHashCode(); | ||
| 18 | 20 | ||
| 19 | 21 | private PythonReferenceComparer() { } | |
| 20 | 22 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,6 +24,12 @@ internal class ClassBase : ManagedType | |||
| 24 | 24 | internal readonly Dictionary<int, MethodObject> richcompare = new(); | |
| 25 | 25 | internal MaybeType type; | |
| 26 | 26 | ||
| 27 | + internal new PyType pyHandle | ||
| 28 | + { | ||
| 29 | + get => (PyType)base.pyHandle; | ||
| 30 | + set => base.pyHandle = value; | ||
| 31 | + } | ||
| 32 | + | ||
| 27 | 33 | internal ClassBase(Type tp) | |
| 28 | 34 | { | |
| 29 | 35 | if (tp is null) throw new ArgumentNullException(nameof(type)); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -727,7 +727,7 @@ public static void InvokeMethodVoid(IPythonDerivedType obj, string methodName, s | |||
| 727 | 727 | if (method.Reference != Runtime.PyNone) | |
| 728 | 728 | { | |
| 729 | 729 | // if the method hasn't been overridden then it will be a managed object | |
| 730 | - ManagedType? managedMethod = ManagedType.GetManagedObject(method.Handle); | ||
| 730 | + ManagedType? managedMethod = ManagedType.GetManagedObject(method); | ||
| 731 | 731 | if (null == managedMethod) | |
| 732 | 732 | { | |
| 733 | 733 | var pyargs = new PyObject[args.Length]; | |
@@ -827,7 +827,7 @@ public static void InvokeCtor(IPythonDerivedType obj, string origCtorName, objec | |||
| 827 | 827 | try | |
| 828 | 828 | { | |
| 829 | 829 | // create the python object | |
| 830 | - BorrowedReference type = TypeManager.GetTypeReference(obj.GetType()); | ||
| 830 | + var type = TypeManager.GetType(obj.GetType()); | ||
| 831 | 831 | self = new CLRObject(obj, type); | |
| 832 | 832 | ||
| 833 | 833 | // set __pyobj__ to self and deref the python object which will allow this | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -77,10 +77,10 @@ internal static void DisposePythonWrappersForClrTypes() | |||
| 77 | 77 | cache.Clear(); | |
| 78 | 78 | } | |
| 79 | 79 | ||
| 80 | - private static int TraverseTypeClear(IntPtr ob, IntPtr arg) | ||
| 80 | + private static int TraverseTypeClear(BorrowedReference ob, IntPtr arg) | ||
| 81 | 81 | { | |
| 82 | 82 | var visited = (HashSet<IntPtr>)GCHandle.FromIntPtr(arg).Target; | |
| 83 | - if (!visited.Add(ob)) | ||
| 83 | + if (!visited.Add(ob.DangerousGetAddressOrNull())) | ||
| 84 | 84 | { | |
| 85 | 85 | return 0; | |
| 86 | 86 | } | |
@@ -96,7 +96,7 @@ private static int TraverseTypeClear(IntPtr ob, IntPtr arg) | |||
| 96 | 96 | internal static void SaveRuntimeData(RuntimeDataStorage storage) | |
| 97 | 97 | { | |
| 98 | 98 | var contexts = storage.AddValue("contexts", | |
| 99 | - new Dictionary<IntPtr, InterDomainContext>()); | ||
| 99 | + new Dictionary<PyType, InterDomainContext>(PythonReferenceComparer.Instance)); | ||
| 100 | 100 | storage.AddValue("cache", cache); | |
| 101 | 101 | foreach (var cls in cache) | |
| 102 | 102 | { | |
@@ -143,7 +143,7 @@ internal static Dictionary<ManagedType, InterDomainContext> RestoreRuntimeData(R | |||
| 143 | 143 | { | |
| 144 | 144 | cache = storage.GetValue<Dictionary<MaybeType, ClassBase>>("cache"); | |
| 145 | 145 | var invalidClasses = new List<KeyValuePair<MaybeType, ClassBase>>(); | |
| 146 | - var contexts = storage.GetValue <Dictionary<IntPtr, InterDomainContext>>("contexts"); | ||
| 146 | + var contexts = storage.GetValue <Dictionary<PyType, InterDomainContext>>("contexts"); | ||
| 147 | 147 | var loadedObjs = new Dictionary<ManagedType, InterDomainContext>(); | |
| 148 | 148 | foreach (var pair in cache) | |
| 149 | 149 | { | |
@@ -265,7 +265,7 @@ private static PyType InitPyType(Type type, ClassBase impl) | |||
| 265 | 265 | var pyType = TypeManager.GetOrCreateClass(type); | |
| 266 | 266 | ||
| 267 | 267 | // Set the handle attributes on the implementing instance. | |
| 268 | - impl.tpHandle = impl.pyHandle = pyType.Handle; | ||
| 268 | + impl.pyHandle = impl.tpHandle = pyType; | ||
| 269 | 269 | ||
| 270 | 270 | return pyType; | |
| 271 | 271 | } | |
@@ -558,11 +558,11 @@ private static ClassInfo GetClassInfo(Type type) | |||
| 558 | 558 | } | |
| 559 | 559 | // Note the given instance might be uninitialized | |
| 560 | 560 | ob = GetClass(tp); | |
| 561 | - if (ob.pyHandle == IntPtr.Zero && ob is ClassObject) | ||
| 561 | + if (ob.pyHandle is null && ob is ClassObject) | ||
| 562 | 562 | { | |
| 563 | - ob.pyHandle = ob.tpHandle = TypeManager.GetOrCreateClass(tp).Handle; | ||
| 563 | + ob.pyHandle = ob.tpHandle = TypeManager.GetOrCreateClass(tp); | ||
| 564 | 564 | } | |
| 565 | - Debug.Assert(ob.pyHandle != IntPtr.Zero); | ||
| 565 | + Debug.Assert(ob.pyHandle is not null); | ||
| 566 | 566 | // GetClass returns a Borrowed ref. ci.members owns the reference. | |
| 567 | 567 | ob.IncrRefCount(); | |
| 568 | 568 | ci.members[mi.Name] = ob; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -288,6 +288,9 @@ internal static ThunkInfo GetThunk(Delegate @delegate) | |||
| 288 | 288 | [UnmanagedFunctionPointer(CallingConvention.Cdecl)] | |
| 289 | 289 | public delegate int ObjObjFunc(IntPtr ob, IntPtr arg); | |
| 290 | 290 | ||
| 291 | + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] | ||
| 292 | + public delegate int BP_I32(BorrowedReference ob, IntPtr arg); | ||
| 293 | + | ||
| 291 | 294 | [UnmanagedFunctionPointer(CallingConvention.Cdecl)] | |
| 292 | 295 | public delegate void DestructorFunc(IntPtr ob); | |
| 293 | 296 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -167,7 +167,7 @@ private static void SaveRuntimeDataObjects(RuntimeDataStorage storage) | |||
| 167 | 167 | var extensionObjs = new List<ManagedType>(); | |
| 168 | 168 | var wrappers = new Dictionary<object, List<CLRObject>>(); | |
| 169 | 169 | var serializeObjs = new CLRWrapperCollection(); | |
| 170 | - var contexts = new Dictionary<IntPtr, InterDomainContext>(); | ||
| 170 | + var contexts = new Dictionary<PyType, InterDomainContext>(PythonReferenceComparer.Instance); | ||
| 171 | 171 | foreach (var entry in objs) | |
| 172 | 172 | { | |
| 173 | 173 | var obj = entry.Key; | |
@@ -243,7 +243,7 @@ private static Dictionary<ManagedType, InterDomainContext> RestoreRuntimeDataObj | |||
| 243 | 243 | { | |
| 244 | 244 | var extensions = storage.GetValue<List<ManagedType>>("extensions"); | |
| 245 | 245 | var internalStores = storage.GetValue<List<CLRObject>>("internalStores"); | |
| 246 | - var contexts = storage.GetValue <Dictionary<IntPtr, InterDomainContext>>("contexts"); | ||
| 246 | + var contexts = storage.GetValue <Dictionary<PyType, InterDomainContext>>("contexts"); | ||
| 247 | 247 | var storedObjs = new Dictionary<ManagedType, InterDomainContext>(); | |
| 248 | 248 | foreach (var obj in Enumerable.Union(extensions, internalStores)) | |
| 249 | 249 | { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments