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

Commiteb6c17b

Browse files
committed
add a scope class to manage the context of interaction with Python and simplify the variable exchanging
1 parent6afc9e6 commiteb6c17b

File tree

3 files changed

+401
-7
lines changed

3 files changed

+401
-7
lines changed

‎src/embed_tests/Python.EmbeddingTest.csproj

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<ProjectDefaultTargets="Build"xmlns="http://schemas.microsoft.com/developer/msbuild/2003"ToolsVersion="4.0">
33
<PropertyGroup>
44
<ConfigurationCondition=" '$(Configuration)' == ''">Debug</Configuration>
@@ -30,7 +30,8 @@
3030
<DebugType>full</DebugType>
3131
</PropertyGroup>
3232
<PropertyGroupCondition=" '$(Configuration)' == 'ReleaseMono'">
33-
<DefineConstantsCondition="'$(DefineConstants)' == ''"></DefineConstants>
33+
<DefineConstantsCondition="'$(DefineConstants)' == ''">
34+
</DefineConstants>
3435
<Optimize>true</Optimize>
3536
<DebugType>pdbonly</DebugType>
3637
</PropertyGroup>
@@ -40,7 +41,8 @@
4041
<DebugType>full</DebugType>
4142
</PropertyGroup>
4243
<PropertyGroupCondition=" '$(Configuration)' == 'ReleaseWin'">
43-
<DefineConstantsCondition="'$(DefineConstants)' == ''"></DefineConstants>
44+
<DefineConstantsCondition="'$(DefineConstants)' == ''">
45+
</DefineConstants>
4446
<Optimize>true</Optimize>
4547
<DebugType>pdbonly</DebugType>
4648
</PropertyGroup>
@@ -50,7 +52,8 @@
5052
<DebugType>full</DebugType>
5153
</PropertyGroup>
5254
<PropertyGroupCondition=" '$(Configuration)' == 'ReleaseMonoPY3'">
53-
<DefineConstantsCondition="'$(DefineConstants)' == ''"></DefineConstants>
55+
<DefineConstantsCondition="'$(DefineConstants)' == ''">
56+
</DefineConstants>
5457
<Optimize>true</Optimize>
5558
<DebugType>pdbonly</DebugType>
5659
</PropertyGroup>
@@ -60,7 +63,8 @@
6063
<DebugType>full</DebugType>
6164
</PropertyGroup>
6265
<PropertyGroupCondition=" '$(Configuration)' == 'ReleaseWinPY3'">
63-
<DefineConstantsCondition="'$(DefineConstants)' == ''"></DefineConstants>
66+
<DefineConstantsCondition="'$(DefineConstants)' == ''">
67+
</DefineConstants>
6468
<Optimize>true</Optimize>
6569
<DebugType>pdbonly</DebugType>
6670
</PropertyGroup>
@@ -72,7 +76,9 @@
7276
</ItemGroup>
7377
<ItemGroup>
7478
<NoneInclude="..\pythonnet.snk" />
75-
<NoneInclude="packages.config" />
79+
<NoneInclude="packages.config">
80+
<SubType>Designer</SubType>
81+
</None>
7682
</ItemGroup>
7783
<ItemGroup>
7884
<CompileInclude="pyinitialize.cs" />
@@ -82,6 +88,7 @@
8288
<CompileInclude="pyobject.cs" />
8389
<CompileInclude="pythonexception.cs" />
8490
<CompileInclude="pytuple.cs" />
91+
<CompileInclude="pyscope.cs" />
8592
</ItemGroup>
8693
<ItemGroup>
8794
<ProjectReferenceInclude="..\runtime\Python.Runtime.csproj">
@@ -101,4 +108,4 @@
101108
<CopySourceFiles="$(TargetAssembly)"DestinationFolder="$(PythonBuildDir)" />
102109
<CopySourceFiles="$(TargetAssemblyPdb)"Condition="Exists('$(TargetAssemblyPdb)')"DestinationFolder="$(PythonBuildDir)" />
103110
</Target>
104-
</Project>
111+
</Project>

‎src/embed_tests/pyscope.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
usingNUnit.Framework;
2+
usingPython.Runtime;
3+
usingSystem;
4+
usingSystem.Collections.Generic;
5+
usingSystem.Linq;
6+
usingSystem.Text;
7+
8+
namespacePython.EmbeddingTest
9+
{
10+
[TestFixture]
11+
publicclassPyScopeTest
12+
{
13+
[SetUp]
14+
publicvoidSetUp()
15+
{
16+
PythonEngine.Initialize();
17+
}
18+
19+
[TearDown]
20+
publicvoidTearDown()
21+
{
22+
PythonEngine.Shutdown();
23+
}
24+
25+
/// <summary>
26+
/// Set the attribute of a pyobject to null.
27+
/// </summary>
28+
[Test]
29+
publicvoidEval()
30+
{
31+
using(varps=Py.Session())
32+
{
33+
varresult=PyInt.AsInt(ps.Eval("1+2"));
34+
Assert.AreEqual(result.ToInt32(),3);
35+
}
36+
}
37+
38+
[Test]
39+
publicvoidExec()
40+
{
41+
using(varps=Py.Session())
42+
{
43+
ps.SetGlobal("bb",100);//declare a global variable
44+
ps.SetLocal("cc",10);//declare a local variable
45+
ps.ExecIn("aa=bb+cc+3");
46+
varresult=PyInt.AsInt(ps.Get("aa")asPyObject);
47+
Assert.AreEqual(result.ToInt32(),113);
48+
}
49+
}
50+
51+
[Test]
52+
publicvoidExecIn()
53+
{
54+
using(varps=Py.Session())
55+
{
56+
ps.SetGlobal("bb",100);//declare a global variable
57+
ps.SetLocal("cc",10);//declare a local variable
58+
ps.ExecIn("aa=bb+cc+3");
59+
varresult=PyInt.AsInt(ps.Get("aa")asPyObject);
60+
Assert.AreEqual(result.ToInt32(),113);
61+
}
62+
}
63+
64+
[Test]
65+
publicvoidImport()
66+
{
67+
using(varps=Py.Session())
68+
{
69+
ps.Import("sys");//import, it will also be added in globals
70+
ps.Import("io");
71+
ps.ExecIn("sys.stdout = io.StringIO()");//it's working!
72+
ps.ExecIn("print('Hello!')");
73+
varresult=ps.Eval("sys.stdout.getvalue()");
74+
Assert.AreEqual(result.ToString(),"Hello!\n");
75+
}
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp