Skip to content

Commit 66716db

Browse files
committed
PyScope/PyModule cleanup
removed PyScopeManager merged PyScope into PyModule minor behavioral changes
1 parent 8846fd2 commit 66716db

File tree

10 files changed

+529
-828
lines changed

10 files changed

+529
-828
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ details about the cause of the failure
4444
- floating point values passed from Python are no longer silently truncated
4545
when .NET expects an integer [#1342][i1342]
4646
- More specific error messages for method argument mismatch
47+
- BREAKING: most `PyScope` methods will never return `null`. Instead, `PyObject` `None` will be returned.
48+
- BREAKING: `PyScope` was renamed to `PyModule`
4749
- BREAKING: Methods with `ref` or `out` parameters and void return type return a tuple of only the `ref` and `out` parameters.
4850
- BREAKING: to call Python from .NET `Runtime.PythonDLL` property must be set to Python DLL name
4951
or the DLL must be loaded in advance. This must be done before calling any other Python.NET functions.
@@ -97,6 +99,7 @@ Instead, `PyIterable` does that.
9799

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

src/embed_tests/TestPyScope.cs renamed to src/embed_tests/Modules.cs

+45-25
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55

66
namespace Python.EmbeddingTest
77
{
8-
public class PyScopeTest
8+
public class Modules
99
{
10-
private PyScope ps;
10+
private PyModule ps;
1111

1212
[SetUp]
1313
public void SetUp()
1414
{
1515
using (Py.GIL())
1616
{
1717
ps = Py.CreateScope("test");
18-
}
18+
}
1919
}
2020

2121
[TearDown]
@@ -28,6 +28,18 @@ public void Dispose()
2828
}
2929
}
3030

31+
[OneTimeSetUp]
32+
public void OneTimeSetUp()
33+
{
34+
PythonEngine.Initialize();
35+
}
36+
37+
[OneTimeTearDown]
38+
public void OneTimeTearDown()
39+
{
40+
PythonEngine.Shutdown();
41+
}
42+
3143
/// <summary>
3244
/// Eval a Python expression and obtain its return value.
3345
/// </summary>
@@ -243,7 +255,7 @@ public void TestImportScopeFunction()
243255
"def func1():\n" +
244256
" return cc + bb\n");
245257

246-
using (PyScope scope = ps.NewScope())
258+
using (var scope = ps.NewScope())
247259
{
248260
//'func1' is imported from the origion scope
249261
scope.Exec(
@@ -267,27 +279,6 @@ public void TestImportScopeFunction()
267279
}
268280
}
269281

270-
/// <summary>
271-
/// Import a python module into the session with a new name.
272-
/// Equivalent to the Python "import .. as .." statement.
273-
/// </summary>
274-
[Test]
275-
public void TestImportScopeByName()
276-
{
277-
using (Py.GIL())
278-
{
279-
ps.Set("bb", 100);
280-
281-
using (var scope = Py.CreateScope())
282-
{
283-
scope.ImportAll("test");
284-
//scope.ImportModule("test");
285-
286-
Assert.IsTrue(scope.Contains("bb"));
287-
}
288-
}
289-
}
290-
291282
/// <summary>
292283
/// Use the locals() and globals() method just like in python module
293284
/// </summary>
@@ -381,5 +372,34 @@ public void TestThread()
381372
PythonEngine.EndAllowThreads(ts);
382373
}
383374
}
375+
376+
[Test]
377+
public void TestCreate()
378+
{
379+
using var scope = Py.CreateScope();
380+
381+
Assert.IsFalse(PyModule.SysModules.HasKey("testmod"));
382+
383+
PyModule testmod = new PyModule("testmod");
384+
385+
testmod.SetAttr("testattr1", "True".ToPython());
386+
387+
PyModule.SysModules.SetItem("testmod", testmod);
388+
389+
using PyObject code = PythonEngine.Compile(
390+
"import testmod\n" +
391+
"x = testmod.testattr1"
392+
);
393+
scope.Execute(code);
394+
395+
Assert.IsTrue(scope.TryGet("x", out dynamic x));
396+
Assert.AreEqual("True", x.ToString());
397+
}
398+
399+
[Test]
400+
public void ImportClrNamespace()
401+
{
402+
Py.Import(GetType().Namespace);
403+
}
384404
}
385405
}

src/embed_tests/TestPyModule.cs

-50
This file was deleted.

src/embed_tests/pyinitialize.cs

-9
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,6 @@ public void ReInitialize()
9595
PythonEngine.Shutdown();
9696
}
9797

98-
[Test]
99-
public void TestScopeIsShutdown()
100-
{
101-
PythonEngine.Initialize();
102-
var scope = PyScopeManager.Global.Create("test");
103-
PythonEngine.Shutdown();
104-
Assert.That(PyScopeManager.Global.Contains("test"), Is.False);
105-
}
106-
10798
/// <summary>
10899
/// Helper for testing the shutdown handlers.
109100
/// </summary>

0 commit comments

Comments
 (0)