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

Factor out the clearing of clr_data · sdpython/pythonnet@6aa75c5 · GitHub

This repository was archived by the owner on Jul 22, 2023. It is now read-only.

Repository navigation

Commit 6aa75c5

Browse files
Félix Bourbonnais
committed
Factor out the clearing of clr_data
In Runtime_data.cs
1 parent d7d44e8 commit 6aa75c5

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

‎src/runtime/runtime_data.cs‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ public static Type FormatterType
3131

3232
public static ICLRObjectStorer WrappersStorer { get; set; }
3333

34+
/// <summary>
35+
/// Clears the old "clr_data" entry if a previous one is present.
36+
/// </summary>
37+
static void ClearCLRData ()
38+
{
39+
IntPtr capsule = PySys_GetObject("clr_data").DangerousGetAddressUnchecked();
40+
if (capsule != IntPtr.Zero)
41+
{
42+
IntPtr oldData = PyCapsule_GetPointer(capsule, null);
43+
PyMem_Free(oldData);
44+
PyCapsule_SetPointer(capsule, IntPtr.Zero);
45+
}
46+
}
47+
3448
internal static void Stash()
3549
{
3650
var metaStorage = new RuntimeDataStorage();
@@ -70,15 +84,10 @@ internal static void Stash()
7084
Marshal.WriteIntPtr(mem, (IntPtr)ms.Length);
7185
Marshal.Copy(data, 0, mem + IntPtr.Size, (int)ms.Length);
7286

73-
IntPtr capsule = PySys_GetObject("clr_data").DangerousGetAddressUnchecked();
74-
if (capsule != IntPtr.Zero)
75-
{
76-
IntPtr oldData = PyCapsule_GetPointer(capsule, null);
77-
PyMem_Free(oldData);
78-
PyCapsule_SetPointer(capsule, IntPtr.Zero);
79-
}
80-
capsule = PyCapsule_New(mem, null, IntPtr.Zero);
87+
ClearCLRData();
88+
IntPtr capsule = PyCapsule_New(mem, null, IntPtr.Zero);
8189
PySys_SetObject("clr_data", capsule);
90+
// Let the dictionary own the reference
8291
XDecref(capsule);
8392
}
8493

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL