Thanks to visit codestin.com
Credit goes to github.com

Skip to content

add a scope class to manage the context of interaction with Python an… #381

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 22 commits into from
Jun 9, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add a scope class to manage the context of interaction with Python an…
…d simplify the variable exchanging
  • Loading branch information
yagweb committed Apr 7, 2017
commit 72003aa45b3378e57b18a15b78f016641bdbee44
1 change: 1 addition & 0 deletions src/embed_tests/Python.EmbeddingTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<Compile Include="TestPythonEngineProperties.cs" />
<Compile Include="TestPyTuple.cs" />
<Compile Include="TestRuntime.cs" />
<Compile Include="pyscope.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\runtime\Python.Runtime.csproj">
Expand Down
113 changes: 113 additions & 0 deletions src/embed_tests/pyscope.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using NUnit.Framework;
using Python.Runtime;

namespace Python.EmbeddingTest
{
[TestFixture]
public class PyScopeTest
{
PyScope ps;

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

[TearDown]
public void TearDown()
{
ps.Dispose();
}

/// <summary>
/// Eval a Python expression and obtain its return value.
/// </summary>
[Test]
public void TestEval()
{
ps.SetLocal("a", 1);
int result = ps.Eval<int>("a+2");
Assert.AreEqual(result, 3);
}

/// <summary>
/// Exec Python statements and obtain the local variables created.
/// </summary>
[Test]
public void TestExec()
{
ps.SetGlobal("bb", 100); //declare a global variable
ps.SetLocal("cc", 10); //declare a local variable
ps.Exec("aa=bb+cc+3");
int result = ps.Get<System.Int32>("aa");
Assert.AreEqual(result, 113);
}

/// <summary>
/// Exec Python statements in a subscope of the session then discard it.
/// </summary>
[Test]
public void TestSubScope()
{
ps.SetGlobal("bb", 100); //declare a global variable
ps.SetLocal("cc", 10); //declare a local variable

PyScope scope = ps.SubScope();
scope.Exec("aa=bb+cc+3");
int result = scope.Get<System.Int32>("aa");
Assert.AreEqual(result, 113); //
scope.Dispose();

Assert.IsFalse(ps.Exists("aa"));
}

/// <summary>
/// Import a python module into the session.
/// Equivalent to the Python "import" statement.
/// </summary>
[Test]
public void TestImport()
{
dynamic sys = ps.Import("sys");
Assert.IsTrue(ps.Exists("sys"));

ps.Exec("sys.attr1 = 2");
int value1 = ps.Eval<int>("sys.attr1");
int value2 = (int)sys.attr1.AsManagedObject(typeof(int));
Assert.AreEqual(value1, 2);
Assert.AreEqual(value2, 2);
}

/// <summary>
/// Import a python module into the session with a new name.
/// Equivalent to the Python "import .. as .." statement.
/// </summary>
[Test]
public void TestImportAs()
{
ps.ImportAs("sys", "sys1");
Assert.IsTrue(ps.Exists("sys1"));
}

/// <summary>
/// Suspend the Session, and reuse it later.
/// </summary>
[Test]
public void TestSuspend()
{
ps.SetGlobal("bb", 100);
ps.SetLocal("cc", 10);
ps.Suspend();

using (Py.GIL())
{
PythonEngine.RunSimpleString("import sys;");
}

ps.Exec("aa=bb+cc+3");
int result = ps.Get<System.Int32>("aa");
Assert.AreEqual(result, 113);
}
}
}
Loading