8000 add a scope class to manage the context of interaction with Python an… by yagweb · Pull Request #381 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

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
Get/Set Methods renamed
  • Loading branch information
yagweb committed Jun 1, 2017
commit b9dcdacfd9014d5ad30048d1a3910423372c4c43
88 changes: 44 additions & 44 deletions src/embed_tests/TestPyScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void TestEval()
{
using (Py.GIL())
{
ps.SetVariable("a", 1);
ps.Set("a", 1);
var result = ps.Eval<int>("a + 2");
Assert.AreEqual(3, result);
}
Expand All @@ -49,10 +49,10 @@ public void TestExec()
{
using (Py.GIL())
{
ps.SetVariable("bb", 100); //declare a global variable
ps.SetVariable("cc", 10); //declare a local variable
ps.Set("bb", 100); //declare a global variable
ps.Set("cc", 10); //declare a local variable
ps.Exec("aa = bb + cc + 3");
var result = ps.GetVariable<int>("aa");
var result = ps.Get<int>("aa");
Assert.AreEqual(113, result);
}
}
Expand All @@ -66,8 +66,8 @@ public void TestCompileExpression()
{
using (Py.GIL())
{
ps.SetVariable("bb", 100); //declare a global variable
ps.SetVariable("cc", 10); //declare a local variable
ps.Set("bb", 100); //declare a global variable
ps.Set("cc", 10); //declare a local variable
PyObject script = PythonEngine.Compile("bb + cc + 3", "", RunFlagType.Eval);
var result = ps.Execute<int>(script);
Assert.AreEqual(113, result);
Expand All @@ -84,11 +84,11 @@ public void TestCompileStatements()
{
using (Py.GIL())
{
ps.SetVariable("bb", 100); //declare a global variable
ps.SetVariable("cc", 10); //declare a local variable
ps.Set("bb", 100); //declare a global variable
ps.Set("cc", 10); //declare a local variable
PyObject script = PythonEngine.Compile("aa = bb + cc + 3", "", RunFlagType.File);
ps.Execute(script);
var result = ps.GetVariable<int>("aa");
var result = ps.Get<int>("aa");
Assert.AreEqual(113, result);
}
}
Expand All @@ -102,25 +102,25 @@ public void TestScopeFunction()
{
using (Py.GIL())
{
ps.SetVariable("bb", 100);
ps.SetVariable("cc", 10);
ps.Set("bb", 100);
ps.Set("cc", 10);
ps.Exec(
"def func1():\n" +
" bb = cc + 10\n");
dynamic func1 = ps.GetVariable("func1");
dynamic func1 = ps.Get("func1");
func1(); //call the function, it can be called any times
var result = ps.GetVariable<int>("bb");
var result = ps.Get<int>("bb");
Assert.AreEqual(100, result);

ps.SetVariable("bb", 100);
ps.SetVariable("cc", 10);
ps.Set("bb", 100);
ps.Set("cc", 10);
ps.Exec(
"def func2():\n" +
" global bb\n" +
" bb = cc + 10\n");
dynamic func2 = ps.GetVariable("func2");
dynamic func2 = ps.Get("func2");
func2();
result = ps.GetVariable<int>("bb");
result = ps.Get<int>("bb");
Assert.AreEqual(20, result);
}
}
Expand Down Expand Up @@ -151,7 +151,7 @@ public void TestScopeClass()
Assert.AreEqual(130, result);

obj1.update(10);
result = ps.GetVariable<int>("bb");
result = ps.Get<int>("bb");
Assert.AreEqual(30, result);
}
}
Expand All @@ -166,17 +166,17 @@ public void TestImportModule()
using (Py.GIL())
{
dynamic sys = ps.Import("sys");
Assert.IsTrue(ps.ContainsVariable("sys"));
Assert.IsTrue(ps.Contains("sys"));

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

//import as
ps.Import("sys", "sys1");
Assert.IsTrue(ps.ContainsVariable("sys1"));
Assert.IsTrue(ps.Contains("sys1"));
}
}

Expand All @@ -189,18 +189,18 @@ public void TestImportScope()
{
using (Py.GIL())
{
ps.SetVariable("bb", 100);
ps.SetVariable("cc", 10);
ps.Set("bb", 100);
ps.Set("cc", 10);

using (var scope = Py.CreateScope())
{
scope.Import(ps, "ps");
scope.Exec("aa = ps.bb + ps.cc + 3");
var result = scope.GetVariable<int>("aa");
var result = scope.Get<int>("aa");
Assert.AreEqual(113, result);
}

Assert.IsFalse(ps.ContainsVariable("aa"));
Assert.IsFalse(ps.Contains("aa"));
}
}

Expand All @@ -213,17 +213,17 @@ public void TestImportAllFromScope()
{
using (Py.GIL())
{
ps.SetVariable("bb", 100);
ps.SetVariable("cc", 10);
ps.Set("bb", 100);
ps.Set("cc", 10);

using (var scope = ps.NewScope())
{
scope.Exec("aa = bb + cc + 3");
var result = scope.GetVariable<int>("aa");
var result = scope.Get<int>("aa");
Assert.AreEqual(113, result);
}

Assert.IsFalse(ps.ContainsVariable("aa"));
Assert.IsFalse(ps.Contains("aa"));
}
}

Expand All @@ -236,8 +236,8 @@ public void TestImportScopeFunction()
{
using (Py.GIL())
{
ps.SetVariable("bb", 100);
ps.SetVariable("cc", 10);
ps.Set("bb", 100);
ps.Set("cc", 10);
ps.Exec(
"def func1():\n" +
" return cc + bb\n");
Expand All @@ -248,20 +248,20 @@ public void TestImportScopeFunction()
scope.Exec(
"def func2():\n" +
" return func1() - cc - bb\n");
dynamic func2 = scope.GetVariable("func2");
dynamic func2 = scope.Get("func2");

var result1 = func2().As<int>();
Assert.AreEqual(0, result1);

scope.SetVariable("cc", 20);//it has no effect on the globals of 'func1'
scope.Set("cc", 20);//it has no effect on the globals of 'func1'
var result2 = func2().As<int>();
Assert.AreEqual(-10, result2);
scope.SetVariable("cc", 10); //rollback
scope.Set("cc", 10); //rollback

ps.SetVariable("cc", 20);
ps.Set("cc", 20);
var result3 = func2().As<int>();
Assert.AreEqual(10, result3);
ps.SetVariable("cc", 10); //rollback
ps.Set("cc", 10); //rollback
}
}
}
Expand All @@ -275,14 +275,14 @@ public void TestImportScopeByName()
{
using (Py.GIL())
{
ps.SetVariable("bb", 100);
ps.Set("bb", 100);

using (var scope = Py.CreateScope())
{
scope.ImportAll("test");
//scope.ImportModule("test");

Assert.IsTrue(scope.ContainsVariable("bb"));
Assert.IsTrue(scope.Contains("bb"));
}
}
}
Expand All @@ -294,22 +294,22 @@ public void TestImportScopeByName()
public void TestVariables()
{
(ps.Variables() as dynamic)["ee"] = new PyInt(200);
var a0 = ps.GetVariable<int>("ee");
var a0 = ps.Get<int>("ee");
Assert.AreEqual(200, a0);

ps.Exec("locals()['ee'] = 210");
var a1 = ps.GetVariable<int>("ee");
var a1 = ps.Get<int>("ee");
Assert.AreEqual(210, a1);

ps.Exec("globals()['ee'] = 220");
var a2 = ps.GetVariable<int>("ee");
var a2 = ps.Get<int>("ee");
Assert.AreEqual(220, a2);

using (var item = ps.Variables())
{
item["ee"] = new PyInt(230);
}
var a3 = ps.GetVariable<int>("ee");
var a3 = ps.Get<int>("ee");
Assert.AreEqual(230, a3);
}

Expand Down Expand Up @@ -357,13 +357,13 @@ public void TestThread()
{
using (Py.GIL())
{
cnt = ps.GetVariable<int>("th_cnt");
cnt = ps.Get<int>("th_cnt");
}
System.Threading.Thread.Sleep(10);
}
using (Py.GIL())
{
var result = ps.GetVariable<int>("res");
var result = ps.Get<int>("res");
Assert.AreEqual(101* th_cnt, result);
}
PythonEngine.EndAllowThreads(ts);
Expand Down
Loading
0