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

Commit82c21d7

Browse files
committed
fixup! Format pythoengine changes.
1 parent5634ccb commit82c21d7

File tree

2 files changed

+160
-89
lines changed

2 files changed

+160
-89
lines changed

‎src/embed_tests/pyscope.cs

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,81 @@ namespace Python.EmbeddingTest
66
[TestFixture]
77
publicclassPyScopeTest
88
{
9+
PyScopesession;
10+
PyScopeps;
11+
12+
publicPyScopeTest()
13+
{
14+
session=Py.Session();
15+
}
16+
917
[SetUp]
1018
publicvoidSetUp()
1119
{
12-
PythonEngine.Initialize();
20+
ps=session.Scope();
1321
}
1422

1523
[TearDown]
1624
publicvoidTearDown()
1725
{
18-
PythonEngine.Shutdown();
26+
ps.Dispose();
1927
}
2028

2129
/// <summary>
22-
///Set the attribute of a pyobject to null.
30+
///Evaluation a Python expression and obtain the value.
2331
/// </summary>
2432
[Test]
2533
publicvoidEval()
2634
{
27-
using(PySessionps=Py.Session())
28-
{
29-
PyIntresult=PyInt.AsInt(ps.Eval("1+2"));
30-
Assert.AreEqual(result.ToInt32(),3);
31-
}
35+
ps.SetLocal("a",1);
36+
PyIntresult=PyInt.AsInt(ps.Eval("a+2"));
37+
Assert.AreEqual(result.ToInt32(),3);
3238
}
3339

3440
[Test]
3541
publicvoidExec()
3642
{
37-
using(PySessionps=Py.Session())
38-
{
39-
ps.SetGlobal("bb",100);//declare a global variable
40-
ps.SetLocal("cc",10);//declare a local variable
41-
ps.ExecIn("aa=bb+cc+3");
42-
PyIntresult=PyInt.AsInt(ps.Get("aa")asPyObject);
43-
Assert.AreEqual(result.ToInt32(),113);
44-
}
43+
ps.SetGlobal("bb",100);//declare a global variable
44+
ps.SetLocal("cc",10);//declare a local variable
45+
ps.Exec("aa=bb+cc+3");
46+
intresult=ps.Get<System.Int32>("aa");
47+
Assert.AreEqual(result,113);
4548
}
4649

4750
[Test]
4851
publicvoidExecIn()
4952
{
50-
using(PySessionps=Py.Session())
51-
{
52-
ps.SetGlobal("bb",100);//declare a global variable
53-
ps.SetLocal("cc",10);//declare a local variable
54-
ps.ExecIn("aa=bb+cc+3");
55-
PyIntresult=PyInt.AsInt(ps.Get("aa")asPyObject);
56-
Assert.AreEqual(result.ToInt32(),113);
57-
}
53+
ps.SetGlobal("bb",100);//declare a global variable
54+
ps.SetLocal("cc",10);//declare a local variable
55+
ps.ExecIn("aa=bb+cc+3");
56+
PyIntresult=PyInt.AsInt(ps.Get("aa")asPyObject);
57+
Assert.AreEqual(result.ToInt32(),113);
5858
}
5959

6060
[Test]
6161
publicvoidImport()
6262
{
63-
using(PySessionps=Py.Session())
63+
ps.SetGlobal("bb",100);//declare a global variable
64+
ps.SetLocal("cc",10);//declare a local variable
65+
ps.ExecIn("aa=bb+cc+3");
66+
PyIntresult=PyInt.AsInt(ps.Get("aa")asPyObject);
67+
Assert.AreEqual(result.ToInt32(),113);
68+
}
69+
70+
[Test]
71+
publicvoidSuspend()
72+
{
73+
varps=Py.Session();
74+
ps.SetGlobal("bb",100);
75+
ps.SetLocal("cc",10);
76+
ps.Suspend();
77+
using(Py.GIL())
6478
{
65-
ps.Import("sys");//import, it will also be added in globals
66-
ps.Import("io");
67-
ps.ExecIn("sys.stdout = io.StringIO()");//it's working!
68-
ps.ExecIn("print('Hello!')");
69-
PyObjectresult=ps.Eval("sys.stdout.getvalue()");
70-
Assert.AreEqual(result.ToString(),"Hello!\n");
79+
PythonEngine.RunSimpleString("import sys;");
7180
}
81+
ps.ExecIn("aa=bb+cc+3");
82+
intresult=ps.Get<System.Int32>("aa");
83+
Assert.AreEqual(result,113);
7284
}
7385
}
7486
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp