|
| 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 Py.GILState gil; |
| 12 | + |
| 13 | + [SetUp] |
| 14 | + public void SetUp() |
| 15 | + { |
| 16 | + gil = Py.GIL(); |
| 17 | + } |
| 18 | + |
| 19 | + [TearDown] |
| 20 | + public void TearDown() |
| 21 | + { |
| 22 | + gil.Dispose(); |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Set the attribute of a pyobject with a .NET object. |
| 27 | + /// </summary> |
| 28 | + [Test] |
| 29 | + public void AssignObject() |
| 30 | + { |
| 31 | + StringBuilder stream = new StringBuilder(); |
| 32 | + dynamic sys = Py.Import("sys"); |
| 33 | + sys.testattr = stream; |
| 34 | + // Check whether there are the same object. |
| 35 | + var _stream = sys.testattr.AsManagedObject(typeof(StringBuilder)); |
| 36 | + Assert.AreEqual(_stream, stream); |
| 37 | + |
| 38 | + PythonEngine.RunSimpleString( |
| 39 | + "import sys\n" + |
| 40 | + "sys.testattr.Append('Hello!')\n"); |
| 41 | + Assert.AreEqual(stream.ToString(), "Hello!"); |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Set the attribute of a pyobject to null. |
| 46 | + /// </summary> |
| 47 | + [Test] |
| 48 | + public void AssignNone() |
| 49 | + { |
| 50 | + dynamic sys = Py.Import("sys"); |
| 51 | + sys.testattr = new StringBuilder(); |
| 52 | + Assert.IsNotNull(sys.testattr); |
| 53 | + |
| 54 | + sys.testattr = null; |
| 55 | + Assert.IsNull(sys.testattr); |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Check whether we can get the attr of a python object when the |
| 60 | + /// value of attr is a PyObject. |
| 61 | + /// </summary> |
| 62 | + [Test] |
| 63 | + public void AssignPyObject() |
| 64 | + { |
| 65 | + dynamic sys = Py.Import("sys"); |
| 66 | + dynamic io = Py.Import("io"); |
| 67 | + sys.testattr = io.StringIO(); |
| 68 | + dynamic bb = sys.testattr; //Get the PyObject |
| 69 | + bb.write("Hello!"); |
| 70 | + Assert.AreEqual(bb.getvalue().ToString(), "Hello!"); |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Pass the .NET object in Python side. |
| 75 | + /// </summary> |
| 76 | + [Test] |
| 77 | + public void PassObjectInPython() |
| 78 | + { |
| 79 | + StringBuilder stream = new StringBuilder(); |
| 80 | + dynamic sys = Py.Import("sys"); |
| 81 | + sys.testattr1 = stream; |
| 82 | + |
| 83 | + //Pass the .NET object in Python side |
| 84 | + PythonEngine.RunSimpleString( |
| 85 | + "import sys\n" + |
| 86 | + "sys.testattr2 = sys.testattr1\n" |
| 87 | + ); |
| 88 | + |
| 89 | + //Compare in Python |
| 90 | + PythonEngine.RunSimpleString( |
| 91 | + "import sys\n" + |
| 92 | + "sys.testattr3 = sys.testattr1 is sys.testattr2\n" |
| 93 | + ); |
| 94 | + Assert.AreEqual(sys.testattr3.ToString(), "True"); |
| 95 | + |
| 96 | + //Compare in .NET |
| 97 | + Assert.AreEqual(sys.testattr1, sys.testattr2); |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Pass the PyObject in .NET side |
| 102 | + /// </summary> |
| 103 | + [Test] |
| 104 | + public void PassPyObjectInNet() |
| 105 | + { |
| 106 | + StringBuilder stream = new StringBuilder(); |
| 107 | + dynamic sys = Py.Import("sys"); |
| 108 | + sys.testattr1 = stream; |
| 109 | + sys.testattr2 = sys.testattr1; |
| 110 | + |
| 111 | + //Compare in Python |
| 112 | + PyObject res = PythonEngine.RunString( |
| 113 | + "import sys\n" + |
| 114 | + "sys.testattr3 = sys.testattr1 is sys.testattr2\n" |
| 115 | + ); |
| 116 | + Assert.AreEqual(sys.testattr3.ToString(), "True"); |
| 117 | + |
| 118 | + //Compare in .NET |
| 119 | + Assert.AreEqual(sys.testattr1, sys.testattr2); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments