Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
filmor wants to merge6 commits intopythonnet:master
base:master
Choose a base branch
Loading
fromfilmor:skip-gc-on-process-shutdown
Draft
Show file tree
Hide file tree
Changes fromall 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 deletionssrc/embed_tests/BaseFixture.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/embed_tests/CallableObject.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/embed_tests/ClassManagerTests.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp