8000 refactored tp_dealloc in ExtensionType and descendants · pythonnet/pythonnet@25e3864 · GitHub
[go: up one dir, main page]

Skip to content

Commit 25e3864

Browse files
committed
refactored tp_dealloc in ExtensionType and descendants
1 parent 539ce81 commit 25e3864

File tree

9 files changed

+52
-90
lines changed

9 files changed

+52
-90
lines changed

src/runtime/constructorbinding.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,10 @@ public static IntPtr tp_repr(IntPtr ob)
149149
return self.repr;
150150
}
151151

152-
/// <summary>
153-
/// ConstructorBinding dealloc implementation.
154-
/// </summary>
155-
public new static void tp_dealloc(IntPtr ob)
152+
protected override void Dealloc()
156153
{
157-
var self = (ConstructorBinding)GetManagedObject(ob);
158-
Runtime.XDecref(self.repr);
159-
self.Dealloc();
154+
Runtime.Py_CLEAR(ref this.repr);
155+
base.Dealloc();
160156
}
161157

162158
public static int tp_clear(IntPtr ob)
@@ -252,14 +248,10 @@ public static IntPtr tp_repr(IntPtr ob)
252248
return self.repr;
253249
}
254250

255-
/// <summary>
256-
/// ConstructorBinding dealloc implementation.
257-
/// </summary>
258-
public new static void tp_dealloc(IntPtr ob)
251+
protected override void Dealloc()
259252
{
260-
var self = (BoundContructor)GetManagedObject(ob);
261-
Runtime.XDecref(self.repr);
262-
self.Dealloc();
253+
Runtime.Py_CLEAR(ref this.repr);
254+
base.Dealloc();
263255
}
264256

265257
public static int tp_clear(IntPtr ob)

src/runtime/eventbinding.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,10 @@ public static IntPtr tp_repr(IntPtr ob)
103103
return Runtime.PyString_FromString(s);
104104
}
105105

106-
107-
/// <summary>
108-
/// EventBinding dealloc implementation.
109-
/// </summary>
110-
public new static void tp_dealloc(IntPtr ob)
106+
protected override void Dealloc()
111107
{
112-
var self = (EventBinding)GetManagedObject(ob);
113-
Runtime.XDecref(self.target);
114-
self.Dealloc();
108+
Runtime.Py_CLEAR(ref this.target);
109+
base.Dealloc();
115110
}
116111

117112
public static int tp_clear(IntPtr ob)

src/runtime/eventobject.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,14 @@ public static IntPtr tp_repr(IntPtr ob)
198198
}
199199

200200

201-
/// <summary>
202-
/// Descriptor dealloc implementation.
203-
/// </summary>
204-
public new static void tp_dealloc(IntPtr ob)
201+
protected override void Dealloc()
205202
{
206-
var self = (EventObject)GetManagedObject(ob);
207-
if (self.unbound != null)
203+
if (this.unbound is not null)
208204
{
209-
Runtime.XDecref(self.unbound.pyHandle);
205+
Runtime.XDecref(this.unbound.pyHandle);
206+
this.unbound = null;
210207
}
211-
self.Dealloc();
208+
base.Dealloc();
212209
}
213210
}
214211

src/runtime/extensiontype.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,12 @@ void SetupGc ()
5454
}
5555

5656

57-
/// <summary>
58-
/// Common finalization code to support custom tp_deallocs.
59-
/// </summary>
60-
public static void FinalizeObject(ManagedType self)
57+
protected virtual void Dealloc()
6158
{
62-
ClearObjectDict(self.pyHandle);
63-
Runtime.PyObject_GC_Del(self.pyHandle);
59+
ClearObjectDict(this.pyHandle);
60+
Runtime.PyObject_GC_Del(this.pyHandle);
6461
// Not necessary for decref of `tpHandle`.
65-
self.FreeGCHandle();
66-
}
67-
68-
protected void Dealloc()
69-
{
70-
FinalizeObject(this);
62+
this.FreeGCHandle();
7163
}
7264

7365
/// <summary>
@@ -104,7 +96,7 @@ public static void tp_dealloc(IntPtr ob)
10496
// Clean up a Python instance of this extension type. This
10597
// frees the allocated Python object and decrefs the type.
10698
var self = (ExtensionType)GetManagedObject(ob);
107-
self.Dealloc();
99+
self?.Dealloc();
108100
}
109101

110102
protected override void OnLoad(InterDomainContext context)

src/runtime/methodbinding.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public MethodBinding(MethodObject m, IntPtr target, IntPtr targetType)
3131
{
3232
Runtime.XIncref(targetType);
3333
}
34-
34+
3535
this.targetType = targetType;
3636

3737
this.info = null;
@@ -42,12 +42,6 @@ public MethodBinding(MethodObject m, IntPtr target) : this(m, target, IntPtr.Zer
4242
{
4343
}
4444

45-
private void ClearMembers()
46-
{
47-
Runtime.Py_CLEAR(ref target);
48-
Runtime.Py_CLEAR(ref targetType);
49-
}
50-
5145
/// <summary>
5246
/// Implement binding of generic methods using the subscript syntax [].
5347
/// </summary>
@@ -235,14 +229,16 @@ public static IntPtr tp_repr(IntPtr ob)
235229
return Runtime.PyString_FromString($"<{type} method '{name}'>");
236230
}
237231

238-
/// <summary>
239-
/// MethodBinding dealloc implementation.
240-
/// </summary>
241-
public new static void tp_dealloc(IntPtr ob)
232+
private void ClearMembers()
242233
{
243-
var self = (MethodBinding)GetManagedObject(ob);
244-
self.ClearMembers();
245-
self.Dealloc();
234+
Runtime.Py_CLEAR(ref target);
235+
Runtime.Py_CLEAR(ref targetType);
236+
}
237+
238+
protected override void Dealloc()
239+
{
240+
this.ClearMembers();
241+
base.Dealloc();
246242
}
247243

248244
public static int tp_clear(IntPtr ob)

src/runtime/methodobject.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,6 @@ internal bool IsStatic()
120120
return is_static;
121121
}
122122

123-
private void ClearMembers()
124-
{
125-
Runtime.Py_CLEAR(ref doc);
126-
if (unbound != null)
127-
{
128-
Runtime.XDecref(unbound.pyHandle);
129-
unbound = null;
130-
}
131-
}
132-
133123
/// <summary>
134124
/// Descriptor __getattribute__ implementation.
135125
/// </summary>
@@ -210,15 +200,21 @@ public static IntPtr tp_repr(IntPtr ob)
210200
return Runtime.PyString_FromString($"<method '{self.name}'>");
211201
}
212202

213-
/// <summary>
214-
/// Descriptor dealloc implementation.
215-
/// </summary>
216-
public new static void tp_dealloc(IntPtr ob)
203+
private void ClearMembers()
217204
{
218-
var self = (MethodObject)GetManagedObject(ob);
219-
self.ClearMembers();
220-
ClearObjectDict(ob);
221-
self.Dealloc();
205+
Runtime.Py_CLEAR(ref doc);
206+
if (unbound != null)
207+
{
208+
Runtime.XDecref(unbound.pyHandle);
209+
unbound = null;
210+
}
211+
}
212+
213+
1CF5 protected override void Dealloc()
214+
{
215+
this.ClearMembers();
216+
ClearObjectDict(this.pyHandle);
217+
base.Dealloc();
222218
}
223219

224220
public static int tp_clear(IntPtr ob)

src/runtime/moduleobject.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public static IntPtr tp_getattro(IntPtr ob, IntPtr key)
274274
Exceptions.SetError(e);
275275
return IntPtr.Zero;
276276
}
277-
277+
278278

279279
if (attr == null)
280280
{
@@ -295,11 +295,10 @@ public static IntPtr tp_repr(IntPtr ob)
295295
return Runtime.PyString_FromString($"<module '{self.moduleName}'>");
296296
}
297297

298-
public new static void tp_dealloc(IntPtr ob)
298+
protected override void Dealloc()
299299
{
300-
var self = (ModuleObject)GetManagedObject(ob);
301-
tp_clear(ob);
302-
self.Dealloc();
300+
tp_clear(this.pyHandle);
301+
base.Dealloc();
303302
}
304303

305304
public static int tp_traverse(IntPtr ob, IntPtr visit, IntPtr arg)
@@ -345,7 +344,7 @@ protected override void OnSave(InterDomainContext context)
345344
if ((Runtime.PyDict_DelItemString(DictRef, pair.Key) == -1) &&
346345
(Exceptions.ExceptionMatches(Exceptions.KeyError)))
347346
{
348-
// Trying to remove a key that's not in the dictionary
347+
// Trying to remove a key that's not in the dictionary
349348
// raises an error. We don't care about it.
350349
Runtime.PyErr_Clear();
351350
}
@@ -496,7 +495,7 @@ public static Assembly AddReference(string name)
496495
/// clr.GetClrType(IComparable) gives you the Type for IComparable,
497496
/// that you can e.g. perform reflection on. Similar to typeof(IComparable) in C#
498497
/// or clr.GetClrType(IComparable) in IronPython.
499-
///
498+
///
500499
/// </summary>
501500
/// <param name="type"></param>
502501
/// <returns>The Type object</returns>

src/runtime/native/TypeOffset.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ static void ValidateRequiredOffsetsPresent(PropertyInfo[] offsetProperties)
153153
"__instancecheck__",
154154
"__subclasscheck__",
155155
"AddReference",
156-
"FinalizeObject",
157156
"FindAssembly",
158157
"get_SuppressDocs",
159158
"get_SuppressOverloads",

src/runtime/overload.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,10 @@ public static IntPtr tp_repr(IntPtr op)
5858
return doc;
5959
}
6060

61-
/// <summary>
62-
/// OverloadMapper dealloc implementation.
63-
/// </summary>
64-
public new static void tp_dealloc(IntPtr ob)
61+
protected override void Dealloc()
6562
{
66-
var self = (OverloadMapper)GetManagedObject(ob);
67-
Runtime.XDecref(self.target);
68-
self.Dealloc();
63+
Runtime.Py_CLEAR(ref this.target);
64+
base.Dealloc();
6965
}
7066
}
7167
}

0 commit comments

Comments
 (0)
2918
0