Skip to content

Refactoring of tp_clear #1460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions src/runtime/constructorbinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,10 @@ public static IntPtr tp_repr(IntPtr ob)
return self.repr;
}

protected override void Dealloc()
protected override void Clear()
{
Runtime.Py_CLEAR(ref this.repr);
base.Dealloc();
}

public static int tp_clear(IntPtr ob)
{
var self = (ConstructorBinding)GetManagedObject(ob);
Runtime.Py_CLEAR(ref self.repr);
return 0;
base.Clear();
}

public static int tp_traverse(IntPtr ob, IntPtr visit, IntPtr arg)
Expand Down Expand Up @@ -248,17 +241,10 @@ public static IntPtr tp_repr(IntPtr ob)
return self.repr;
}

protected override void Dealloc()
protected override void Clear()
{
Runtime.Py_CLEAR(ref this.repr);
base.Dealloc();
}

public static int tp_clear(IntPtr ob)
{
var self = (BoundContructor)GetManagedObject(ob);
Runtime.Py_CLEAR(ref self.repr);
return 0;
base.Clear();
}

public static int tp_traverse(IntPtr ob, IntPtr visit, IntPtr arg)
Expand Down
11 changes: 2 additions & 9 deletions src/runtime/eventbinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,10 @@ public static IntPtr tp_repr(IntPtr ob)
return Runtime.PyString_FromString(s);
}

protected override void Dealloc()
protected override void Clear()
{
Runtime.Py_CLEAR(ref this.target);
base.Dealloc();
}

public static int tp_clear(IntPtr ob)
{
var self = (EventBinding)GetManagedObject(ob);
Runtime.Py_CLEAR(ref self.target);
return 0;
base.Clear();
}
}
}
4 changes: 2 additions & 2 deletions src/runtime/eventobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,14 @@ public static IntPtr tp_repr(IntPtr ob)
}


protected override void Dealloc()
protected override void Clear()
{
if (this.unbound is not null)
{
Runtime.XDecref(this.unbound.pyHandle);
this.unbound = null;
}
base.Dealloc();
base.Clear();
}
}

Expand Down
26 changes: 21 additions & 5 deletions src/runtime/extensiontype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,21 @@ void SetupGc ()

protected virtual void Dealloc()
{
ClearObjectDict(this.pyHandle);
var type = Runtime.PyObject_TYPE(this.ObjectReference);
Runtime.PyObject_GC_Del(this.pyHandle);
// Not necessary for decref of `tpHandle`.
// Not necessary for decref of `tpHandle` - it is borrowed

this.FreeGCHandle();

// we must decref our type: https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_dealloc
Runtime.XDecref(type.DangerousGetAddress());
}

/// <summary>DecRefs and nulls any fields pointing back to Python</summary>
protected virtual void Clear()
{
ClearObjectDict(this.pyHandle);
// Not necessary for decref of `tpHandle` - it is borrowed
}

/// <summary>
Expand Down Expand Up @@ -88,17 +99,22 @@ public static int tp_descr_set(IntPtr ds, IntPtr ob, IntPtr val)
}


/// <summary>
/// Default dealloc implementation.
/// </summary>
public static void tp_dealloc(IntPtr ob)
{
// Clean up a Python instance of this extension type. This
// frees the allocated Python object and decrefs the type.
var self = (ExtensionType)GetManagedObject(ob);
self?.Clear();
self?.Dealloc();
}

public static int tp_clear(IntPtr ob)
{
var self = (ExtensionType)GetManagedObject(ob);
self?.Clear();
return 0;
}

protected override void OnLoad(InterDomainContext context)
{
base.OnLoad(context);
Expand Down
20 changes: 4 additions & 16 deletions src/runtime/methodbinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,23 +229,11 @@ public static IntPtr tp_repr(IntPtr ob)
return Runtime.PyString_FromString($"<{type} method '{name}'>");
}

private void ClearMembers()
protected override void Clear()
{
Runtime.Py_CLEAR(ref target);
Runtime.Py_CLEAR(ref targetType);
}

protected override void Dealloc()
{
this.ClearMembers();
base.Dealloc();
}

public static int tp_clear(IntPtr ob)
{
var self = (MethodBinding)GetManagedObject(ob);
self.ClearMembers();
return 0;
Runtime.Py_CLEAR(ref this.target);
Runtime.Py_CLEAR(ref this.targetType);
base.Clear();
}

protected override void OnSave(InterDomainContext context)
Expand Down
24 changes: 6 additions & 18 deletions src/runtime/methodobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,29 +200,17 @@ public static IntPtr tp_repr(IntPtr ob)
return Runtime.PyString_FromString($"<method '{self.name}'>");
}

private void ClearMembers()
protected override void Clear()
{
Runtime.Py_CLEAR(ref doc);
if (unbound != null)
Runtime.Py_CLEAR(ref this.doc);
if (this.unbound != null)
{
Runtime.XDecref(unbound.pyHandle);
unbound = null;
Runtime.XDecref(this.unbound.pyHandle);
this.unbound = null;
}
}

protected override void Dealloc()
{
this.ClearMembers();
ClearObjectDict(this.pyHandle);
base.Dealloc();
}

public static int tp_clear(IntPtr ob)
{
var self = (MethodObject)GetManagedObject(ob);
self.ClearMembers();
ClearObjectDict(ob);
return 0;
base.Clear();
}

protected override void OnSave(InterDomainContext context)
Expand Down
19 changes: 6 additions & 13 deletions src/runtime/moduleobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,6 @@ public static IntPtr tp_repr(IntPtr ob)
return Runtime.PyString_FromString($"<module '{self.moduleName}'>");
}

protected override void Dealloc()
{
tp_clear(this.pyHandle);
base.Dealloc();
}

public static int tp_traverse(IntPtr ob, IntPtr visit, IntPtr arg)
{
var self = (ModuleObject)GetManagedObject(ob);
Expand All @@ -314,17 +308,16 @@ public static int tp_traverse(IntPtr ob, IntPtr visit, IntPtr arg)
return 0;
}

public static int tp_clear(IntPtr ob)
protected override void Clear()
{
var self = (ModuleObject)GetManagedObject(ob);
Runtime.Py_CLEAR(ref self.dict);
ClearObjectDict(ob);
foreach (var attr in self.cache.Values)
Runtime.Py_CLEAR(ref this.dict);
ClearObjectDict(this.pyHandle);
foreach (var attr in this.cache.Values)
{
Runtime.XDecref(attr.pyHandle);
}
self.cache.Clear();
return 0;
this.cache.Clear();
base.Clear();
}

protected override void OnSave(InterDomainContext context)
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/overload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public static IntPtr tp_repr(IntPtr op)
return doc;
}

protected override void Dealloc()
protected override void Clear()
{
Runtime.Py_CLEAR(ref this.target);
base.Dealloc();
base.Clear();
}
}
}