| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,6 +32,21 @@ public static void Py_IsInitializedValue() | |||
| 32 | 32 | Assert.AreEqual(0, Runtime.Runtime.Py_IsInitialized()); | |
| 33 | 33 | } | |
| 34 | 34 | ||
| 35 | + [Test] | ||
| 36 | + public static void IterAcrossRuns() | ||
| 37 | + { | ||
| 38 | + Runtime.Runtime.Py_Initialize(); | ||
| 39 | + BorrowedReference builtins = Runtime.Runtime.PyEval_GetBuiltins(); | ||
| 40 | + BorrowedReference iter = Runtime.Runtime.PyDict_GetItemString(builtins, "iter"); | ||
| 41 | + | ||
| 42 | + using var ownedIter = new NewReference(iter); | ||
| 43 | + Runtime.Runtime.Py_Finalize(); | ||
| 44 | + | ||
| 45 | + Runtime.Runtime.Py_Initialize(); | ||
| 46 | + ownedIter.Dispose(); | ||
| 47 | + Runtime.Runtime.Py_Finalize(); | ||
| 48 | + } | ||
| 49 | + | ||
| 35 | 50 | [Test] | |
| 36 | 51 | public static void RefCountTest() | |
| 37 | 52 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,58 +28,68 @@ internal static class Util | |||
| 28 | 28 | [MethodImpl(MethodImplOptions.AggressiveInlining)] | |
| 29 | 29 | internal static int ReadInt32(BorrowedReference ob, int offset) | |
| 30 | 30 | { | |
| 31 | + Debug.Assert(offset >= 0); | ||
| 31 | 32 | return Marshal.ReadInt32(ob.DangerousGetAddress(), offset); | |
| 32 | 33 | } | |
| 33 | 34 | [MethodImpl(MethodImplOptions.AggressiveInlining)] | |
| 34 | 35 | internal static long ReadInt64(BorrowedReference ob, int offset) | |
| 35 | 36 | { | |
| 37 | + Debug.Assert(offset >= 0); | ||
| 36 | 38 | return Marshal.ReadInt64(ob.DangerousGetAddress(), offset); | |
| 37 | 39 | } | |
| 38 | 40 | ||
| 39 | 41 | [MethodImpl(MethodImplOptions.AggressiveInlining)] | |
| 40 | 42 | internal unsafe static T* ReadPtr<T>(BorrowedReference ob, int offset) | |
| 41 | 43 | where T: unmanaged | |
| 42 | 44 | { | |
| 45 | + Debug.Assert(offset >= 0); | ||
| 43 | 46 | IntPtr ptr = Marshal.ReadIntPtr(ob.DangerousGetAddress(), offset); | |
| 44 | 47 | return (T*)ptr; | |
| 45 | 48 | } | |
| 46 | 49 | ||
| 47 | 50 | [MethodImpl(MethodImplOptions.AggressiveInlining)] | |
| 48 | 51 | internal unsafe static IntPtr ReadIntPtr(BorrowedReference ob, int offset) | |
| 49 | 52 | { | |
| 53 | + Debug.Assert(offset >= 0); | ||
| 50 | 54 | return Marshal.ReadIntPtr(ob.DangerousGetAddress(), offset); | |
| 51 | 55 | } | |
| 52 | 56 | ||
| 53 | 57 | [MethodImpl(MethodImplOptions.AggressiveInlining)] | |
| 54 | 58 | internal unsafe static BorrowedReference ReadRef(BorrowedReference @ref, int offset) | |
| 55 | 59 | { | |
| 60 | + Debug.Assert(offset >= 0); | ||
| 56 | 61 | return new BorrowedReference(ReadIntPtr(@ref, offset)); | |
| 57 | 62 | } | |
| 58 | 63 | ||
| 59 | 64 | [MethodImpl(MethodImplOptions.AggressiveInlining)] | |
| 60 | 65 | internal static void WriteInt32(BorrowedReference ob, int offset, int value) | |
| 61 | 66 | { | |
| 67 | + Debug.Assert(offset >= 0); | ||
| 62 | 68 | Marshal.WriteInt32(ob.DangerousGetAddress(), offset, value); | |
| 63 | 69 | } | |
| 64 | 70 | [MethodImpl(MethodImplOptions.AggressiveInlining)] | |
| 65 | 71 | internal static void WriteInt64(BorrowedReference ob, int offset, long value) | |
| 66 | 72 | { | |
| 73 | + Debug.Assert(offset >= 0); | ||
| 67 | 74 | Marshal.WriteInt64(ob.DangerousGetAddress(), offset, value); | |
| 68 | 75 | } | |
| 69 | 76 | [MethodImpl(MethodImplOptions.AggressiveInlining)] | |
| 70 | 77 | internal unsafe static void WriteIntPtr(BorrowedReference ob, int offset, IntPtr value) | |
| 71 | 78 | { | |
| 79 | + Debug.Assert(offset >= 0); | ||
| 72 | 80 | Marshal.WriteIntPtr(ob.DangerousGetAddress(), offset, value); | |
| 73 | 81 | } | |
| 74 | 82 | [MethodImpl(MethodImplOptions.AggressiveInlining)] | |
| 75 | 83 | internal unsafe static void WriteRef(BorrowedReference ob, int offset, in StolenReference @ref) | |
| 76 | 84 | { | |
| 85 | + Debug.Assert(offset >= 0); | ||
| 77 | 86 | Marshal.WriteIntPtr(ob.DangerousGetAddress(), offset, @ref.DangerousGetAddress()); | |
| 78 | 87 | } | |
| 79 | 88 | ||
| 80 | 89 | [MethodImpl(MethodImplOptions.AggressiveInlining)] | |
| 81 | 90 | internal unsafe static void WriteNullableRef(BorrowedReference ob, int offset, in StolenReference @ref) | |
| 82 | 91 | { | |
| 92 | + Debug.Assert(offset >= 0); | ||
| 83 | 93 | Marshal.WriteIntPtr(ob.DangerousGetAddress(), offset, @ref.DangerousGetAddressOrNull()); | |
| 84 | 94 | } | |
| 85 | 95 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -143,7 +143,7 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType, | |||
| 143 | 143 | public new static NewReference mp_subscript(BorrowedReference ob, BorrowedReference idx) | |
| 144 | 144 | { | |
| 145 | 145 | var obj = (CLRObject)GetManagedObject(ob)!; | |
| 146 | - var arrObj = (ArrayObject)GetManagedObjectType(ob)!; | ||
| 146 | + var arrObj = (ArrayObject)GetManagedObject(Runtime.PyObject_TYPE(ob))!; | ||
| 147 | 147 | if (!arrObj.type.Valid) | |
| 148 | 148 | { | |
| 149 | 149 | return Exceptions.RaiseTypeError(arrObj.type.DeletedMessage); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -337,33 +337,37 @@ public static NewReference tp_repr(BorrowedReference ob) | |||
| 337 | 337 | /// </summary> | |
| 338 | 338 | public static void tp_dealloc(NewReference lastRef) | |
| 339 | 339 | { | |
| 340 | - GCHandle? gcHandle = TryGetGCHandle(lastRef.Borrow()); | ||
| 340 | + Runtime.PyGC_ValidateLists(); | ||
| 341 | + Runtime.PyObject_GC_UnTrack(lastRef.Borrow()); | ||
| 341 | 342 | ||
| 342 | - tp_clear(lastRef.Borrow()); | ||
| 343 | + CallClear(lastRef.Borrow()); | ||
| 343 | 344 | ||
| 344 | 345 | IntPtr addr = lastRef.DangerousGetAddress(); | |
| 345 | 346 | bool deleted = CLRObject.reflectedObjects.Remove(addr); | |
| 346 | 347 | Debug.Assert(deleted); | |
| 347 | 348 | ||
| 348 | - Runtime.PyObject_GC_UnTrack(lastRef.Borrow()); | ||
| 349 | - Runtime.PyObject_GC_Del(lastRef.Steal()); | ||
| 350 | - | ||
| 351 | - gcHandle?.Free(); | ||
| 349 | + DecrefTypeAndFree(lastRef.Steal()); | ||
| 350 | + Runtime.PyGC_ValidateLists(); | ||
| 352 | 351 | } | |
| 353 | 352 | ||
| 354 | 353 | public static int tp_clear(BorrowedReference ob) | |
| 355 | 354 | { | |
| 355 | + Runtime.PyGC_ValidateLists(); | ||
| 356 | + GCHandle? gcHandle = TryGetGCHandle(ob); | ||
| 357 | + gcHandle?.Free(); | ||
| 358 | + | ||
| 356 | 359 | int baseClearResult = BaseUnmanagedClear(ob); | |
| 357 | 360 | if (baseClearResult != 0) | |
| 358 | 361 | { | |
| 359 | 362 | return baseClearResult; | |
| 360 | 363 | } | |
| 361 | 364 | ||
| 362 | 365 | ClearObjectDict(ob); | |
| 366 | + Runtime.PyGC_ValidateLists(); | ||
| 363 | 367 | return 0; | |
| 364 | 368 | } | |
| 365 | 369 | ||
| 366 | - static unsafe int BaseUnmanagedClear(BorrowedReference ob) | ||
| 370 | + internal static unsafe int BaseUnmanagedClear(BorrowedReference ob) | ||
| 367 | 371 | { | |
| 368 | 372 | var type = Runtime.PyObject_TYPE(ob); | |
| 369 | 373 | var unmanagedBase = GetUnmanagedBaseType(type); | |
@@ -374,10 +378,10 @@ static unsafe int BaseUnmanagedClear(BorrowedReference ob) | |||
| 374 | 378 | } | |
| 375 | 379 | var clear = (delegate* unmanaged[Cdecl]<BorrowedReference, int>)clearPtr; | |
| 376 | 380 | ||
| 377 | - bool usesSubtypeClear = clearPtr == Util.ReadIntPtr(Runtime.CLRMetaType, TypeOffset.tp_clear); | ||
| 381 | + bool usesSubtypeClear = clearPtr == TypeManager.subtype_clear; | ||
| 378 | 382 | if (usesSubtypeClear) | |
| 379 | 383 | { | |
| 380 | - // workaround for https://bugs.python.org/issue45266 | ||
| 384 | + // workaround for https://bugs.python.org/issue45266 (subtype_clear) | ||
| 381 | 385 | using var dict = Runtime.PyObject_GenericGetDict(ob); | |
| 382 | 386 | if (Runtime.PyMapping_HasKey(dict.Borrow(), PyIdentifier.__clear_reentry_guard__) != 0) | |
| 383 | 387 | return 0; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,13 +1,11 @@ | |||
| 1 | 1 | using System; | |
| 2 | - using System.Collections.Concurrent; | ||
| 3 | 2 | using System.Collections.Generic; | |
| 4 | 3 | using System.ComponentModel; | |
| 5 | 4 | using System.Diagnostics; | |
| 6 | 5 | using System.Linq; | |
| 7 | 6 | using System.Reflection; | |
| 8 | 7 | using System.Reflection.Emit; | |
| 9 | 8 | using System.Runtime.InteropServices; | |
| 10 | - using System.Threading; | ||
| 11 | 9 | ||
| 12 | 10 | using Python.Runtime.Native; | |
| 13 | 11 | ||
@@ -71,6 +69,7 @@ internal ClassDerivedObject(Type tp) : base(tp) | |||
| 71 | 69 | ||
| 72 | 70 | public new static void tp_dealloc(NewReference ob) | |
| 73 | 71 | { | |
| 72 | + Runtime.PyGC_ValidateLists(); | ||
| 74 | 73 | var self = (CLRObject)GetManagedObject(ob.Borrow())!; | |
| 75 | 74 | ||
| 76 | 75 | // don't let the python GC destroy this object | |
@@ -84,6 +83,7 @@ internal ClassDerivedObject(Type tp) : base(tp) | |||
| 84 | 83 | GCHandle gc = GCHandle.Alloc(self, GCHandleType.Weak); | |
| 85 | 84 | SetGCHandle(ob.Borrow(), gc); | |
| 86 | 85 | oldHandle.Free(); | |
| 86 | + Runtime.PyGC_ValidateLists(); | ||
| 87 | 87 | } | |
| 88 | 88 | ||
| 89 | 89 | /// <summary> | |
@@ -800,6 +800,8 @@ public static void InvokeSetProperty<T>(IPythonDerivedType obj, string propertyN | |||
| 800 | 800 | ||
| 801 | 801 | public static void InvokeCtor(IPythonDerivedType obj, string origCtorName, object[] args) | |
| 802 | 802 | { | |
| 803 | + Debug.Assert(Runtime.PyGILState_Check() != 0); | ||
| 804 | + | ||
| 803 | 805 | // call the base constructor | |
| 804 | 806 | obj.GetType().InvokeMember(origCtorName, | |
| 805 | 807 | BindingFlags.InvokeMethod, | |
@@ -833,58 +835,12 @@ public static void InvokeCtor(IPythonDerivedType obj, string origCtorName, objec | |||
| 833 | 835 | } | |
| 834 | 836 | } | |
| 835 | 837 | ||
| 836 | - static readonly ConcurrentQueue<IntPtr> finalizeQueue = new(); | ||
| 837 | - static readonly Lazy<Thread> derivedFinalizer = new(() => | ||
| 838 | - { | ||
| 839 | - var thread = new Thread(DerivedFinalizerMain) | ||
| 840 | - { | ||
| 841 | - IsBackground = true, | ||
| 842 | - }; | ||
| 843 | - thread.Start(); | ||
| 844 | - return thread; | ||
| 845 | - }, LazyThreadSafetyMode.ExecutionAndPublication); | ||
| 846 | - | ||
| 847 | - static void DerivedFinalizerMain() | ||
| 848 | - { | ||
| 849 | - while (true) | ||
| 850 | - { | ||
| 851 | - if (0 == Runtime.Py_IsInitialized()) | ||
| 852 | - { | ||
| 853 | - Thread.Sleep(millisecondsTimeout: 1000); | ||
| 854 | - } | ||
| 855 | - | ||
| 856 | - PyGILState gs = Runtime.PyGILState_Ensure(); | ||
| 857 | - try | ||
| 858 | - { | ||
| 859 | - while (finalizeQueue.Count > 0) | ||
| 860 | - { | ||
| 861 | - finalizeQueue.TryDequeue(out IntPtr obj); | ||
| 862 | - var @ref = new BorrowedReference(obj); | ||
| 863 | - GCHandle gcHandle = ManagedType.GetGCHandle(@ref); | ||
| 864 | - | ||
| 865 | - bool deleted = CLRObject.reflectedObjects.Remove(obj); | ||
| 866 | - Debug.Assert(deleted); | ||
| 867 | - Runtime.PyObject_GC_Del(@ref); | ||
| 868 | - | ||
| 869 | - gcHandle.Free(); | ||
| 870 | - } | ||
| 871 | - | ||
| 872 | - } | ||
| 873 | - finally | ||
| 874 | - { | ||
| 875 | - Runtime.PyGILState_Release(gs); | ||
| 876 | - } | ||
| 877 | - } | ||
| 878 | - } | ||
| 879 | 838 | public static void PyFinalize(IPythonDerivedType obj) | |
| 880 | 839 | { | |
| 881 | 840 | // the C# object is being destroyed which must mean there are no more | |
| 882 | 841 | // references to the Python object as well | |
| 883 | 842 | var self = GetPyObj(obj).DangerousGetAddress(); | |
| 884 | - finalizeQueue.Enqueue(self); | ||
| 885 | - SetPyObj(obj, null); | ||
| 886 | - | ||
| 887 | - GC.KeepAlive(derivedFinalizer.Value); | ||
| 843 | + Finalizer.Instance.AddDerivedFinalizedObject(ref self); | ||
| 888 | 844 | } | |
| 889 | 845 | ||
| 890 | 846 | internal static BorrowedReference GetPyObj(IPythonDerivedType obj) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -63,23 +63,6 @@ internal static void RemoveClasses() | |||
| 63 | 63 | cache.Clear(); | |
| 64 | 64 | } | |
| 65 | 65 | ||
| 66 | - private static int TraverseTypeClear(BorrowedReference ob, IntPtr arg) | ||
| 67 | - { | ||
| 68 | - var visited = (HashSet<IntPtr>)GCHandle.FromIntPtr(arg).Target; | ||
| 69 | - if (!visited.Add(ob.DangerousGetAddressOrNull())) | ||
| 70 | - { | ||
| 71 | - return 0; | ||
| 72 | - } | ||
| 73 | - var clrObj = ManagedType.GetManagedObject(ob); | ||
| 74 | - if (clrObj != null) | ||
| 75 | - { | ||
| 76 | - BorrowedReference tp = Runtime.PyObject_TYPE(ob); | ||
| 77 | - ManagedType.CallTypeTraverse(ob, tp, TraverseTypeClear, arg); | ||
| 78 | - ManagedType.CallTypeClear(ob, tp); | ||
| 79 | - } | ||
| 80 | - return 0; | ||
| 81 | - } | ||
| 82 | - | ||
| 83 | 66 | internal static ClassManagerState SaveRuntimeData() | |
| 84 | 67 | { | |
| 85 | 68 | var contexts = new Dictionary<ReflectedClrType, InterDomainContext>(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -147,24 +147,15 @@ public static NewReference tp_repr(BorrowedReference ob) | |||
| 147 | 147 | return new NewReference(self.repr); | |
| 148 | 148 | } | |
| 149 | 149 | ||
| 150 | - protected override void Clear(BorrowedReference ob) | ||
| 151 | - { | ||
| 152 | - Runtime.Py_CLEAR(ref this.repr); | ||
| 153 | - base.Clear(ob); | ||
| 154 | - } | ||
| 155 | - | ||
| 156 | 150 | public static int tp_traverse(BorrowedReference ob, IntPtr visit, IntPtr arg) | |
| 157 | 151 | { | |
| 158 | - var self = (ConstructorBinding)GetManagedObject(ob)!; | ||
| 159 | - int res = PyVisit(self.typeToCreate, visit, arg); | ||
| 160 | - if (res != 0) return res; | ||
| 152 | + var self = (ConstructorBinding?)GetManagedObject(ob); | ||
| 153 | + if (self is null) return 0; | ||
| 161 | 154 | ||
| 162 | - if (self.repr is not null) | ||
| 163 | - { | ||
| 164 | - res = PyVisit(self.repr, visit, arg); | ||
| 165 | - if (res != 0) return res; | ||
| 166 | - } | ||
| 167 | - return 0; | ||
| 155 | + Runtime.PyGC_ValidateLists(); | ||
| 156 | + int res = PyVisit(self.typeToCreate, visit, arg); | ||
| 157 | + Runtime.PyGC_ValidateLists(); | ||
| 158 | + return res; | ||
| 168 | 159 | } | |
| 169 | 160 | } | |
| 170 | 161 | ||
@@ -241,24 +232,15 @@ public static NewReference tp_repr(BorrowedReference ob) | |||
| 241 | 232 | return new NewReference(self.repr); | |
| 242 | 233 | } | |
| 243 | 234 | ||
| 244 | - protected override void Clear(BorrowedReference ob) | ||
| 245 | - { | ||
| 246 | - Runtime.Py_CLEAR(ref this.repr); | ||
| 247 | - base.Clear(ob); | ||
| 248 | - } | ||
| 249 | - | ||
| 250 | 235 | public static int tp_traverse(BorrowedReference ob, IntPtr visit, IntPtr arg) | |
| 251 | 236 | { | |
| 252 | - var self = (BoundContructor)GetManagedObject(ob)!; | ||
| 253 | - int res = PyVisit(self.typeToCreate, visit, arg); | ||
| 254 | - if (res != 0) return res; | ||
| 237 | + var self = (BoundContructor?)GetManagedObject(ob); | ||
| 238 | + if (self is null) return 0; | ||
| 255 | 239 | ||
| 256 | - if (self.repr is not null) | ||
| 257 | - { | ||
| 258 | - res = PyVisit(self.repr, visit, arg); | ||
| 259 | - if (res != 0) return res; | ||
| 260 | - } | ||
| 261 | - return 0; | ||
| 240 | + Runtime.PyGC_ValidateLists(); | ||
| 241 | + int res = PyVisit(self.typeToCreate, visit, arg); | ||
| 242 | + Runtime.PyGC_ValidateLists(); | ||
| 243 | + return res; | ||
| 262 | 244 | } | |
| 263 | 245 | } | |
| 264 | 246 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments