-
Notifications
You must be signed in to change notification settings - Fork 751
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
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
8e0b1a2
Deprecate public RunString
vmuriart 72003aa
add a scope class to manage the context of interaction with Python an…
yagweb efd3798
Rename several methods and add three methods
yagweb 9f50e25
rebased
yagweb add5ba8
Rebased update
vmuriart c15555c
format cleanup
vmuriart b0d57e9
create unnamed pyscope, make PyScope.GILState save
yagweb ceaaef0
fixup! create unnamed pyscope, make PyScope.GILState save
yagweb dec39c7
remove GIL and rebased
yagweb e117d60
Add several methods
yagweb 904d9ed
add a Variables method
yagweb 5484451
fixup! add a Variables method
yagweb 2e063c2
remove private method _GetVariable
yagweb dd492f4
add unit test for Variables() method
yagweb df6a49a
add several methods and rebased
yagweb f35d75b
add a new class PyScopeManager
yagweb 497d7aa
cleaned up the Import methods
yagweb 60ce28b
Merge branch 'master' into add_scope
ebf5a2b
Merge branch 'master' into add_scope
2c3db3d
updated according to filmor's comments
yagweb 30543eb
fixup! updated according to filmor's comments
yagweb b9dcdac
Get/Set Methods renamed
yagweb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add several methods
add ImportScope: a scope can import variable from any scope, equivalent to python 'import * from mod' add dynamic member support add an OnDispose event remove the field ‘globals’ referring to python module put the scope class in a new file Unit test: TestThread uses scope function replacing Exec/Eval to speed up the execution.
- Loading branch information
commit e117d604b556f318fecb54c59bb5a935cc605ec6
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,290 @@ | ||
using System; | ||
using NUnit.Framework; | ||
using Python.Runtime; | ||
|
||
namespace Python.EmbeddingTest | ||
{ | ||
public class PyScopeTest | ||
{ | ||
private PyScope ps; | ||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
using (Py.GIL()) | ||
{ | ||
ps = Py.CreateScope("test"); | ||
} | ||
} | ||
|
||
[TearDown] | ||
public void Dispose() | ||
{ | ||
using (Py.GIL()) | ||
{ | ||
ps.Dispose(); | ||
ps = null; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Eval a Python expression and obtain its return value. | ||
/// </summary> | ||
[Test] | ||
public void TestEval() | ||
{ | ||
using (Py.GIL()) | ||
{ | ||
ps.SetVariable("a", 1); | ||
var result = ps.Eval<int>("a + 2"); | ||
Assert.AreEqual(3, result); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Exec Python statements and obtain the variables created. | ||
/// </summary> | ||
[Test] | ||
public void TestExec() | ||
{ | ||
using (Py.GIL()) | ||
{ | ||
ps.SetVariable("bb", 100); //declare a global variable | ||
ps.SetVariable("cc", 10); //declare a local variable | ||
ps.Exec("aa = bb + cc + 3"); | ||
var result = ps.GetVariable<int>("aa"); | ||
Assert.AreEqual(113, result); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Compile an expression into an ast object; | ||
/// Execute the ast and obtain its return value. | ||
/// </summary> | ||
[Test] | ||
public void TestCompileExpression() | ||
{ | ||
using (Py.GIL()) | ||
{ | ||
ps.SetVariable("bb", 100); //declare a global variable | ||
ps.SetVariable("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); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Compile Python statements into an ast object; | ||
/// Execute the ast; | ||
/// Obtain the local variables created. | ||
/// </summary> | ||
[Test] | ||
public void TestCompileStatements() | ||
{ | ||
using (Py.GIL()) | ||
{ | ||
ps.SetVariable("bb", 100); //declare a global variable | ||
ps.SetVariable("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"); | ||
Assert.AreEqual(113, result); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Create a function in the scope, then the function can read variables in the scope. | ||
/// It cannot write the variables unless it uses the 'global' keyword. | ||
/// </summary> | ||
[Test] | ||
public void TestScopeFunction() | ||
{ | ||
using (Py.GIL()) | ||
{ | ||
ps.SetVariable("bb", 100); | ||
ps.SetVariable("cc", 10); | ||
ps.Exec( | ||
"def func1():\n" + | ||
" bb = cc + 10\n"); | ||
dynamic func1 = ps.GetVariable("func1"); | ||
func1(); //call the function, it can be called any times | ||
var result = ps.GetVariable<int>("bb"); | ||
Assert.AreEqual(100, result); | ||
|
||
ps.SetVariable("bb", 100); | ||
ps.SetVariable("cc", 10); | ||
ps.Exec( | ||
"def func2():\n" + | ||
" global bb\n" + | ||
" bb = cc + 10\n"); | ||
dynamic func2 = ps.GetVariable("func2"); | ||
func2(); | ||
result = ps.GetVariable<int>("bb"); | ||
Assert.AreEqual(20, result); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Import a python module into the session. | ||
/// Equivalent to the Python "import" statement. | ||
/// </summary> | ||
[Test] | ||
public void TestImportModule() | ||
{ | ||
using (Py.GIL()) | ||
{ | ||
dynamic sys = ps.ImportModule("sys"); | ||
Assert.IsTrue(ps.ContainsVariable("sys")); | ||
|
||
ps.Exec("sys.attr1 = 2"); | ||
var value1 = ps.Eval<int>("sys.attr1"); | ||
var value2 = (int)sys.attr1.AsManagedObject(typeof(int)); | ||
Assert.AreEqual(2, value1); | ||
Assert.AreEqual(2, value2); | ||
|
||
//import as | ||
ps.ImportModule("sys", "sys1"); | ||
Assert.IsTrue(ps.ContainsVariable("sys1")); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Create a scope and import variables from a scope, | ||
/// exec Python statements in the scope then discard it. | ||
/// </summary> | ||
[Test] | ||
public void TestImportScope() | ||
{ | ||
using (Py.GIL()) | ||
{ | ||
ps.SetVariable("bb", 100); | ||
ps.SetVariable("cc", 10); | ||
|
||
PyScope scope = ps.CreateScope(); | ||
|
||
scope.Exec("aa = bb + cc + 3"); | ||
var result = scope.GetVariable<int>("aa"); | ||
Assert.AreEqual(113, result); | ||
|
||
scope.Dispose(); | ||
|
||
Assert.IsFalse(ps.ContainsVariable("aa")); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Create a scope and import variables from a scope, | ||
/// call the function imported. | ||
/// </summary> | ||
[Test] | ||
public void TestImportScopeFunction() | ||
{ | ||
using (Py.GIL()) | ||
{ | ||
ps.SetVariable("bb", 100); | ||
ps.SetVariable("cc", 10); | ||
ps.Exec( | ||
"def func1():\n" + | ||
" return cc + bb\n"); | ||
|
||
PyScope scope = ps.CreateScope(); | ||
|
||
//'func1' is imported from the origion scope | ||
scope.Exec( | ||
"def func2():\n" + | ||
" return func1() - cc - bb\n"); | ||
dynamic func2 = scope.GetVariable("func2"); | ||
|
||
var result1 = func2().AsManagedObject(typeof(int)); | ||
Assert.AreEqual(0, result1); | ||
|
||
scope.SetVariable("cc", 20);//it has no effect on the globals of 'func1' | ||
var result2 = func2().AsManagedObject(typeof(int)); | ||
Assert.AreEqual(-10, result2); | ||
scope.SetVariable("cc", 10); //rollback | ||
|
||
ps.SetVariable("cc", 20); | ||
var result3 = func2().AsManagedObject(typeof(int)); | ||
Assert.AreEqual(10, result3); | ||
ps.SetVariable("cc", 10); //rollback | ||
|
||
scope.Dispose(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, wrap in |
||
} | ||
} | ||
|
||
/// <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.SetVariable("bb", 100); | ||
|
||
var scope = Py.CreateScope(); | ||
scope.ImportScope("test"); | ||
|
||
Assert.IsTrue(scope.ContainsVariable("bb")); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Share a pyscope by multiple threads. | ||
/// </summary> | ||
[Test] | ||
public void TestThread() | ||
{ | ||
//After the proposal here https://github.com/pythonnet/pythonnet/pull/419 complished, | ||
//the BeginAllowThreads statement blow and the last EndAllowThreads statement | ||
//should be removed. | ||
dynamic _ps = ps; | ||
var ts = PythonEngine.BeginAllowThreads(); | ||
using (Py.GIL()) | ||
{ | ||
_ps.res = 0; | ||
_ps.bb = 100; | ||
_ps.th_cnt = 0; | ||
//add function to the scope | ||
//can be call many times, more efficient than ast | ||
ps.Exec( | ||
"def update():\n" + | ||
" global res, th_cnt\n" + | ||
" res += bb + 1\n" + | ||
" th_cnt += 1\n" | ||
); | ||
} | ||
int th_cnt = 3; | ||
for (int i =0; i< th_cnt; i++) | ||
{ | ||
System.Threading.Thread th = new System.Threading.Thread(()=> | ||
{ | ||
using (Py.GIL()) | ||
{ | ||
//ps.GetVariable<dynamic>("update")(); //call the scope function dynamicly | ||
_ps.update(); | ||
} | ||
}); | ||
th.Start(); | ||
} | ||
//equivalent to Thread.Join, make the main thread join the GIL competition | ||
int cnt = 0; | ||
while(cnt != th_cnt) | ||
{ | ||
using (Py.GIL()) | ||
{ | ||
cnt = ps.GetVariable<int>("th_cnt"); | ||
} | ||
System.Threading.Thread.Sleep(10); | ||
} | ||
using (Py.GIL()) | ||
{ | ||
var result = ps.GetVariable<int>("res"); | ||
Assert.AreEqual(101* th_cnt, result); | ||
} | ||
PythonEngine.EndAllowThreads(ts); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
using
to give a good example?