| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -236,10 +236,10 @@ private void Dispose(bool disposing) | |||
| 236 | 236 | ||
| 237 | 237 | ~PyBuffer() | |
| 238 | 238 | { | |
| 239 | - if (disposedValue) | ||
| 240 | - { | ||
| 241 | - return; | ||
| 242 | - } | ||
| 239 | + Debug.Assert(!disposedValue); | ||
| 240 | + | ||
| 241 | + _exporter.CheckRun(); | ||
| 242 | + | ||
| 243 | 243 | Finalizer.Instance.AddFinalizedObject(ref _view.obj); | |
| 244 | 244 | } | |
| 245 | 245 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,6 +27,7 @@ public partial class PyObject : DynamicObject, IDisposable | |||
| 27 | 27 | #endif | |
| 28 | 28 | ||
| 29 | 29 | protected internal IntPtr obj = IntPtr.Zero; | |
| 30 | + readonly int run = Runtime.GetRun(); | ||
| 30 | 31 | ||
| 31 | 32 | public static PyObject None => new PyObject(new BorrowedReference(Runtime.PyNone)); | |
| 32 | 33 | internal BorrowedReference Reference => new BorrowedReference(this.obj); | |
@@ -95,11 +96,15 @@ internal PyObject(in StolenReference reference) | |||
| 95 | 96 | // when the managed wrapper is garbage-collected. | |
| 96 | 97 | ~PyObject() | |
| 97 | 98 | { | |
| 98 | - if (obj == IntPtr.Zero) | ||
| 99 | - { | ||
| 100 | - return; | ||
| 101 | - } | ||
| 99 | + Debug.Assert(obj != IntPtr.Zero); | ||
| 100 | + | ||
| 101 | + #if TRACE_ALLOC | ||
| 102 | + CheckRun(); | ||
| 103 | + #endif | ||
| 104 | + | ||
| 102 | 105 | Finalizer.Instance.AddFinalizedObject(ref obj); | |
| 106 | + | ||
| 107 | + Dispose(false); | ||
| 103 | 108 | } | |
| 104 | 109 | ||
| 105 | 110 | ||
@@ -167,17 +172,6 @@ public object AsManagedObject(Type t) | |||
| 167 | 172 | ||
| 168 | 173 | internal bool IsDisposed => obj == IntPtr.Zero; | |
| 169 | 174 | ||
| 170 | - /// <summary> | ||
| 171 | - /// Dispose Method | ||
| 172 | - /// </summary> | ||
| 173 | - /// <remarks> | ||
| 174 | - /// The Dispose method provides a way to explicitly release the | ||
| 175 | - /// Python object represented by a PyObject instance. It is a good | ||
| 176 | - /// idea to call Dispose on PyObjects that wrap resources that are | ||
| 177 | - /// limited or need strict lifetime control. Otherwise, references | ||
| 178 | - /// to Python objects will not be released until a managed garbage | ||
| 179 | - /// collection occurs. | ||
| 180 | - /// </remarks> | ||
| 181 | 175 | protected virtual void Dispose(bool disposing) | |
| 182 | 176 | { | |
| 183 | 177 | if (this.obj == IntPtr.Zero) | |
@@ -188,6 +182,8 @@ protected virtual void Dispose(bool disposing) | |||
| 188 | 182 | if (Runtime.Py_IsInitialized() == 0) | |
| 189 | 183 | throw new InvalidOperationException("Python runtime must be initialized"); | |
| 190 | 184 | ||
| 185 | + CheckRun(); | ||
| 186 | + | ||
| 191 | 187 | if (!Runtime.IsFinalizing) | |
| 192 | 188 | { | |
| 193 | 189 | long refcount = Runtime.Refcount(this.obj); | |
@@ -221,10 +217,26 @@ protected virtual void Dispose(bool disposing) | |||
| 221 | 217 | this.obj = IntPtr.Zero; | |
| 222 | 218 | } | |
| 223 | 219 | ||
| 220 | + /// <summary> | ||
| 221 | + /// The Dispose method provides a way to explicitly release the | ||
| 222 | + /// Python object represented by a PyObject instance. It is a good | ||
| 223 | + /// idea to call Dispose on PyObjects that wrap resources that are | ||
| 224 | + /// limited or need strict lifetime control. Otherwise, references | ||
| 225 | + /// to Python objects will not be released until a managed garbage | ||
| 226 | + /// collection occurs. | ||
| 227 | + /// </summary> | ||
| 224 | 228 | public void Dispose() | |
| 225 | 229 | { | |
| 226 | - Dispose(true); | ||
| 227 | 230 | GC.SuppressFinalize(this); | |
| 231 | + Dispose(true); | ||
| 232 | + } | ||
| 233 | + | ||
| 234 | + internal void CheckRun() | ||
| 235 | + { | ||
| 236 | + if (run != Runtime.GetRun()) | ||
| 237 | + throw new InvalidOperationException( | ||
| 238 | + "PythonEngine was shut down after this object was created." + | ||
| 239 | + " It is an error to attempt to dispose or to continue using it."); | ||
| 228 | 240 | } | |
| 229 | 241 | ||
| 230 | 242 | internal BorrowedReference GetPythonTypeReference() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -85,7 +85,15 @@ internal static Version PyVersion | |||
| 85 | 85 | } | |
| 86 | 86 | } | |
| 87 | 87 | ||
| 88 | - /// <summary> | ||
| 88 | + static int run = 0; | ||
| 89 | + | ||
| 90 | + internal static int GetRun() | ||
| 91 | + { | ||
| 92 | + int runNumber = run; | ||
| 93 | + Debug.Assert(runNumber > 0, "This must only be called after Runtime is initialized at least once"); | ||
| 94 | + return runNumber; | ||
| 95 | + } | ||
| 96 | + | ||
| 89 | 97 | /// Initialize the runtime... | |
| 90 | 98 | /// </summary> | |
| 91 | 99 | /// <remarks>Always call this method from the Main thread. After the | |
@@ -110,6 +118,9 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd | |||
| 110 | 118 | if (!interpreterAlreadyInitialized) | |
| 111 | 119 | { | |
| 112 | 120 | Py_InitializeEx(initSigs ? 1 : 0); | |
| 121 | + | ||
| 122 | + NewRun(); | ||
| 123 | + | ||
| 113 | 124 | if (PyEval_ThreadsInitialized() == 0) | |
| 114 | 125 | { | |
| 115 | 126 | PyEval_InitThreads(); | |
@@ -130,6 +141,16 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd | |||
| 130 | 141 | { | |
| 131 | 142 | PyGILState_Ensure(); | |
| 132 | 143 | } | |
| 144 | + | ||
| 145 | + BorrowedReference pyRun = PySys_GetObject("__pynet_run__"); | ||
| 146 | + if (pyRun != null) | ||
| 147 | + { | ||
| 148 | + run = checked((int)PyLong_AsSignedSize_t(pyRun)); | ||
| 149 | + } | ||
| 150 | + else | ||
| 151 | + { | ||
| 152 | + NewRun(); | ||
| 153 | + } | ||
| 133 | 154 | } | |
| 134 | 155 | MainManagedThreadId = Thread.CurrentThread.ManagedThreadId; | |
| 135 | 156 | ||
@@ -175,6 +196,13 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd | |||
| 175 | 196 | inspect = GetModuleLazy("inspect"); | |
| 176 | 197 | } | |
| 177 | 198 | ||
| 199 | + static void NewRun() | ||
| 200 | + { | ||
| 201 | + run++; | ||
| 202 | + using var pyRun = PyLong_FromLongLong(run); | ||
| 203 | + PySys_SetObject("__pynet_run__", pyRun); | ||
| 204 | + } | ||
| 205 | + | ||
| 178 | 206 | private static void InitPyMembers() | |
| 179 | 207 | { | |
| 180 | 208 | IntPtr op; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments