FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

partially switched classmanager.cs to the new style references · DEVBOX10/pythonnet@ee65632 · GitHub

Repository navigation

Commit ee65632

Browse files
committed
partially switched classmanager.cs to the new style references
1 parent f6b84da commit ee65632

6 files changed

Lines changed: 25 additions & 14 deletions

File tree

‎src/runtime/PythonReferenceComparer.cs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23

34
namespace Python.Runtime
@@ -6,15 +7,16 @@ namespace Python.Runtime
67
/// Compares Python object wrappers by Python object references.
78
/// <para>Similar to <see cref="object.ReferenceEquals"/> but for Python objects</para>
89
/// </summary>
10+
[Serializable]
911
public sealed class PythonReferenceComparer : IEqualityComparer<PyObject>
1012
{
1113
public static PythonReferenceComparer Instance { get; } = new PythonReferenceComparer();
1214
public bool Equals(PyObject? x, PyObject? y)
1315
{
14-
return x?.Handle == y?.Handle;
16+
return x?.rawPtr == y?.rawPtr;
1517
}
1618

17-
public int GetHashCode(PyObject obj) => obj.Handle.GetHashCode();
19+
public int GetHashCode(PyObject obj) => obj.rawPtr.GetHashCode();
1820

1921
private PythonReferenceComparer() { }
2022
}

‎src/runtime/classbase.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ internal class ClassBase : ManagedType
2424
internal readonly Dictionary<int, MethodObject> richcompare = new();
2525
internal MaybeType type;
2626

27+
internal new PyType pyHandle
28+
{
29+
get => (PyType)base.pyHandle;
30+
set => base.pyHandle = value;
31+
}
32+
2733
internal ClassBase(Type tp)
2834
{
2935
if (tp is null) throw new ArgumentNullException(nameof(type));

‎src/runtime/classderived.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ public static void InvokeMethodVoid(IPythonDerivedType obj, string methodName, s
727727
if (method.Reference != Runtime.PyNone)
728728
{
729729
// 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);
731731
if (null == managedMethod)
732732
{
733733
var pyargs = new PyObject[args.Length];
@@ -827,7 +827,7 @@ public static void InvokeCtor(IPythonDerivedType obj, string origCtorName, objec
827827
try
828828
{
829829
// create the python object
830-
BorrowedReference type = TypeManager.GetTypeReference(obj.GetType());
830+
var type = TypeManager.GetType(obj.GetType());
831831
self = new CLRObject(obj, type);
832832

833833
// set __pyobj__ to self and deref the python object which will allow this

‎src/runtime/classmanager.cs‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ internal static void DisposePythonWrappersForClrTypes()
7777
cache.Clear();
7878
}
7979

80-
private static int TraverseTypeClear(IntPtr ob, IntPtr arg)
80+
private static int TraverseTypeClear(BorrowedReference ob, IntPtr arg)
8181
{
8282
var visited = (HashSet<IntPtr>)GCHandle.FromIntPtr(arg).Target;
83-
if (!visited.Add(ob))
83+
if (!visited.Add(ob.DangerousGetAddressOrNull()))
8484
{
8585
return 0;
8686
}
@@ -96,7 +96,7 @@ private static int TraverseTypeClear(IntPtr ob, IntPtr arg)
9696
internal static void SaveRuntimeData(RuntimeDataStorage storage)
9797
{
9898
var contexts = storage.AddValue("contexts",
99-
new Dictionary<IntPtr, InterDomainContext>());
99+
new Dictionary<PyType, InterDomainContext>(PythonReferenceComparer.Instance));
100100
storage.AddValue("cache", cache);
101101
foreach (var cls in cache)
102102
{
@@ -143,7 +143,7 @@ internal static Dictionary<ManagedType, InterDomainContext> RestoreRuntimeData(R
143143
{
144144
cache = storage.GetValue<Dictionary<MaybeType, ClassBase>>("cache");
145145
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");
147147
var loadedObjs = new Dictionary<ManagedType, InterDomainContext>();
148148
foreach (var pair in cache)
149149
{
@@ -265,7 +265,7 @@ private static PyType InitPyType(Type type, ClassBase impl)
265265
var pyType = TypeManager.GetOrCreateClass(type);
266266

267267
// Set the handle attributes on the implementing instance.
268-
impl.tpHandle = impl.pyHandle = pyType.Handle;
268+
impl.pyHandle = impl.tpHandle = pyType;
269269

270270
return pyType;
271271
}
@@ -558,11 +558,11 @@ private static ClassInfo GetClassInfo(Type type)
558558
}
559559
// Note the given instance might be uninitialized
560560
ob = GetClass(tp);
561-
if (ob.pyHandle == IntPtr.Zero && ob is ClassObject)
561+
if (ob.pyHandle is null && ob is ClassObject)
562562
{
563-
ob.pyHandle = ob.tpHandle = TypeManager.GetOrCreateClass(tp).Handle;
563+
ob.pyHandle = ob.tpHandle = TypeManager.GetOrCreateClass(tp);
564564
}
565-
Debug.Assert(ob.pyHandle != IntPtr.Zero);
565+
Debug.Assert(ob.pyHandle is not null);
566566
// GetClass returns a Borrowed ref. ci.members owns the reference.
567567
ob.IncrRefCount();
568568
ci.members[mi.Name] = ob;

‎src/runtime/interop.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ internal static ThunkInfo GetThunk(Delegate @delegate)
288288
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
289289
public delegate int ObjObjFunc(IntPtr ob, IntPtr arg);
290290

291+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
292+
public delegate int BP_I32(BorrowedReference ob, IntPtr arg);
293+
291294
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
292295
public delegate void DestructorFunc(IntPtr ob);
293296

‎src/runtime/runtime_data.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ private static void SaveRuntimeDataObjects(RuntimeDataStorage storage)
167167
var extensionObjs = new List<ManagedType>();
168168
var wrappers = new Dictionary<object, List<CLRObject>>();
169169
var serializeObjs = new CLRWrapperCollection();
170-
var contexts = new Dictionary<IntPtr, InterDomainContext>();
170+
var contexts = new Dictionary<PyType, InterDomainContext>(PythonReferenceComparer.Instance);
171171
foreach (var entry in objs)
172172
{
173173
var obj = entry.Key;
@@ -243,7 +243,7 @@ private static Dictionary<ManagedType, InterDomainContext> RestoreRuntimeDataObj
243243
{
244244
var extensions = storage.GetValue<List<ManagedType>>("extensions");
245245
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");
247247
var storedObjs = new Dictionary<ManagedType, InterDomainContext>();
248248
foreach (var obj in Enumerable.Union(extensions, internalStores))
249249
{

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL