Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
refactored tp_dealloc in ExtensionType and descendants
  • Loading branch information
lostmsu committed May 23, 2021
commit b4cf5a26f5b57015dd945615489a97e889adb97c
20 changes: 6 additions & 14 deletions src/runtime/constructorbinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,10 @@ public static IntPtr tp_repr(IntPtr ob)
return self.repr;
}

/// <summary>
/// ConstructorBinding dealloc implementation.
/// </summary>
public new static void tp_dealloc(IntPtr ob)
protected override void Dealloc()
{
var self = (ConstructorBinding)GetManagedObject(ob);
Runtime.XDecref(self.repr);
self.Dealloc();
Runtime.Py_CLEAR(ref this.repr);
base.Dealloc();
}

public static int tp_clear(IntPtr ob)
Expand Down Expand Up @@ -252,14 +248,10 @@ public static IntPtr tp_repr(IntPtr ob)
return self.repr;
}

/// <summary>
/// ConstructorBinding dealloc implementation.
/// </summary>
public new static void tp_dealloc(IntPtr ob)
protected override void Dealloc()
{
var self = (BoundContructor)GetManagedObject(ob);
Runtime.XDecref(self.repr);
self.Dealloc();
Runtime.Py_CLEAR(ref this.repr);
base.Dealloc();
}

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


/// <summary>
/// EventBinding dealloc implementation.
/// </summary>
public new static void tp_dealloc(IntPtr ob)
protected override void Dealloc()
{
var self = (EventBinding)GetManagedObject(ob);
Runtime.XDecref(self.target);
self.Dealloc();
Runtime.Py_CLEAR(ref this.target);
base.Dealloc();
}

public static int tp_clear(IntPtr ob)
Expand Down
13 changes: 5 additions & 8 deletions src/runtime/eventobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,14 @@ public static IntPtr tp_repr(IntPtr ob)
}


/// <summary>
/// Descriptor dealloc implementation.
/// </summary>
public new static void tp_dealloc(IntPtr ob)
protected override void Dealloc()
{
var self = (EventObject)GetManagedObject(ob);
if (self.unbound != null)
if (this.unbound is not null)
{
Runtime.XDecref(self.unbound.pyHandle);
Runtime.XDecref(this.unbound.pyHandle);
this.unbound = null;
}
self.Dealloc();
base.Dealloc();
}
}

Expand Down
18 changes: 5 additions & 13 deletions src/runtime/extensiontype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,12 @@ void SetupGc ()
}


/// <summary>
/// Common finalization code to support custom tp_deallocs.
/// </summary>
public static void FinalizeObject(ManagedType self)
protected virtual void Dealloc()
{
ClearObjectDict(self.pyHandle);
Runtime.PyObject_GC_Del(self.pyHandle);
ClearObjectDict(this.pyHandle);
Runtime.PyObject_GC_Del(this.pyHandle);
// Not necessary for decref of `tpHandle`.
self.FreeGCHandle();
}

protected void Dealloc()
{
FinalizeObject(this);
this.FreeGCHandle();
}

/// <summary>
Expand Down Expand Up @@ -104,7 +96,7 @@ 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.Dealloc();
self?.Dealloc();
}

protected override void OnLoad(InterDomainContext context)
Expand Down
24 changes: 10 additions & 14 deletions src/runtime/methodbinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public MethodBinding(MethodObject m, IntPtr target, IntPtr targetType)
{
Runtime.XIncref(targetType);
}

this.targetType = targetType;

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

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

/// <summary>
/// Implement binding of generic methods using the subscript syntax [].
/// </summary>
Expand Down Expand Up @@ -235,14 +229,16 @@ public static IntPtr tp_repr(IntPtr ob)
return Runtime.PyString_FromString($"<{type} method '{name}'>");
}

/// <summary>
/// MethodBinding dealloc implementation.
/// </summary>
public new static void tp_dealloc(IntPtr ob)
private void ClearMembers()
{
var self = (MethodBinding)GetManagedObject(ob);
self.ClearMembers();
self.Dealloc();
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)
Expand Down
32 changes: 14 additions & 18 deletions src/runtime/methodobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,6 @@ internal bool IsStatic()
return is_static;
}

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

/// <summary>
/// Descriptor __getattribute__ implementation.
/// </summary>
Expand Down Expand Up @@ -210,15 +200,21 @@ public static IntPtr tp_repr(IntPtr ob)
return Runtime.PyString_FromString($"<method '{self.name}'>");
}

/// <summary>
/// Descriptor dealloc implementation.
/// </summary>
public new static void tp_dealloc(IntPtr ob)
private void ClearMembers()
{
var self = (MethodObject)GetManagedObject(ob);
self.ClearMembers();
ClearObjectDict(ob);
self.Dealloc();
Runtime.Py_CLEAR(ref doc);
if (unbound != null)
{
Runtime.XDecref(unbound.pyHandle);
unbound = null;
}
}

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

public static int tp_clear(IntPtr ob)
Expand Down
13 changes: 6 additions & 7 deletions src/runtime/moduleobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public static IntPtr tp_getattro(IntPtr ob, IntPtr key)
Exceptions.SetError(e);
return IntPtr.Zero;
}


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

public new static void tp_dealloc(IntPtr ob)
protected override void Dealloc()
{
var self = (ModuleObject)GetManagedObject(ob);
tp_clear(ob);
self.Dealloc();
tp_clear(this.pyHandle);
base.Dealloc();
}

public static int tp_traverse(IntPtr ob, IntPtr visit, IntPtr arg)
Expand Down Expand Up @@ -345,7 +344,7 @@ protected override void OnSave(InterDomainContext context)
if ((Runtime.PyDict_DelItemString(DictRef, pair.Key) == -1) &&
(Exceptions.ExceptionMatches(Exceptions.KeyError)))
{
// Trying to remove a key that's not in the dictionary
// Trying to remove a key that's not in the dictionary
// raises an error. We don't care about it.
Runtime.PyErr_Clear();
}
Expand Down Expand Up @@ -496,7 +495,7 @@ public static Assembly AddReference(string name)
/// clr.GetClrType(IComparable) gives you the Type for IComparable,
/// that you can e.g. perform reflection on. Similar to typeof(IComparable) in C#
/// or clr.GetClrType(IComparable) in IronPython.
///
///
/// </summary>
/// <param name="type"></param>
/// <returns>The Type object</returns>
Expand Down
1 change: 0 additions & 1 deletion src/runtime/native/TypeOffset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ static void ValidateRequiredOffsetsPresent(PropertyInfo[] offsetProperties)
"__instancecheck__",
"__subclasscheck__",
"AddReference",
"FinalizeObject",
"FindAssembly",
"get_SuppressDocs",
"get_SuppressOverloads",
Expand Down
10 changes: 3 additions & 7 deletions src/runtime/overload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,10 @@ public static IntPtr tp_repr(IntPtr op)
return doc;
}

/// <summary>
/// OverloadMapper dealloc implementation.
/// </summary>
public new static void tp_dealloc(IntPtr ob)
protected override void Dealloc()
{
var self = (OverloadMapper)GetManagedObject(ob);
Runtime.XDecref(self.target);
self.Dealloc();
Runtime.Py_CLEAR(ref this.target);
base.Dealloc();
}
}
}