`PyScope`/`PyModule` API cleanup by lostmsu · Pull Request #1569 · pythonnet/pythonnet · GitHub
Skip to content
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
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
Loading