1- using NUnit . Framework ;
2- using Python . Runtime ;
31using System ;
4- using System . Collections . Generic ;
5- using System . Linq ;
62using System . Text ;
3+ using NUnit . Framework ;
4+ using Python . Runtime ;
75
86namespace Python . EmbeddingTest
97{
@@ -20,7 +18,7 @@ public void SetUp()
2018 gs = PythonEngine . AcquireLock ( ) ;
2119
2220 /* redirect sys.stdout to a .NET object */
23- this . stream = new outstream ( ) ;
21+ stream = new outstream ( ) ;
2422 }
2523
2624 [ TearDown ]
@@ -48,21 +46,23 @@ public void AssignNone()
4846 public void AssignObject ( )
4947 {
5048 dynamic sys = Py . Import ( "sys" ) ;
51- sys . stdout = this . stream ;
49+ sys . stdout = stream ;
5250 // Check whether there are the same object.
5351 Assert . AreEqual ( sys . stdout , stream ) ;
5452
55- this . stream . clear ( ) ;
53+ stream . clear ( ) ;
5654 PythonEngine . RunSimpleString ( "print('Hello!')" ) ;
5755 Assert . AreEqual ( stream . getvalue ( ) , "Hello!\n " ) ;
5856 }
5957
6058 /// <summary>
6159 /// When the .NET object was created and used in Python side.
6260 /// </summary>
61+ /// <remarks>
62+ /// Needs Pull Request #376 in order for the exception below to show up.
63+ /// </remarks>
6364 [ Test ]
64- //[Explicit]
65- [ Ignore ]
65+ [ Ignore ( "System.ArgumentException : Cannot pass a GCHandle across AppDomains" ) ]
6666 public void AssignObjectInPython ( )
6767 {
6868 PythonEngine . RunSimpleString ( @"
@@ -71,7 +71,7 @@ from Python.EmbeddingTest import outstream
7171sys.stdout = outstream()
7272" ) ;
7373 dynamic sys = Py . Import ( "sys" ) ;
74- var obj = sys . stdout ;
74+ dynamic obj = sys . stdout ;
7575 Assert . IsTrue ( obj is outstream ) ;
7676 }
7777
@@ -84,7 +84,7 @@ public void AssignPyObject()
8484 dynamic sys = Py . Import ( "sys" ) ;
8585 dynamic io = Py . Import ( "io" ) ;
8686 sys . stderr = io . StringIO ( ) ;
87- var bb = sys . stderr ; //Get the PyObject
87+ dynamic bb = sys . stderr ; //Get the PyObject
8888 bb . write ( "Hello!" ) ;
8989 Assert . AreEqual ( bb . getvalue ( ) . ToString ( ) , "Hello!" ) ;
9090 }
@@ -96,20 +96,20 @@ public void AssignPyObject()
9696 public void PassObjectInPython ( )
9797 {
9898 dynamic sys = Py . Import ( "sys" ) ;
99- sys . stdout = this . stream ;
99+ sys . stdout = stream ;
100100
101101 //Pass the .NET object in Python side
102102 PythonEngine . RunSimpleString ( "import sys\n " +
103- "from io import StringIO\n " +
104- "sys.stderr = sys.stdout\n " ) ;
103+ "from io import StringIO\n " +
104+ "sys.stderr = sys.stdout\n " ) ;
105105
106106 //Compare in Python
107- this . stream . clear ( ) ;
107+ stream . clear ( ) ;
108108 PythonEngine . RunSimpleString ( "import sys\n " +
109- "print((sys.stderr is sys.stdout))" ) ;
110- Assert . AreEqual ( this . stream . getvalue ( ) , "True\n " ) ;
109+ "print((sys.stderr is sys.stdout))" ) ;
110+ Assert . AreEqual ( stream . getvalue ( ) , "True\n " ) ;
111111
112- //compare in .NET
112+ //Compare in .NET
113113 Assert . AreEqual ( sys . stdout , sys . stderr ) ;
114114 }
115115
@@ -120,15 +120,15 @@ public void PassObjectInPython()
120120 public void PassPyObjectInNet ( )
121121 {
122122 dynamic sys = Py . Import ( "sys" ) ;
123- sys . stdout = this . stream ;
123+ sys . stdout = stream ;
124124 sys . stderr = sys . stdout ;
125125
126126 //Compare in Python
127- var res = PythonEngine . RunString ( "import sys\n " +
128- "print(sys.stderr is sys.stdout)" ) ;
127+ PyObject res = PythonEngine . RunString ( "import sys\n " +
128+ "print(sys.stderr is sys.stdout)" ) ;
129129 Assert . AreEqual ( sys . stdout . getvalue ( ) . ToString ( ) , "False\n " ) ;
130130
131- //compare in .NET
131+ //Compare in .NET
132132 Assert . AreEqual ( sys . stdout , sys . stderr ) ;
133133 }
134134 }
@@ -138,25 +138,34 @@ public void PassPyObjectInNet()
138138 /// </summary>
139139 public class outstream
140140 {
141+ private StringBuilder buffer ;
142+
141143 public outstream ( )
142144 {
143- this . buffer = new StringBuilder ( ) ;
145+ buffer = new StringBuilder ( ) ;
144146 }
145- private StringBuilder buffer ;
146- public void write ( String str )
147+
148+ public void write ( string str )
149+ {
150+ buffer . Append ( str ) ;
151+ }
152+
153+ public void flush ( )
154+ {
155+ }
156+
157+ public void close ( )
147158 {
148- this . buffer . Append ( str ) ;
149159 }
150- public void flush ( ) { }
151- public void close ( ) { }
152160
153161 public void clear ( )
154162 {
155- this . buffer . Clear ( ) ;
163+ buffer . Clear ( ) ;
156164 }
165+
157166 public string getvalue ( )
158167 {
159- return this . buffer . ToString ( ) ;
168+ return buffer . ToString ( ) ;
160169 }
161170 }
162171}
0 commit comments