|
| 1 | +usingNUnit.Framework; |
| 2 | +usingPython.Runtime; |
| 3 | +usingSystem; |
| 4 | +usingSystem.Collections.Generic; |
| 5 | +usingSystem.Linq; |
| 6 | +usingSystem.Text; |
| 7 | + |
| 8 | +namespacePython.EmbeddingTest |
| 9 | +{ |
| 10 | +[TestFixture] |
| 11 | +publicclassdynamicTest |
| 12 | +{ |
| 13 | +privateIntPtrgs; |
| 14 | +privateoutstreamstream; |
| 15 | + |
| 16 | +[SetUp] |
| 17 | +publicvoidSetUp() |
| 18 | +{ |
| 19 | +PythonEngine.Initialize(); |
| 20 | +gs=PythonEngine.AcquireLock(); |
| 21 | + |
| 22 | +/* redirect sys.stdout to a .NET object */ |
| 23 | +this.stream=newoutstream(); |
| 24 | +} |
| 25 | + |
| 26 | +[TearDown] |
| 27 | +publicvoidTearDown() |
| 28 | +{ |
| 29 | +PythonEngine.ReleaseLock(gs); |
| 30 | +PythonEngine.Shutdown(); |
| 31 | +} |
| 32 | + |
| 33 | +/// <summary> |
| 34 | +/// Set the attribute of a pyobject to null. |
| 35 | +/// </summary> |
| 36 | +[Test] |
| 37 | +publicvoidAssignNone() |
| 38 | +{ |
| 39 | +dynamicsys=Py.Import("sys"); |
| 40 | +sys.stderr=null; |
| 41 | +Assert.IsNull(sys.stderr); |
| 42 | +} |
| 43 | + |
| 44 | +/// <summary> |
| 45 | +/// Set the attribute of a pyobject with a .NET object. |
| 46 | +/// </summary> |
| 47 | +[Test] |
| 48 | +publicvoidAssignObject() |
| 49 | +{ |
| 50 | +dynamicsys=Py.Import("sys"); |
| 51 | +sys.stdout=this.stream; |
| 52 | +// Check whether there are the same object. |
| 53 | +Assert.AreEqual(sys.stdout,stream); |
| 54 | + |
| 55 | +this.stream.clear(); |
| 56 | +PythonEngine.RunSimpleString("print('Hello!')"); |
| 57 | +Assert.AreEqual(stream.getvalue(),"Hello!\n"); |
| 58 | +} |
| 59 | + |
| 60 | +/// <summary> |
| 61 | +/// When the .NET object was created and used in Python side. |
| 62 | +/// </summary> |
| 63 | +[Test] |
| 64 | +//[Explicit] |
| 65 | +[Ignore] |
| 66 | +publicvoidAssignObjectInPython() |
| 67 | +{ |
| 68 | +PythonEngine.RunSimpleString(@" |
| 69 | +import sys |
| 70 | +from Python.EmbeddingTest import outstream |
| 71 | +sys.stdout = outstream() |
| 72 | +"); |
| 73 | +dynamicsys=Py.Import("sys"); |
| 74 | +varobj=sys.stdout; |
| 75 | +Assert.IsTrue(objisoutstream); |
| 76 | +} |
| 77 | + |
| 78 | +/// <summary> |
| 79 | +/// Check whether we can get the attr of a python object when the value of attr is a PyObject. |
| 80 | +/// </summary> |
| 81 | +[Test] |
| 82 | +publicvoidAssignPyObject() |
| 83 | +{ |
| 84 | +dynamicsys=Py.Import("sys"); |
| 85 | +dynamicio=Py.Import("io"); |
| 86 | +sys.stderr=io.StringIO(); |
| 87 | +varbb=sys.stderr;//Get the PyObject |
| 88 | +bb.write("Hello!"); |
| 89 | +Assert.AreEqual(bb.getvalue().ToString(),"Hello!"); |
| 90 | +} |
| 91 | + |
| 92 | +/// <summary> |
| 93 | +/// Pass the .NET object in Python side. |
| 94 | +/// </summary> |
| 95 | +[Test] |
| 96 | +publicvoidPassObjectInPython() |
| 97 | +{ |
| 98 | +dynamicsys=Py.Import("sys"); |
| 99 | +sys.stdout=this.stream; |
| 100 | + |
| 101 | +//Pass the .NET object in Python side |
| 102 | +PythonEngine.RunSimpleString("import sys\n"+ |
| 103 | +"from io import StringIO\n"+ |
| 104 | +"sys.stderr = sys.stdout\n"); |
| 105 | + |
| 106 | +//Compare in Python |
| 107 | +this.stream.clear(); |
| 108 | +PythonEngine.RunSimpleString("import sys\n"+ |
| 109 | +"print((sys.stderr is sys.stdout))"); |
| 110 | +Assert.AreEqual(this.stream.getvalue(),"True\n"); |
| 111 | + |
| 112 | +//compare in .NET |
| 113 | +Assert.AreEqual(sys.stdout,sys.stderr); |
| 114 | +} |
| 115 | + |
| 116 | +/// <summary> |
| 117 | +/// Pass the PyObject in .NET side |
| 118 | +/// </summary> |
| 119 | +[Test] |
| 120 | +publicvoidPassPyObjectInNet() |
| 121 | +{ |
| 122 | +dynamicsys=Py.Import("sys"); |
| 123 | +sys.stdout=this.stream; |
| 124 | +sys.stderr=sys.stdout; |
| 125 | + |
| 126 | +//Compare in Python |
| 127 | +varres=PythonEngine.RunString("import sys\n"+ |
| 128 | +"print(sys.stderr is sys.stdout)"); |
| 129 | +Assert.AreEqual(sys.stdout.getvalue().ToString(),"False\n"); |
| 130 | + |
| 131 | +//compare in .NET |
| 132 | +Assert.AreEqual(sys.stdout,sys.stderr); |
| 133 | +} |
| 134 | +} |
| 135 | + |
| 136 | +/// <summary> |
| 137 | +/// Implement the interface of the sys.stdout redirection |
| 138 | +/// </summary> |
| 139 | +publicclassoutstream |
| 140 | +{ |
| 141 | +publicoutstream() |
| 142 | +{ |
| 143 | +this.buffer=newStringBuilder(); |
| 144 | +} |
| 145 | +privateStringBuilderbuffer; |
| 146 | +publicvoidwrite(Stringstr) |
| 147 | +{ |
| 148 | +this.buffer.Append(str); |
| 149 | +} |
| 150 | +publicvoidflush(){} |
| 151 | +publicvoidclose(){} |
| 152 | + |
| 153 | +publicvoidclear() |
| 154 | +{ |
| 155 | +this.buffer.Clear(); |
| 156 | +} |
| 157 | +publicstringgetvalue() |
| 158 | +{ |
| 159 | +returnthis.buffer.ToString(); |
| 160 | +} |
| 161 | +} |
| 162 | +} |