Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
den-run-ai merged 22 commits intopythonnet:masterfromyagweb:add_scope
Jun 9, 2017
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
22 commits
Select commitHold shift + click to select a range
8e0b1a2
Deprecate public RunString
vmuriartMar 3, 2017
72003aa
add a scope class to manage the context of interaction with Python an…
yagwebFeb 15, 2017
efd3798
Rename several methods and add three methods
yagwebFeb 23, 2017
9f50e25
rebased
yagwebFeb 24, 2017
add5ba8
Rebased update
vmuriartFeb 28, 2017
c15555c
format cleanup
vmuriartFeb 28, 2017
b0d57e9
create unnamed pyscope, make PyScope.GILState save
yagwebMar 1, 2017
ceaaef0
fixup! create unnamed pyscope, make PyScope.GILState save
yagwebMar 2, 2017
dec39c7
remove GIL and rebased
yagwebMar 7, 2017
e117d60
Add several methods
yagwebMar 7, 2017
904d9ed
add a Variables method
yagwebMar 14, 2017
5484451
fixup! add a Variables method
yagwebMar 15, 2017
2e063c2
remove private method _GetVariable
yagwebMar 15, 2017
dd492f4
add unit test for Variables() method
yagwebMar 15, 2017
df6a49a
add several methods and rebased
yagwebApr 7, 2017
f35d75b
add a new class PyScopeManager
yagwebApr 24, 2017
497d7aa
cleaned up the Import methods
yagwebApr 28, 2017
60ce28b
Merge branch 'master' into add_scope
May 24, 2017
ebf5a2b
Merge branch 'master' into add_scope
May 24, 2017
2c3db3d
updated according to filmor's comments
yagwebMay 31, 2017
30543eb
fixup! updated according to filmor's comments
yagwebMay 31, 2017
b9dcdac
Get/Set Methods renamed
yagwebJun 1, 2017
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
1 change: 1 addition & 0 deletionssrc/embed_tests/Python.EmbeddingTest.csproj
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -100,6 +100,7 @@
<Compile Include="TestNamedArguments.cs" />
<Compile Include="TestPyWith.cs" />
<Compile Include="TestRuntime.cs" />
<Compile Include="TestPyScope.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\runtime\Python.Runtime.csproj">
Expand Down
372 changes: 372 additions & 0 deletionssrc/embed_tests/TestPyScope.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,372 @@
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.Set("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.Set("bb", 100); //declare a global variable
ps.Set("cc", 10); //declare a local variable
ps.Exec("aa = bb + cc + 3");
var result = ps.Get<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.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);
}
}

/// <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.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.Get<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.Set("bb", 100);
ps.Set("cc", 10);
ps.Exec(
"def func1():\n" +
" bb = cc + 10\n");
dynamic func1 = ps.Get("func1");
func1(); //call the function, it can be called any times
var result = ps.Get<int>("bb");
Assert.AreEqual(100, result);

ps.Set("bb", 100);
ps.Set("cc", 10);
ps.Exec(
"def func2():\n" +
" global bb\n" +
" bb = cc + 10\n");
dynamic func2 = ps.Get("func2");
func2();
result = ps.Get<int>("bb");
Assert.AreEqual(20, result);
}
}

/// <summary>
/// Create a class in the scope, the class can read variables in the scope.
/// Its methods can write the variables with the help of 'global' keyword.
/// </summary>
[Test]
public void TestScopeClass()
{
using (Py.GIL())
{
dynamic _ps = ps;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

why is this dynamic scope necessary?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

oh, I see below, you dynamically accessed the class

_ps.bb = 100;
ps.Exec(
"class Class1():\n" +
" def __init__(self, value):\n" +
" self.value = value\n" +
" def call(self, arg):\n" +
" return self.value + bb + arg\n" + //use scope variables
" def update(self, arg):\n" +
" global bb\n" +
" bb = self.value + arg\n" //update scope variable
);
dynamic obj1 = _ps.Class1(20);
var result = obj1.call(10).As<int>();
Assert.AreEqual(130, result);

obj1.update(10);
result = ps.Get<int>("bb");
Assert.AreEqual(30, 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.Import("sys");
Assert.IsTrue(ps.Contains("sys"));

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

//import as
ps.Import("sys", "sys1");
Assert.IsTrue(ps.Contains("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.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.Get<int>("aa");
Assert.AreEqual(113, result);
}

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

/// <summary>
/// Create a scope and import variables from a scope,
/// exec Python statements in the scope then discard it.
/// </summary>
[Test]
public void TestImportAllFromScope()
{
using (Py.GIL())
{
ps.Set("bb", 100);
ps.Set("cc", 10);

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

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

/// <summary>
/// Create a scope and import variables from a scope,
/// call the function imported.
/// </summary>
[Test]
public void TestImportScopeFunction()
{
using (Py.GIL())
{
ps.Set("bb", 100);
ps.Set("cc", 10);
ps.Exec(
"def func1():\n" +
" return cc + bb\n");

using (PyScope scope = ps.NewScope())
{
//'func1' is imported from the origion scope
scope.Exec(
"def func2():\n" +
" return func1() - cc - bb\n");
dynamic func2 = scope.Get("func2");

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

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

ps.Set("cc", 20);
var result3 = func2().As<int>();
Assert.AreEqual(10, result3);
ps.Set("cc", 10); //rollback
}
}
}

/// <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.Set("bb", 100);

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

Assert.IsTrue(scope.Contains("bb"));
}
}
}

/// <summary>
/// Use the locals() and globals() method just like in python module
/// </summary>
[Test]
public void TestVariables()
{
(ps.Variables() as dynamic)["ee"] = new PyInt(200);
var a0 = ps.Get<int>("ee");
Assert.AreEqual(200, a0);

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

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

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

/// <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.Get<int>("th_cnt");
}
System.Threading.Thread.Sleep(10);
}
using (Py.GIL())
{
var result = ps.Get<int>("res");
Assert.AreEqual(101* th_cnt, result);
}
PythonEngine.EndAllowThreads(ts);
}
}
}
3 changes: 2 additions & 1 deletionsrc/runtime/Python.Runtime.csproj
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand DownExpand Up@@ -127,6 +127,7 @@
<Compile Include="pylong.cs" />
<Compile Include="pynumber.cs" />
<Compile Include="pyobject.cs" />
<Compile Include="pyscope.cs" />
<Compile Include="pysequence.cs" />
<Compile Include="pystring.cs" />
<Compile Include="pythonengine.cs" />
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp