Skip to content
Merged
Prev Previous commit
Next Next commit
removed code testing possiblity to dispose objects after domain restart
  • Loading branch information
lostmsu committed Nov 12, 2021
commit 60d90c6d6fa5c1fc365487aaff526e38b040475a
110 changes: 0 additions & 110 deletions src/embed_tests/TestDomainReload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,116 +179,6 @@ public static void CrossDomainObject()

#endregion

#region Tempary tests

// https://github.com/pythonnet/pythonnet/pull/1074#issuecomment-596139665
[Test]
public void CrossReleaseBuiltinType()
{
void ExecTest()
{
try
{
PythonEngine.Initialize();
var numRef = CreateNumReference();
Assert.True(numRef.IsAlive);
PythonEngine.Shutdown(); // <- "run" 1 ends
PythonEngine.Initialize(); // <- "run" 2 starts

GC.Collect();
GC.WaitForPendingFinalizers(); // <- this will put former `num` into Finalizer queue
Finalizer.Instance.Collect();
// ^- this will call PyObject.Dispose, which will call XDecref on `num.Handle`,
// but Python interpreter from "run" 1 is long gone, so it will corrupt memory instead.
Assert.False(numRef.IsAlive);
}
finally
{
PythonEngine.Shutdown();
}
}

var errorArgs = new List<Finalizer.ErrorArgs>();
void ErrorHandler(object sender, Finalizer.ErrorArgs e)
{
errorArgs.Add(e);
}
Finalizer.Instance.ErrorHandler += ErrorHandler;
try
{
for (int i = 0; i < 10; i++)
{
ExecTest();
}
}
finally
{
Finalizer.Instance.ErrorHandler -= ErrorHandler;
}
Assert.AreEqual(errorArgs.Count, 0);
}

[Test]
public void CrossReleaseCustomType()
{
void ExecTest()
{
try
{
PythonEngine.Initialize();
var objRef = CreateConcreateObject();
Assert.True(objRef.IsAlive);
PythonEngine.Shutdown(); // <- "run" 1 ends
PythonEngine.Initialize(); // <- "run" 2 starts
GC.Collect();
GC.WaitForPendingFinalizers();
Finalizer.Instance.Collect();
Assert.False(objRef.IsAlive);
}
finally
{
PythonEngine.Shutdown();
}
}

var errorArgs = new List<Finalizer.ErrorArgs>();
void ErrorHandler(object sender, Finalizer.ErrorArgs e)
{
errorArgs.Add(e);
}
Finalizer.Instance.ErrorHandler += ErrorHandler;
try
{
for (int i = 0; i < 10; i++)
{
ExecTest();
}
}
finally
{
Finalizer.Instance.ErrorHandler -= ErrorHandler;
}
Assert.AreEqual(errorArgs.Count, 0);
}

private static WeakReference CreateNumReference()
{
var num = 3216757418.ToPython();
Assert.AreEqual(num.Refcount, 1);
WeakReference numRef = new WeakReference(num, false);
return numRef;
}

private static WeakReference CreateConcreateObject()
{
var obj = new Domain.MyClass().ToPython();
Assert.AreEqual(obj.Refcount, 1);
WeakReference numRef = new WeakReference(obj, false);
return numRef;
}

#endregion Tempary tests

/// <summary>
/// This is a magic incantation required to run code in an application
/// domain other than the current one.
Expand Down