|
| 1 | +using System; |
| 2 | +using System.Text; |
| 3 | +using NUnit.Framework; |
| 4 | +using Python.Runtime; |
| 5 | + |
| 6 | +namespace Python.EmbeddingTest |
| 7 | +{ |
| 8 | + [TestFixture] |
| 9 | + public class dynamicTest |
| 10 | + { |
| 11 | + private IntPtr gs; |
| 12 | + private outstream stream; |
| 13 | + |
| 14 | + [SetUp] |
| 15 | + public void SetUp() |
| 16 | + { |
| 17 | + PythonEngine.Ini
6D47
tialize(); |
| 18 | + gs = PythonEngine.AcquireLock(); |
| 19 | + |
| 20 | + /* redirect sys.stdout to a .NET object */ |
| 21 | + stream = new outstream(); |
| 22 | + } |
| 23 | + |
| 24 | + [TearDown] |
| 25 | + public void TearDown() |
| 26 | + { |
| 27 | + PythonEngine.ReleaseLock(gs); |
| 28 | + PythonEngine.Shutdown(); |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Set the attribute of a pyobject to null. |
| 33 | + /// </summary> |
| 34 | + [Test] |
| 35 | + public void AssignNone() |
| 36 | + { |
| 37 | + dynamic sys = Py.Import("sys"); |
| 38 | + sys.stderr = null; |
| 39 | + Assert.IsNull(sys.stderr); |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Set the attribute of a pyobject with a .NET object. |
| 44 | + /// </summary> |
| 45 | + [Test] |
| 46 | + public void AssignObject() |
| 47 | + { |
| 48 | + dynamic sys = Py.Import("sys"); |
| 49 | + sys.stdout = stream; |
| 50 | + // Check whether there are the same object. |
| 51 | + Assert.AreEqual(sys.stdout, stream); |
| 52 | + |
| 53 | + stream.clear(); |
| 54 | + PythonEngine.RunSimpleString("print('Hello!')"); |
| 55 | + Assert.AreEqual(stream.getvalue(), "Hello!\n"); |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// When the .NET object was created and used in Python side. |
| 60 | + /// </summary> |
| 61 | + /// <remarks> |
| 62 | + /// Needs Pull Request #376 in order for the exception below to show up. |
| 63 | + /// </remarks> |
| 64 | + [Test] |
| 65 | + [Ignore("System.ArgumentException : Cannot pass a GCHandle across AppDomains")] |
| 66 | + public void AssignObjectInPython() |
| 67 | + { |
| 68 | + PythonEngine.RunSimpleString( |
| 69 | + "import sys\n" + |
| 70 | + "from Python.EmbeddingTest import outstream\n" + |
| 71 | + "sys.stdout = outstream()\n" |
| 72 | + ); |
| 73 | + dynamic sys = Py.Import("sys"); |
| 74 | + dynamic 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 | + dynamic 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 = stream; |
| 100 | + |
| 101 | + //Pass the .NET object in Python side |
| 102 | + PythonEngine.RunSimpleString( |
| 103 | + "import sys\n" + |
| 104 | + "from io import StringIO\n" + |
| 105 | + "sys.stderr = sys.stdout\n" |
| 106 | + ); |
| 107 | + |
| 108 | + //Compare in Python |
| 109 | + stream.clear(); |
| 110 | + PythonEngine.RunSimpleString( |
| 111 | + "import sys\n" + |
| 112 | + "print((sys.stderr is sys.stdout))\n" |
| 113 | + ); |
| 114 | + Assert.AreEqual(stream.getvalue(), "True\n"); |
| 115 | + |
| 116 | + //Compare in .NET |
| 117 | + Assert.AreEqual(sys.stdout, sys.stderr); |
| 118 | + } |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Pass the PyObject in .NET side |
| 122 | + /// </summary> |
| 123 | + [Test] |
| 124 | + public void PassPyObjectInNet() |
| 125 | + { |
| 126 | + dynamic sys = P
8090
y.Import("sys"); |
| 127 | + sys.stdout = stream; |
| 128 | + sys.stderr = sys.stdout; |
| 129 | + |
| 130 | + //Compare in Python |
| 131 | + PyObject res = PythonEngine.RunString( |
| 132 | + "import sys\n" + |
| 133 | + "print(sys.stderr is sys.stdout)\n" |
| 134 | + ); |
| 135 | + Assert.AreEqual(sys.stdout.getvalue().ToString(), "False\n"); |
| 136 | + |
| 137 | + //Compare in .NET |
| 138 | + Assert.AreEqual(sys.stdout, sys.stderr); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /// <summary> |
| 143 | + /// Implement the interface of the sys.stdout redirection |
| 144 | + /// </summary> |
| 145 | + public class outstream |
| 146 | + { |
| 147 | + private StringBuilder buffer; |
| 148 | + |
| 149 | + public outstream() |
| 150 | + { |
| 151 | + buffer = new StringBuilder(); |
| 152 | + } |
| 153 | + |
| 154 | + public void write(string str) |
| 155 | + { |
| 156 | + buffer.Append(str); |
| 157 | + } |
| 158 | + |
| 159 | + public void flush() |
| 160 | + { |
| 161 | + } |
| 162 | + |
| 163 | + public void close() |
| 164 | + { |
| 165 | + } |
| 166 | + |
| 167 | + public void clear() |
| 168 | + { |
| 169 | + buffer.Clear(); |
| 170 | + } |
| 171 | + |
| 172 | + public string getvalue() |
| 173 | + { |
| 174 | + return buffer.ToString(); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments