Skip to content

Commit 5bca333

Browse files
committed
refactored tp_clear in ExtensionType and descendants into a virtual C# function Clear
1 parent 25e3864 commit 5bca333

8 files changed

+54
-37
lines changed

src/runtime/constructorbinding.cs

+6-8
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,10 @@ protected override void Dealloc()
155155
base.Dealloc();
156156
}
157157

158-
public static int tp_clear(IntPtr ob)
158+
protected override void Clear()
159159
{
160-
var self = (ConstructorBinding)GetManagedObject(ob);
161-
Runtime.Py_CLEAR(ref self.repr);
162-
return 0;
160+
Runtime.Py_CLEAR(ref this.repr);
161+
base.Clear();
163162
}
164163

165164
public static int tp_traverse(IntPtr ob, IntPtr visit, IntPtr arg)
@@ -254,11 +253,10 @@ protected override void Dealloc()
254253
base.Dealloc();
255254
}
256255

257-
public static int tp_clear(IntPtr ob)
256+
protected override void Clear()
258257
{
259-
var self = (BoundContructor)GetManagedObject(ob);
260-
Runtime.Py_CLEAR(ref self.repr);
261-
return 0;
258+
Runtime.Py_CLEAR(ref this.repr);
259+
base.Clear();
262260
}
263261

264262
public static int tp_traverse(IntPtr ob, IntPtr visit, IntPtr arg)

src/runtime/eventbinding.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,10 @@ protected override void Dealloc()
109109
base.Dealloc();
110110
}
111111

112-
public static int tp_clear(IntPtr ob)
112+
protected override void Clear()
113113
{
114-
var self = (EventBinding)GetManagedObject(ob);
115-
Runtime.Py_CLEAR(ref self.target);
116-
return 0;
114+
Runtime.Py_CLEAR(ref this.target);
115+
base.Clear();
117116
}
118117
}
119118
}

src/runtime/eventobject.cs

+10
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ protected override void Dealloc()
207207
}
208208
base.Dealloc();
209209
}
210+
211+
protected override void Clear()
212+
{
213+
if (this.unbound is not null)
214+
{
215+
Runtime.XDecref(this.unbound.pyHandle);
216+
this.unbound = null;
217+
}
218+
base.Clear();
219+
}
210220
}
211221

212222

src/runtime/extensiontype.cs

+10-3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ protected virtual void Dealloc()
6262
this.FreeGCHandle();
6363
}
6464

65+
/// <summary>DecRefs and nulls any fields pointing back to Python</summary>
66+
protected virtual void Clear() { }
67+
6568
/// <summary>
6669
/// Type __setattr__ implementation.
6770
/// </summary>
@@ -88,9 +91,6 @@ public static int tp_descr_set(IntPtr ds, IntPtr ob, IntPtr val)
8891
}
8992

9093

91-
/// <summary>
92-
/// Default dealloc implementation.
93-
/// </summary>
9494
public static void tp_dealloc(IntPtr ob)
9595
{
9696
// Clean up a Python instance of this extension type. This
@@ -99,6 +99,13 @@ public static void tp_dealloc(IntPtr ob)
9999
self?.Dealloc();
100100
}
101101

102+
public static int tp_clear(IntPtr ob)
103+
{
104+
var self = (ExtensionType)GetManagedObject(ob);
105+
self?.Clear();
106+
return 0;
107+
}
108+
102109
protected override void OnLoad(InterDomainContext context)
103110
{
104111
base.OnLoad(context);

src/runtime/methodbinding.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,10 @@ protected override void Dealloc()
241241
base.Dealloc();
242242
}
243243

244-
public static int tp_clear(IntPtr ob)
244+
protected override void Clear()
245245
{
246-
var self = (MethodBinding)GetManagedObject(ob);
247-
self.ClearMembers();
248-
return 0;
246+
this.ClearMembers();
247+
base.Clear();
249248
}
250249

251250
protected override void OnSave(InterDomainContext context)

src/runtime/methodobject.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,11 @@ protected override void Dealloc()
217217
base.Dealloc();
218218
}
219219

220-
public static int tp_clear(IntPtr ob)
220+
protected override void Clear()
221221
{
222-
var self = (MethodObject)GetManagedObject(ob);
223-
self.ClearMembers();
224-
ClearObjectDict(ob);
225-
return 0;
222+
this.ClearMembers();
223+
ClearObjectDict(this.pyHandle);
224+
base.Clear();
226225
}
227226

228227
protected override void OnSave(InterDomainContext context)

src/runtime/moduleobject.cs

+12-13
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,6 @@ public static IntPtr tp_repr(IntPtr ob)
295295
return Runtime.PyString_FromString($"<module '{self.moduleName}'>");
296296
}
297297

298-
protected override void Dealloc()
299-
{
300-
tp_clear(this.pyHandle);
301-
base.Dealloc();
302-
}
303-
304298
public static int tp_traverse(IntPtr ob, IntPtr visit, IntPtr arg)
305299
{
306300
var self = (ModuleObject)GetManagedObject(ob);
@@ -314,17 +308,22 @@ public static int tp_traverse(IntPtr ob, IntPtr visit, IntPtr arg)
314308
return 0;
315309
}
316310

317-
public static int tp_clear(IntPtr ob)
311+
protected override void Dealloc()
318312
{
319-
var self = (ModuleObject)GetManagedObject(ob);
320-
Runtime.Py_CLEAR(ref self.dict);
321-
ClearObjectDict(ob);
322-
foreach (var attr in self.cache.Values)
313+
tp_clear(this.pyHandle);
314+
base.Dealloc();
315+
}
316+
317+
protected override void Clear()
318+
{
319+
Runtime.Py_CLEAR(ref this.dict);
320+
ClearObjectDict(this.pyHandle);
321+
foreach (var attr in this.cache.Values)
323322
{
324323
Runtime.XDecref(attr.pyHandle);
325324
}
326-
self.cache.Clear();
327-
return 0;
325+
this.cache.Clear();
326+
base.Clear();
328327
}
329328

330329
protected override void OnSave(InterDomainContext context)

src/runtime/overload.cs

+6
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,11 @@ protected override void Dealloc()
6363
Runtime.Py_CLEAR(ref this.target);
6464
base.Dealloc();
6565
}
66+
67+
protected override void Clear()
68+
{
69+
Runtime.Py_CLEAR(ref this.target);
70+
base.Clear();
71+
}
6672
}
6773
}

0 commit comments

Comments
 (0)