|
| 1 | +using NUnit.Framework; |
| 2 | +using Python.Runtime; |
| 3 | + |
| 4 | +namespace Python.EmbeddingTest |
| 5 | +{ |
| 6 | + [TestFixture] |
| 7 | + public class PyScopeTest |
| 8 | + { |
| 9 | + PyScope ps; |
| 10 | + |
| 11 | + [SetUp] |
| 12 | + public void SetUp() |
| 13 | + { |
| 14 | + ps = Py.Session("test"); |
| 15 | + } |
| 16 | + |
| 17 | + [TearDown] |
| 18 | + public void TearDown() |
| 19 | + { |
| 20 | + ps.Dispose(); |
| 21 | + } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Eval a Python expression and obtain its return value. |
| 25 | + /// </summary> |
| 26 | + [Test] |
| 27 | + public void TestEval() |
| 28 | + { |
| 29 | + ps.SetLocal("a", 1); |
| 30 | + int result = ps.Eval<int>("a+2"); |
| 31 | + Assert.AreEqual(result, 3); |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Exec Python statements and obtain the local variables created. |
| 36 | + /// </summary> |
| 37 | + [Test] |
| 38 | + public void TestExec() |
| 39 | + { |
| 40 | + ps.SetGlobal("bb", 100); //declare a global variable |
| 41 | + ps.SetLocal("cc", 10); //declare a local variable |
| 42 | + ps.Exec("aa=bb+cc+3"); |
| 43 | + int result = ps.Get<System.Int32>("aa"); |
| 44 | + Assert.AreEqual(result, 113); |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Exec Python statements in a subscope of the session then discard it. |
| 49 | + /// </summary> |
| 50 | + [Test] |
| 51 | + public void TestSubScope() |
| 52 | + { |
| 53 | + ps.SetGlobal("bb", 100); //declare a global variable |
| 54 | + ps.SetLocal("cc", 10); //declare a local variable |
| 55 | + |
| 56 | + PyScope scope = ps.SubScope(); |
| 57 | + scope.Exec("aa=bb+cc+3"); |
| 58 | + int result = scope.Get<System.Int32>("aa"); |
| 59 | + Assert.AreEqual(result, 113); // |
| 60 | + scope.Dispose(); |
| 61 | + |
| 62 | + Assert.IsFalse(ps.Exists("aa")); |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Import a python module into the session. |
| 67 | + /// Equivalent to the Python "import" statement. |
| 68 | + /// </summary> |
| 69 | + [Test] |
| 70 | + public void TestImport() |
| 71 | + { |
| 72 | + dynamic sys = ps.Import("sys"); |
| 73 | + Assert.IsTrue(ps.Exists("sys")); |
| 74 | + |
| 75 | + ps.Exec("sys.attr1 = 2"); |
| 76 | + int value1 = ps.Eval<int>("sys.attr1"); |
| 77 | + int value2 = (int)sys.attr1.AsManagedObject(typeof(int)); |
| 78 | + Assert.AreEqual(value1, 2); |
| 79 | + Assert.AreEqual(value2, 2); |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Import a python module into the session with a new name. |
| 84 | + /// Equivalent to the Python "import .. as .." statement. |
| 85 | + /// </summary> |
| 86 | + [Test] |
| 87 | + public void TestImportAs() |
| 88 | + { |
| 89 | + ps.ImportAs("sys", "sys1"); |
| 90 | + Assert.IsTrue(ps.Exists("sys1")); |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Suspend the Session, and reuse it later. |
| 95 | + /// </summary> |
| 96 | + [Test] |
| 97 | + public void TestSuspend() |
| 98 | + { |
| 99 | + ps.SetGlobal("bb", 100); |
| 100 | + ps.SetLocal("cc", 10); |
| 101 | + ps.Suspend(); |
| 102 | + |
| 103 | + using (Py.GIL()) |
| 104 | + { |
| 105 | + PythonEngine.RunSimpleString("import sys;"); |
| 106 | + } |
| 107 | + |
| 108 | + ps.Exec("aa=bb+cc+3"); |
| 109 | + int result = ps.Get<System.Int32>("aa"); |
| 110 | + Assert.AreEqual(result, 113); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments