8000 Factor out ExtensionType's Gc setup mechanic · pythonnet/pythonnet@eb8cb8a · GitHub
[go: up one dir, main page]

Skip to content

Commit eb8cb8a

Browse files
committed
Factor out ExtensionType's Gc setup mechanic
To avoid code duplication.
1 parent 8075f48 commit eb8cb8a

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/runtime/extensiontype.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,24 @@ public ExtensionType()
2929

3030
IntPtr py = Runtime.PyType_GenericAlloc(tp, 0);
3131

32+
// Steals a ref to tpHandle.
33+
tpHandle = tp;
34+
pyHandle = py;
35+
36+
SetupGc();
37+
}
38+
39+
void SetupGc ()
40+
{
3241
GCHandle gc = AllocGCHandle(TrackTypes.Extension);
33-
Marshal.WriteIntPtr(py, ObjectOffset.magic(tp), (IntPtr)gc);
42+
Marshal.WriteIntPtr(pyHandle, ObjectOffset.magic(tpHandle), (IntPtr)gc);
3443

3544
// We have to support gc because the type machinery makes it very
3645
// hard not to - but we really don't have a need for it in most
3746
// concrete extension types, so untrack the object to save calls
3847
// from Python into the managed runtime that are pure overhead.
3948

40-
Runtime.PyObject_GC_UnTrack(py);
41-
42-
// Steals a ref to tpHandle.
43-
tpHandle = tp;
44-
pyHandle = py;
49+
Runtime.PyObject_GC_UnTrack(pyHandle);
4550
}
4651

4752

@@ -101,9 +106,7 @@ public static void tp_dealloc(IntPtr ob)
101106
protected override void OnLoad(InterDomainContext context)
102107
{
103108
base.OnLoad(context);
104-
GCHandle gc = AllocGCHandle(TrackTypes.Extension);
105-
Marshal.WriteIntPtr(pyHandle, ObjectOffset.magic(tpHandle), (IntPtr)gc);
106-
Runtime.PyObject_GC_UnTrack(pyHandle);
109+
SetupGc();
107110
}
108111
}
109112
}

0 commit comments

Comments
 (0)
0