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

PyScope/PyModule API cleanup#1569

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

Merged
lostmsu merged 1 commit intopythonnet:masterfromlosttech:pyscope-cleanup
Sep 30, 2021
Merged
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
3 changes: 3 additions & 0 deletionsCHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,6 +44,8 @@ details about the cause of the failure
- floating point values passed from Python are no longer silently truncated
when .NET expects an integer [#1342][i1342]
- More specific error messages for method argument mismatch
- BREAKING: most `PyScope` methods will never return `null`. Instead, `PyObject` `None` will be returned.
- BREAKING: `PyScope` was renamed to `PyModule`
- BREAKING: Methods with `ref` or `out` parameters and void return type return a tuple of only the `ref` and `out` parameters.
- BREAKING: to call Python from .NET `Runtime.PythonDLL` property must be set to Python DLL name
or the DLL must be loaded in advance. This must be done before calling any other Python.NET functions.
Expand DownExpand Up@@ -97,6 +99,7 @@ Instead, `PyIterable` does that.

- implicit assembly loading (you have to explicitly `clr.AddReference` before doing import)
- messages in `PythonException` no longer start with exception type
- `PyScopeManager`, `PyScopeException`, `PyScope` (use `PyModule` instead)
- support for .NET Framework 4.0-4.6; Mono before 5.4. Python.NET now requires .NET Standard 2.0
(see [the matrix](https://docs.microsoft.com/en-us/dotnet/standard/net-standard#net-implementation-support))

Expand Down
70 changes: 45 additions & 25 deletionssrc/embed_tests/TestPyScope.cs → src/embed_tests/Modules.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,17 +5,17 @@

namespace Python.EmbeddingTest
{
public classPyScopeTest
public classModules
{
privatePyScope ps;
privatePyModule ps;

[SetUp]
public void SetUp()
{
using (Py.GIL())
{
ps = Py.CreateScope("test");
}
}
}

[TearDown]
Expand All@@ -28,6 +28,18 @@ public void Dispose()
}
}

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

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

/// <summary>
/// Eval a Python expression and obtain its return value.
/// </summary>
Expand DownExpand Up@@ -243,7 +255,7 @@ public void TestImportScopeFunction()
"def func1():\n" +
" return cc + bb\n");

using (PyScope scope = ps.NewScope())
using (var scope = ps.NewScope())
{
//'func1' is imported from the origion scope
scope.Exec(
Expand All@@ -267,27 +279,6 @@ public void TestImportScopeFunction()
}
}

/// <summary>
/// Import a python module into the session with a new name.
/// Equivalent to the Python "import .. as .." statement.
/// </summary>
[Test]
public void TestImportScopeByName()
{
using (Py.GIL())
{
ps.Set("bb", 100);

using (var scope = Py.CreateScope())
{
scope.ImportAll("test");
//scope.ImportModule("test");

Assert.IsTrue(scope.Contains("bb"));
}
}
}

/// <summary>
/// Use the locals() and globals() method just like in python module
/// </summary>
Expand DownExpand Up@@ -381,5 +372,34 @@ public void TestThread()
PythonEngine.EndAllowThreads(ts);
}
}

[Test]
public void TestCreate()
{
using var scope = Py.CreateScope();

Assert.IsFalse(PyModule.SysModules.HasKey("testmod"));

PyModule testmod = new PyModule("testmod");

testmod.SetAttr("testattr1", "True".ToPython());

PyModule.SysModules.SetItem("testmod", testmod);

using PyObject code = PythonEngine.Compile(
"import testmod\n" +
"x = testmod.testattr1"
);
scope.Execute(code);

Assert.IsTrue(scope.TryGet("x", out dynamic x));
Assert.AreEqual("True", x.ToString());
}

[Test]
public void ImportClrNamespace()
{
Py.Import(GetType().Namespace);
}
}
}
50 changes: 0 additions & 50 deletionssrc/embed_tests/TestPyModule.cs
View file
Open in desktop

This file was deleted.

9 changes: 0 additions & 9 deletionssrc/embed_tests/pyinitialize.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -95,15 +95,6 @@ public void ReInitialize()
PythonEngine.Shutdown();
}

[Test]
public void TestScopeIsShutdown()
{
PythonEngine.Initialize();
var scope = PyScopeManager.Global.Create("test");
PythonEngine.Shutdown();
Assert.That(PyScopeManager.Global.Contains("test"), Is.False);
}

/// <summary>
/// Helper for testing the shutdown handlers.
/// </summary>
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp