@@ -6,69 +6,81 @@ namespace Python.EmbeddingTest
6
6
[ TestFixture ]
7
7
public class PyScopeTest
8
8
{
9
+ PyScope session ;
10
+ PyScope ps ;
11
+
12
+ public PyScopeTest ( )
13
+ {
14
+ session = Py . Session ( ) ;
15
+ }
16
+
9
17
[ SetUp ]
10
18
public void SetUp ( )
11
19
{
12
- PythonEngine . Initialize ( ) ;
20
+ ps = session . Scope ( ) ;
13
21
}
14
22
15
23
[ TearDown ]
16
24
public void TearDown ( )
17
25
{
18
- PythonEngine . Shutdown ( ) ;
26
+ ps . Dispose ( ) ;
19
27
}
20
28
21
29
/// <summary>
22
- /// Set the attribute of a pyobject to null .
30
+ /// Evaluation a Python expression and obtain the value .
23
31
/// </summary>
24
32
[ Test ]
25
33
public void Eval ( )
26
34
{
27
- using ( PySession ps = Py . Session ( ) )
28
- {
29
- PyInt result = PyInt . AsInt ( ps . Eval ( "1+2" ) ) ;
30
- Assert . AreEqual ( result . ToInt32 ( ) , 3 ) ;
31
- }
35
+ ps . SetLocal ( "a" , 1 ) ;
36
+ PyInt result = PyInt . AsInt ( ps . Eval ( "a+2" ) ) ;
37
+ Assert . AreEqual ( result . ToInt32 ( ) , 3 ) ;
32
38
}
33
39
34
40
[ Test ]
35
41
public void Exec ( )
36
42
{
37
- using ( PySession ps = 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
- PyInt result = PyInt . AsInt ( ps . Get ( "aa" ) as PyObject ) ;
43
- Assert . AreEqual ( result . ToInt32 ( ) , 113
10000
span>) ;
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
+ int result = ps . Get < System . Int32 > ( "aa" ) ;
47
+ Assert . AreEqual ( result , 113 ) ;
45
48
}
46
49
47
50
[ Test ]
48
51
public void ExecIn ( )
49
52
{
50
- using ( PySession ps = 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
- PyInt result = PyInt . AsInt ( ps . Get ( "aa" ) as PyObject ) ;
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
+ PyInt result = PyInt . AsInt ( ps . Get ( "aa" ) as PyObject ) ;
57
+ Assert . AreEqual ( result . ToInt32 ( ) , 113 ) ;
58
58
}
59
59
60
60
[ Test ]
61
61
public void Import ( )
62
62
{
63
- using ( PySession ps = 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
+ PyInt result = PyInt . AsInt ( ps . Get ( "aa" ) as PyObject ) ;
67
+ Assert . AreEqual ( result . ToInt32 ( ) , 113 ) ;
68
+ }
69
+
70
+ [ Test ]
71
+ public void Suspend ( )
72
+ {
73
+ var ps = Py . Session ( ) ;
74
+
6D4E
ps . SetGlobal ( "bb" , 100 ) ;
75
+ ps . SetLocal ( "cc" , 10 ) ;
76
+ ps . Suspend ( ) ;
77
+ using ( Py . GIL ( ) )
64
78
{
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
- PyObject result = ps . Eval ( "sys.stdout.getvalue()" ) ;
70
- Assert . AreEqual ( result . ToString ( ) , "Hello!\n " ) ;
79
+ PythonEngine . RunSimpleString ( "import sys;" ) ;
71
80
}
81
+ ps . ExecIn ( "aa=bb+cc+3" ) ;
82
+ int result = ps . Get < System . Int32 > ( "aa" ) ;
83
+ Assert . AreEqual ( result , 113 ) ;
72
84
}
73
85
}
74
86
}
0 commit comments