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

Skip to content

Commit eb6c17b

Browse files
committed
add a scope class to manage the context of interaction with Python and simplify the variable exchanging
1 parent 6afc9e6 commit eb6c17b

File tree

3 files changed

+401
-7
lines changed

3 files changed

+401
-7
lines changed

src/embed_tests/Python.EmbeddingTest.csproj

+14-7
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
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
33
<PropertyGroup>
44
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -30,7 +30,8 @@
3030
<DebugType>full</DebugType>
3131
</PropertyGroup>
3232
<PropertyGroup Condition=" '$(Configuration)' == 'ReleaseMono'">
33-
<DefineConstants Condition="'$(DefineConstants)' == ''"></DefineConstants>
33+
<DefineConstants Condition="'$(DefineConstants)' == ''">
34+
</DefineConstants>
3435
<Optimize>true</Optimize>
3536
<DebugType>pdbonly</DebugType>
3637
</PropertyGroup>
@@ -40,7 +41,8 @@
4041
<DebugType>full</DebugType>
4142
</PropertyGroup>
4243
<PropertyGroup Condition=" '$(Configuration)' == 'ReleaseWin'">
43-
<DefineConstants Condition="'$(DefineConstants)' == ''"></DefineConstants>
44+
<DefineConstants Condition="'$(DefineConstants)' == ''">
45+
</DefineConstants>
4446
<Optimize>true</Optimize>
4547
<DebugType>pdbonly</DebugType>
4648
</PropertyGroup>
@@ -50,7 +52,8 @@
5052
<DebugType>full</DebugType>
5153
</PropertyGroup>
5254
<PropertyGroup Condition=" '$(Configuration)' == 'ReleaseMonoPY3'">
53-
<DefineConstants Condition="'$(DefineConstants)' == ''"></DefineConstants>
55+
<DefineConstants Condition="'$(DefineConstants)' == ''">
56+
</DefineConstants>
5457
<Optimize>true</Optimize>
5558
<DebugType>pdbonly</DebugType>
5659
</PropertyGroup>
@@ -60,7 +63,8 @@
6063
<DebugType>full</DebugType>
6164
</PropertyGroup>
6265
<PropertyGroup Condition=" '$(Configuration)' == 'ReleaseWinPY3'">
63-
<DefineConstants Condition="'$(DefineConstants)' == ''"></DefineConstants>
66+
<DefineConstants Condition="'$(DefineConstants)' == ''">
67+
</DefineConstants>
6468
<Optimize>true</Optimize>
6569
<DebugType>pdbonly</DebugType>
6670
</PropertyGroup>
@@ -72,7 +76,9 @@
7276
</ItemGroup>
7377
<ItemGroup>
7478
<None Include="..\pythonnet.snk" />
75-
<None Include="packages.config" />
79+
<None Include="packages.config">
80+
<SubType>Designer</SubType>
81+
</None>
7682
</ItemGroup>
7783
<ItemGroup>
7884
<Compile Include="pyinitialize.cs" />
@@ -82,6 +88,7 @@
8288
<Compile Include="pyobject.cs" />
8389
<Compile Include="pythonexception.cs" />
8490
<Compile Include="pytuple.cs" />
91+
<Compile Include="pyscope.cs" />
8592
</ItemGroup>
8693
<ItemGroup>
8794
<ProjectReference Include="..\runtime\Python.Runtime.csproj">
@@ -101,4 +108,4 @@
101108
<Copy SourceFiles="$(TargetAssembly)" DestinationFolder="$(PythonBuildDir)" />
102109
<Copy SourceFiles="$(TargetAssemblyPdb)" Condition="Exists('$(TargetAssemblyPdb)')" DestinationFolder="$(PythonBuildDir)" />
103110
</Target>
104-
</Project>
111+
</Project>

src/embed_tests/pyscope.cs

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using NUnit.Framework;
2+
using Python.Runtime;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
8+
namespace Python.EmbeddingTest
9+
{
10+
[TestFixture]
11+
public class PyScopeTest
12+
{
13+
[SetUp]
14+
public void SetUp()
15+
{
16+
PythonEngine.Initialize();
17+
}
18+
19+
[TearDown]
20+
public void TearDown()
21+
{
22+
PythonEngine.Shutdown();
23+
}
24+
25+
/// <summary>
26+
/// Set the attribute of a pyobject to null.
27+
/// </summary>
28+
[Test]
29+
public void Eval()
30+
{
31+
using (var ps = Py.Session())
32+
{
33+
var result = PyInt.AsInt(ps.Eval("1+2"));
34+
Assert.AreEqual(result.ToInt32(), 3);
35+
}
36+
}
37+
38+
[Test]
39+
public void Exec()
40+
{
41+
using (var ps = 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+
var result = PyInt.AsInt(ps.Get("aa") as PyObject);
47+
Assert.AreEqual(result.ToInt32(), 113);
48+
}
49+
}
50+
51+
[Test]
52+
public void ExecIn()
53+
{
54+
using (var ps = 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+
var result = PyInt.AsInt(ps.Get("aa") as PyObject);
60+
Assert.AreEqual(result.ToInt32(), 113);
61+
}
62+
}
63+
64+
[Test]
65+
public void Import()
66+
{
67+
using (var ps = 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+
var result = ps.Eval("sys.stdout.getvalue()");
74+
Assert.AreEqual(result.ToString(), "Hello!\n");
75+
}
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)