Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Skip garbage collection on process shutdown #2245

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

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
20 changes: 20 additions & 0 deletions src/embed_tests/BaseFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using NUnit.Framework;
using Python.Runtime;

namespace Python.EmbeddingTest;

public class BaseFixture
{
[OneTimeSetUp]
public void BaseSetup()
{
PythonEngine.Initialize();
}

[OneTimeTearDown]
public void BaseTearDown()
{
PythonEngine.Shutdown(allowReload: true);
PyObjectConversions.Reset();
}
}
126 changes: 64 additions & 62 deletions src/embed_tests/CallableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,83 +5,85 @@

using Python.Runtime;

namespace Python.EmbeddingTest
namespace Python.EmbeddingTest;

public class CallableObject : BaseFixture
{
public class CallableObject
IPythonBaseTypeProvider _provider;

[OneTimeSetUp]
public void SetUp()
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
using var locals = new PyDict();
PythonEngine.Exec(CallViaInheritance.BaseClassSource, locals: locals);
CustomBaseTypeProvider.BaseClass = new PyType(locals[CallViaInheritance.BaseClassName]);
PythonEngine.InteropConfiguration.PythonBaseTypeProviders.Add(new CustomBaseTypeProvider());
}
using var locals = new PyDict();
PythonEngine.Exec(CallViaInheritance.BaseClassSource, locals: locals);
CustomBaseTypeProvider.BaseClass = new PyType(locals[CallViaInheritance.BaseClassName]);
_provider = new CustomBaseTypeProvider();
PythonEngine.InteropConfiguration.PythonBaseTypeProviders.Add(_provider);
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}
[Test]
public void CallMethodMakesObjectCallable()
{
var doubler = new DerivedDoubler();
dynamic applyObjectTo21 = PythonEngine.Eval("lambda o: o(21)");
Assert.AreEqual(doubler.__call__(21), (int)applyObjectTo21(doubler.ToPython()));
}
[Test]
public void CallMethodCanBeInheritedFromPython()
{
var callViaInheritance = new CallViaInheritance();
dynamic applyObjectTo14 = PythonEngine.Eval("lambda o: o(14)");
Assert.AreEqual(callViaInheritance.Call(14), (int)applyObjectTo14(callViaInheritance.ToPython()));
}
[OneTimeTearDown]
public void TearDown()
{
PythonEngine.InteropConfiguration.PythonBaseTypeProviders.Remove(_provider);
}

[Test]
public void CanOverwriteCall()
{
var callViaInheritance = new CallViaInheritance();
using var scope = Py.CreateScope();
scope.Set("o", callViaInheritance);
scope.Exec("orig_call = o.Call");
scope.Exec("o.Call = lambda a: orig_call(a*7)");
int result = scope.Eval<int>("o.Call(5)");
Assert.AreEqual(105, result);
}
[Test]
public void CallMethodMakesObjectCallable()
{
var doubler = new DerivedDoubler();
dynamic applyObjectTo21 = PythonEngine.Eval("lambda o: o(21)");
Assert.AreEqual(doubler.__call__(21), (int)applyObjectTo21(doubler.ToPython()));
}
[Test]
public void CallMethodCanBeInheritedFromPython()
{
var callViaInheritance = new CallViaInheritance();
dynamic applyObjectTo14 = PythonEngine.Eval("lambda o: o(14)");
Assert.AreEqual(callViaInheritance.Call(14), (int)applyObjectTo14(callViaInheritance.ToPython()));
}

class Doubler
{
public int __call__(int arg) => 2 * arg;
}
[Test]
public void CanOverwriteCall()
{
var callViaInheritance = new CallViaInheritance();
using var scope = Py.CreateScope();
scope.Set("o", callViaInheritance);
scope.Exec("orig_call = o.Call");
scope.Exec("o.Call = lambda a: orig_call(a*7)");
int result = scope.Eval<int>("o.Call(5)");
Assert.AreEqual(105, result);
}

class Doubler
{
public int __call__(int arg) => 2 * arg;
}

class DerivedDoubler : Doubler { }
class DerivedDoubler : Doubler { }

class CallViaInheritance
{
public const string BaseClassName = "Forwarder";
public static readonly string BaseClassSource = $@"
class CallViaInheritance
{
public const string BaseClassName = "Forwarder";
public static readonly string BaseClassSource = $@"
class MyCallableBase:
def __call__(self, val):
return self.Call(val)

class {BaseClassName}(MyCallableBase): pass
";
public int Call(int arg) => 3 * arg;
}
public int Call(int arg) => 3 * arg;
}

class CustomBaseTypeProvider : IPythonBaseTypeProvider
{
internal static PyType BaseClass;
class CustomBaseTypeProvider : IPythonBaseTypeProvider
{
internal static PyType BaseClass;

public IEnumerable<PyType> GetBaseTypes(Type type, IList<PyType> existingBases)
{
Assert.Greater(BaseClass.Refcount, 0);
return type != typeof(CallViaInheritance)
? existingBases
: new[] { BaseClass };
}
public IEnumerable<PyType> GetBaseTypes(Type type, IList<PyType> existingBases)
{
Assert.Greater(BaseClass.Refcount, 0);
return type != typeof(CallViaInheritance)
? existingBases
: new[] { BaseClass };
}
}
}
43 changes: 15 additions & 28 deletions src/embed_tests/ClassManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,26 @@

using Python.Runtime;

namespace Python.EmbeddingTest
namespace Python.EmbeddingTest;

public class ClassManagerTests : BaseFixture
{
public class ClassManagerTests
[Test]
public void NestedClassDerivingFromParent()
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}

[Test]
public void NestedClassDerivingFromParent()
{
var f = new NestedTestContainer().ToPython();
f.GetAttr(nameof(NestedTestContainer.Bar));
}
var f = new NestedTestContainer().ToPython();
f.GetAttr(nameof(NestedTestContainer.Bar));
}
}

public class NestedTestParent
public class NestedTestParent
{
public class Nested : NestedTestParent
{
public class Nested : NestedTestParent
{
}
}
}

public class NestedTestContainer
{
public NestedTestParent Bar = new NestedTestParent.Nested();
}
public class NestedTestContainer
{
public NestedTestParent Bar = new NestedTestParent.Nested();
}
Loading