Skip to content

Refactoring of tp_dealloc #1459

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
53 changes: 42 additions & 11 deletions src/runtime/ManagedTypes.cd
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
</TypeIdentifier>
</Class>
<Class Name="Python.Runtime.BoundContructor" Collapsed="true">
<Position X="40.75" Y="3.25" Width="1.5" />
<Position X="29.5" Y="4.5" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAACAAAAAAABABAAAAACAAAABAJAAAAAAAAAAIAAAQ=</HashCode>
<HashCode>AAAACAAAAAAABABAAAAACAAAABAJAEAAAAAAAAIAAAA=</HashCode>
<FileName>constructorbinding.cs</FileName>
</TypeIdentifier>
</Class>
Expand All @@ -52,7 +52,7 @@
<Class Name="Python.Runtime.ConstructorBinding" Collapsed="true">
<Position X="29.5" Y="3.25" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAACAAAAAAABAAAAAAACAAAABAJAAAAAAAAAAIAEAQ=</HashCode>
<HashCode>AAAACAAAAAAABAAAAAAACAAAABAJAEAAAAAAAAIAEAA=</HashCode>
<FileName>constructorbinding.cs</FileName>
</TypeIdentifier>
</Class>
Expand All @@ -64,7 +64,15 @@
</TypeIdentifier>
</Class>
<Class Name="Python.Runtime.EventBinding" Collapsed="true">
<Position X="43" Y="3.25" Width="1.5" />
<Position X="22.75" Y="1.75" Width="1.5" />
<InheritanceLine Type="Python.Runtime.ExtensionType" FixedFromPoint="true">
<Path>
<Point X="28.25" Y="2.441" />
<Point X="28.25" Y="2.875" />
<Point X="23.5" Y="2.875" />
<Point X="23.5" Y="2.441" />
</Path>
</InheritanceLine>
<TypeIdentifier>
<HashCode>AAAAAAAAAAAADAAAIAAAEABAAAAAAAACAAAAAAIAAAQ=</HashCode>
<FileName>eventbinding.cs</FileName>
Expand All @@ -87,12 +95,12 @@
<Class Name="Python.Runtime.ExtensionType" Collapsed="true">
<Position X="27.5" Y="1.75" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAAAAAAAECAAAAAEEBAAAAAAABAAQ=</HashCode>
<HashCode>AAAAAAAAAAAAAAAAAAAAAAACAAAAAEEBAAAAAAABAAQ=</HashCode>
<FileName>extensiontype.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="Python.Runtime.FieldObject" Collapsed="true">
<Position X="34" Y="3.25" Width="1.5" />
<Position X="31.75" Y="3.25" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAIBEAA=</HashCode>
<FileName>fieldobject.cs</FileName>
Expand Down Expand Up @@ -120,21 +128,29 @@
</TypeIdentifier>
</Class>
<Class Name="Python.Runtime.MethodBinding" Collapsed="true">
<Position X="31.75" Y="3.25" Width="1.5" />
<Position X="33.75" Y="1.75" Width="1.5" />
<InheritanceLine Type="Python.Runtime.ExtensionType" FixedFromPoint="true" FixedToPoint="true">
<Path>
<Point X="28.25" Y="2.441" />
<Point X="28.25" Y="2.875" />
<Point X="34.5" Y="2.875" />
<Point X="34.5" Y="2.441" />
</Path>
</InheritanceLine>
<TypeIdentifier>
<HashCode>EAAAAAAAAIAADABAIAAAAAAAAAgBAAAAUgAAAAIAAAQ=</HashCode>
<FileName>methodbinding.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="Python.Runtime.MethodObject" Collapsed="true">
<Position X="36.25" Y="3.25" Width="1.5" />
<Position X="33.75" Y="3.25" Width="1.5" />
<TypeIdentifier>
<HashCode>FIADAAAAAAAIBAAAIAAIAAAIAAgFAAAAUAAgAAIAEAQ=</HashCode>
<FileName>methodobject.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="Python.Runtime.ModuleFunctionObject" Collapsed="true">
<Position X="36.25" Y="4.75" Width="1.5" />
<Position X="33.75" Y="4.75" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAIAAAA=</HashCode>
<FileName>modulefunctionobject.cs</FileName>
Expand All @@ -148,18 +164,33 @@
</TypeIdentifier>
</Class>
<Class Name="Python.Runtime.ModulePropertyObject" Collapsed="true">
<Position X="27.25" Y="3.25" Width="1.5" />
<Position X="27.25" Y="4.75" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
<FileName>modulepropertyobject.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="Python.Runtime.PropertyObject" Collapsed="true">
<Position X="38.5" Y="3.25" Width="1.5" />
<Position X="27.25" Y="3.25" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAQAAAAAIBEAg=</HashCode>
<FileName>propertyobject.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="Python.Runtime.OverloadMapper" Collapsed="true">
<Position X="35.5" Y="1.75" Width="1.5" />
<InheritanceLine Type="Python.Runtime.ExtensionType" ManuallyRouted="true" FixedFromPoint="true" FixedToPoint="true">
<Path>
<Point X="28.25" Y="2.441" />
<Point X="28.25" Y="2.875" />
<Point X="36.188" Y="2.875" />
<Point X="36.188" Y="2.441" />
</Path>
</InheritanceLine>
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAIAAAAAAAAAABAAAAAgAAAAIAAAQ=</HashCode>
<FileName>overload.cs</FileName>
</TypeIdentifier>
</Class>
<Font Name="Segoe UI" Size="9" />
</ClassDiagram>
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();
}
}
}