Skip to content

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 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 1 commit into from
Sep 30, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff 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 Down Expand 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 deletions src/embed_tests/TestPyScope.cs → src/embed_tests/Modules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

namespace Python.EmbeddingTest
{
public class PyScopeTest
public class Modules
{
private PyScope ps;
private PyModule 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 Down Expand 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 Down Expand 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 deletions src/embed_tests/TestPyModule.cs

This file was deleted.

9 changes: 0 additions & 9 deletions src/embed_tests/pyinitialize.cs
Original file line number Diff line number Diff 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