|
1 | 1 | using System; |
2 | 2 | using System.Collections; |
3 | | -using System.Diagnostics; |
4 | 3 | using System.Runtime.InteropServices; |
5 | | -using System.Runtime.Serialization; |
6 | 4 |
|
7 | 5 | namespace Python.Runtime |
8 | 6 | { |
@@ -288,44 +286,40 @@ public static IntPtr tp_repr(IntPtr ob) |
288 | 286 | public static void tp_dealloc(IntPtr ob) |
289 | 287 | { |
290 | 288 | ManagedType self = GetManagedObject(ob); |
| 289 | + if (Runtime.PyType_SUPPORTS_WEAKREFS(Runtime.PyObject_TYPE(ob))) |
| 290 | + { |
| 291 | + Runtime.PyObject_ClearWeakRefs(ob); |
| 292 | + } |
291 | 293 | tp_clear(ob); |
292 | | - Runtime.PyObject_GC_UnTrack(self.pyHandle); |
293 | | - Runtime.PyObject_GC_Del(self.pyHandle); |
| 294 | + Runtime.PyObject_GC_UnTrack(ob); |
| 295 | + Runtime.PyObject_GC_Del(ob); |
294 | 296 | self.FreeGCHandle(); |
295 | 297 | } |
296 | 298 |
|
297 | 299 | public static int tp_clear(IntPtr ob) |
298 | 300 | { |
299 | 301 | ManagedType self = GetManagedObject(ob); |
300 | | - if (!self.IsTypeObject()) |
301 | | - { |
302 | | - ClearObjectDict(ob); |
303 | | - } |
304 | | - self.tpHandle = IntPtr.Zero; |
| 302 | + ClearObjectDict(ob); |
| 303 | + Runtime.Py_CLEAR(ref self.tpHandle); |
305 | 304 | return 0; |
306 | 305 | } |
307 | 306 |
|
308 | 307 | protected override void OnSave(InterDomainContext context) |
309 | 308 | { |
310 | 309 | base.OnSave(context); |
311 | | - if (pyHandle != tpHandle) |
312 | | - { |
313 | | - IntPtr dict = GetObjectDict(pyHandle); |
314 | | - Runtime.XIncref(dict); |
315 | | - context.Storage.AddValue("dict", dict); |
316 | | - } |
| 310 | + IntPtr dict = GetObjectDict(pyHandle); |
| 311 | + Runtime.XIncref(dict); |
| 312 | + Runtime.XIncref(tpHandle); |
| 313 | + context.Storage.AddValue("dict", dict); |
317 | 314 | } |
318 | 315 |
|
319 | 316 | protected override void OnLoad(InterDomainContext context) |
320 | 317 | { |
321 | 318 | base.OnLoad(context); |
322 | | - if (pyHandle != tpHandle) |
323 | | - { |
324 | | - IntPtr dict = context.Storage.GetValue<IntPtr>("dict"); |
325 | | - SetObjectDict(pyHandle, dict); |
326 | | - } |
| 319 | + IntPtr dict = context.Storage.GetValue<IntPtr>("dict"); |
| 320 | + SetObjectDict(pyHandle, dict); |
327 | 321 | gcHandle = AllocGCHandle(); |
328 | | - Marshal.WriteIntPtr(pyHandle, TypeOffset.magic(), (IntPtr)gcHandle); |
| 322 | + Marshal.WriteIntPtr(pyHandle, ObjectOffset.magic(tpHandle), (IntPtr)gcHandle); |
329 | 323 | } |
330 | 324 | } |
331 | 325 | } |
0 commit comments