|
| 1 | +using NUnit.Framework; |
| 2 | +using Python.Runtime; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | + |
| 8 | +namespace Python.EmbeddingTest |
| 9 | +{ |
| 10 | + [TestFixture] |
| 11 | + public class dynamicTest |
| 12 | + { |
| 13 | + private IntPtr gs; |
| 14 | + private outstream stream; |
| 15 | + |
| 16 | + [SetUp]
6854
td> |
| 17 | + public void SetUp() |
| 18 | + { |
| 19 | + PythonEngine.Initialize(); |
| 20 | + gs = PythonEngine.AcquireLock(); |
| 21 | + |
| 22 | + /* redirect sys.stdout to a .NET object */ |
| 23 | + this.stream = new outstream(); |
| 24 | + } |
| 25 | + |
| 26 | + [TearDown] |
| 27 | + public void TearDown() |
| 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 | + public void AssignNone() |
| 38 | + { |
| 39 | + dynamic sys = 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 | + public void AssignObject() |
| 49 | + { |
| 50 | + dynamic sys = 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 | + public void AssignObjectInPython() |
| 67 | + { |
| 68 | + PythonEngine.RunSimpleString(@" |
| 69 | +import sys |
| 70 | +from Python.EmbeddingTest import outstream |
| 71 | +sys.stdout = outstream() |
| 72 | +"); |
| 73 | + dynamic sys = Py.Import("sys"); |
| 74 | + var obj = sys.stdout; |
| 75 | + Assert.IsTrue(obj is outstream); |
| 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 | + public void AssignPyObject() |
| 83 | + { |
| 84 | + dynamic sys = Py.Import("sys"); |
| 85 | + dynamic io = Py.Import("io"); |
| 86 | + sys.stderr = io.StringIO(); |
| 87 | + var bb = 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 | + public void PassObjectInPython() |
| 97 | + { |
| 98 | + dynamic sys = 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 | + public void PassPyObjectInNet() |
| 121 | + { |
| 122 | + dynamic sys = Py.Import("sys"); |
| 123 | + sys.stdout = this.stream; |
| 124 | + sys.stderr = sys.stdout; |
| 125 | + |
| 126 | + //Compare in Python |
| 127 | + var res = 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 | + public class outstream |
| 140 | + { |
| 141 | + public outstream() |
| 142 | + { |
| 143 | + this.buffer = new StringBuilder(); |
| 144 | + } |
| 145 | + private StringBuilder buffer; |
| 146 | + public void write(String str) |
| 147 | + { |
| 148 | + this.buffer.Append(str); |
| 149 | + } |
| 150 | + public void flush() { } |
| 151 | + public void close() { } |
| 152 | + |
| 153 | + public void clear() |
| 154 | + { |
| 155 | + this.buffer.Clear(); |
| 156 | + } |
| 157 | + public string getvalue() |
| 158 | + { |
| 159 | + return this.buffer.ToString(); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments