@@ -6,69 +6,81 @@ namespace Python.EmbeddingTest
66 [ TestFixture ]
77 public class PyScopeTest
88 {
9+ PyScope session ;
10+ PyScope ps ;
11+
12+ public PyScopeTest ( )
13+ {
14+ session = Py . Session ( ) ;
15+ }
16+
917 [ SetUp ]
1018 public void SetUp ( )
1119 {
12- PythonEngine . Initialize ( ) ;
20+ ps = session . Scope ( ) ;
1321 }
1422
1523 [ TearDown ]
1624 public void TearDown ( )
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 public void Eval ( )
2634 {
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 ) ;
3238 }
3339
3440 [ Test ]
3541 public void Exec ( )
3642 {
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 ) ;
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 ) ;
4548 }
4649
4750 [ Test ]
4851 public void ExecIn ( )
4952 {
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 ) ;
5858 }
5959
6060 [ Test ]
6161 public void Import ( )
6262 {
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+ 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- PyObject result = 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+ int result = ps . Get < System . Int32 > ( "aa" ) ;
83+ Assert . AreEqual ( result , 113 ) ;
7284 }
7385 }
7486}
0 commit comments