|
| 1 | +using System; |
| 2 | +using NUnit.Framework; |
| 3 | +using Python.Runtime; |
| 4 | + |
| 5 | +namespace Python.EmbeddingTest |
| 6 | +{ |
| 7 | + public class PyScopeTest |
| 8 | + { |
| 9 | + private PyScope ps; |
| 10 | + |
| 11 | + [SetUp] |
| 12 | + public void SetUp() |
| 13 | + { |
| 14 | + using (Py.GIL()) |
| 15 | + { |
| 16 | + ps = Py.CreateScope("test"); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + [TearDown] |
| 21 | + public void Dispose() |
| 22 | + { |
| 23 | + using (Py.GIL()) |
| 24 | + { |
| 25 | + ps.Dispose(); |
| 26 | + ps = null; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Eval a Python expression and obtain its return value. |
| 32 | + /// </summary> |
| 33 | + [Test] |
| 34 | + public void TestEval() |
| 35 | + { |
| 36 | + using (Py.GIL()) |
| 37 | + { |
| 38 | + ps.Set("a", 1); |
| 39 | + var result = ps.Eval<int>("a + 2"); |
| 40 | + Assert.AreEqual(3, result); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Exec Python statements and obtain the variables created. |
| 46 | + /// </summary> |
| 47 | + [Test] |
| 48 | + public void TestExec() |
| 49 | + { |
| 50 | + using (Py.GIL()) |
| 51 | + { |
| 52 | + ps.Set("bb", 100); //declare a global variable |
| 53 | + ps.Set("cc", 10); //declare a local variable |
| 54 | + ps.Exec("aa = bb + cc + 3"); |
| 55 | + var result = ps.Get<int>("aa"); |
| 56 | + Assert.AreEqual(113, result); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Compile an expression into an ast object; |
| 62 | + /// Execute the ast and obtain its return value. |
| 63 | + /// </summary> |
| 64 | + [Test] |
| 65 | + public void TestCompileExpression() |
| 66 | + { |
| 67 | + using (Py.GIL()) |
| 68 | + { |
| 69 | + ps.Set("bb", 100); //declare a global variable |
| 70 | + ps.Set("cc", 10); //declare a local variable |
| 71 | + PyObject script = PythonEngine.Compile("bb + cc + 3", "", RunFlagType.Eval); |
| 72 | + var result = ps.Execute<int>(script); |
| 73 | + Assert.AreEqual(113, result); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Compile Python statements into an ast object; |
| 79 | + /// Execute the ast; |
| 80 | + /// Obtain the local variables created. |
| 81 | + /// </summary> |
| 82 | + [Test] |
| 83 | + public void TestCompileStatements() |
| 84 | + { |
| 85 | + using (Py.GIL()) |
| 86 | + { |
| 87 | + ps.Set("bb", 100); //declare a global variable |
| 88 | + ps.Set("cc", 10); //declare a local variable |
| 89 | + PyObject script = PythonEngine.Compile("aa = bb + cc + 3", "", RunFlagType.File); |
| 90 | + ps.Execute(script); |
| 91 | + var result = ps.Get<int>("aa"); |
| 92 | + Assert.AreEqual(113, result); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Create a function in the scope, then the function can read variables in the scope. |
| 98 | + /// It cannot write the variables unless it uses the 'global' keyword. |
| 99 | + /// </summary> |
| 100 | + [Test] |
| 101 | + public void TestScopeFunction() |
| 102 | + { |
| 103 | + using (Py.GIL()) |
| 104 | + { |
| 105 | + ps.Set("bb", 100); |
| 106 | + ps.Set("cc", 10); |
| 107 | + ps.Exec( |
| 108 | + "def func1():\n" + |
| 109 | + " bb = cc + 10\n"); |
| 110 | + dynamic func1 = ps.Get("func1"); |
| 111 | + func1(); //call the function, it can be called any times |
| 112 | + var result = ps.Get<int>("bb"); |
| 113 | + Assert.AreEqual(100, result); |
| 114 | + |
| 115 | + ps.Set("bb", 100); |
| 116 | + ps.Set("cc", 10); |
| 117 | + ps.Exec( |
| 118 | + "def func2():\n" + |
| 119 | + " global bb\n" + |
| 120 | + " bb = cc + 10\n"); |
| 121 | + dynamic func2 = ps.Get("func2"); |
| 122 | + func2(); |
| 123 | + result = ps.Get<int>("bb"); |
| 124 | + Assert.AreEqual(20, result); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Create a class in the scope, the class can read variables in the scope. |
| 130 | + /// Its methods can write the variables with the help of 'global' keyword. |
| 131 | + /// </summary> |
| 132 | + [Test] |
| 133 | + public void TestScopeClass() |
| 134 | + { |
| 135 | + using (Py.GIL()) |
| 136 | + { |
| 137 | + dynamic _ps = ps; |
| 138 | + _ps.bb = 100; |
| 139 | + ps.Exec( |
| 140 | + "class Class1():\n" + |
| 141 | + " def __init__(self, value):\n" + |
| 142 | + " self.value = value\n" + |
| 143 | + " def call(self, arg):\n" + |
| 144 | + " return self.value + bb + arg\n" + //use scope variables |
| 145 | + " def update(self, arg):\n" + |
| 146 | + " global bb\n" + |
| 147 | + " bb = self.value + arg\n" //update scope variable |
| 148 | + ); |
| 149 | + dynamic obj1 = _ps.Class1(20); |
| 150 | + var result = obj1.call(10).As<int>(); |
| 151 | + Assert.AreEqual(130, result); |
| 152 | + |
| 153 | + obj1.update(10); |
| 154 | + result = ps.Get<int>("bb"); |
| 155 | + Assert.AreEqual(30, result); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + /// <summary> |
| 160 | + /// Import a python module into the session. |
| 161 | + /// Equivalent to the Python "import" statement. |
| 162 | + /// </summary> |
| 163 | + [Test] |
| 164 | + public void TestImportModule() |
| 165 | + { |
| 166 | + using (Py.GIL()) |
| 167 | + { |
| 168 | + dynamic sys = ps.Import("sys"); |
| 169 | + Assert.IsTrue(ps.Contains("sys")); |
| 170 | + |
| 171 | + ps.Exec("sys.attr1 = 2"); |
| 172 | + var value1 = ps.Eval<int>("sys.attr1"); |
| 173 | + var value2 = sys.attr1.As<int>(); |
| 174 | + Assert.AreEqual(2, value1); |
| 175 | + Assert.AreEqual(2, value2); |
| 176 | + |
| 177 | + //import as |
| 178 | + ps.Import("sys", "sys1"); |
| 179 | + Assert.IsTrue(ps.Contains("sys1")); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + /// <summary> |
| 184 | + /// Create a scope and import variables from a scope, |
| 185 | + /// exec Python statements in the scope then discard it. |
| 186 | + /// </summary> |
| 187 | + [Test] |
| 188 | + public void TestImportScope() |
| 189 | + { |
| 190 | + using (Py.GIL()) |
| 191 | + { |
| 192 | + ps.Set("bb", 100); |
| 193 | + ps.Set("cc", 10); |
| 194 | + |
| 195 | + using (var scope = Py.CreateScope()) |
| 196 | + { |
| 197 | + scope.Import(ps, "ps"); |
| 198 | + scope.Exec("aa = ps.bb + ps.cc + 3"); |
| 199 | + var result = scope.Get<int>("aa"); |
| 200 | + Assert.AreEqual(113, result); |
| 201 | + } |
| 202 | + |
| 203 | + Assert.IsFalse(ps.Contains("aa")); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + /// <summary> |
| 208 | + /// Create a scope and import variables from a scope, |
| 209 | + /// exec Python statements in the scope then discard it. |
| 210 | + /// </summary> |
| 211 | + [Test] |
| 212 | + public void TestImportAllFromScope() |
| 213 | + { |
| 214 | + using (Py.GIL()) |
| 215 | + { |
| 216 | + ps.Set("bb", 100); |
| 217 | + ps.Set("cc", 10); |
| 218 | + |
| 219 | + using (var scope = ps.NewScope()) |
| 220 | + { |
| 221 | + scope.Exec("aa = bb + cc + 3"); |
| 222 | + var result = scope.Get<int>("aa"); |
| 223 | + Assert.AreEqual(113, result); |
| 224 | + } |
| 225 | + |
| 226 | + Assert.IsFalse(ps.Contains("aa")); |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + /// <summary> |
| 231 | + /// Create a scope and import variables from a scope, |
| 232 | + /// call the function imported. |
| 233 | + /// </summary> |
| 234 | + [Test] |
| 235 | + public void TestImportScopeFunction() |
| 236 | + { |
| 237 | + using (Py.GIL()) |
| 238 | + { |
| 239 | + ps.Set("bb", 100); |
| 240 | + ps.Set("cc", 10); |
| 241 | + ps.Exec( |
| 242 | + "def func1():\n" + |
| 243 | + " return cc + bb\n"); |
| 244 | + |
| 245 | + using (PyScope scope = ps.NewScope()) |
| 246 | + { |
| 247 | + //'func1' is imported from the origion scope |
| 248 | + scope.Exec( |
| 249 | + "def func2():\n" + |
| 250 | + " return func1() - cc - bb\n"); |
| 251 | + dynamic func2 = scope.Get("func2"); |
| 252 | + |
| 253 | + var result1 = func2().As<int>(); |
| 254 | + Assert.AreEqual(0, result1); |
| 255 | + |
| 256 | + scope.Set("cc", 20);//it has no effect on the globals of 'func1' |
| 257 | + var result2 = func2().As<int>(); |
| 258 | + Assert.AreEqual(-10, result2); |
| 259 | + scope.Set("cc", 10); //rollback |
| 260 | + |
| 261 | + ps.Set("cc", 20); |
| 262 | + var result3 = func2().As<int>(); |
| 263 | + Assert.AreEqual(10, result3); |
| 264 | + ps.Set("cc", 10); //rollback |
| 265 | + } |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + /// <summary> |
| 270 | + /// Import a python module into the session with a new name. |
| 271 | + /// Equivalent to the Python "import .. as .." statement. |
| 272 | + /// </summary> |
| 273 | + [Test] |
| 274 | + public void TestImportScopeByName() |
| 275 | + { |
| 276 | + using (Py.GIL()) |
| 277 | + { |
| 278 | + ps.Set("bb", 100); |
| 279 | + |
| 280 | + using (var scope = Py.CreateScope()) |
| 281 | + { |
| 282 | + scope.ImportAll("test"); |
| 283 | + //scope.ImportModule("test"); |
| 284 | + |
| 285 | + Assert.IsTrue(scope.Contains("bb")); |
| 286 | + } |
| 287 | + } |
| 288 | + } |
| 289 | + |
| 290 | + /// <summary> |
| 291 | + /// Use the locals() and globals() method just like in python module |
| 292 | + /// </summary> |
| 293 | + [Test] |
| 294 | + public void TestVariables() |
| 295 | + { |
| 296 | + (ps.Variables() as dynamic)["ee"] = new PyInt(200); |
| 297 | + var a0 = ps.Get<int>("ee"); |
| 298 | + Assert.AreEqual(200, a0); |
| 299 | + |
| 300 | + ps.Exec("locals()['ee'] = 210"); |
| 301 | + var a1 = ps.Get<int>("ee"); |
| 302 | + Assert.AreEqual(210, a1); |
| 303 | + |
| 304 | + ps.Exec("globals()['ee'] = 220"); |
| 305 | + var a2 = ps.Get<int>("ee"); |
| 306 | + Assert.AreEqual(220, a2); |
| 307 | + |
| 308 | + using (var item = ps.Variables()) |
| 309 | + { |
| 310 | + item["ee"] = new PyInt(230); |
| 311 | + } |
| 312 | + var a3 = ps.Get<int>("ee"); |
| 313 | + Assert.AreEqual(230, a3); |
| 314 | + } |
| 315 | + |
| 316 | + /// <summary> |
| 317 | + /// Share a pyscope by multiple threads. |
| 318 | + /// </summary> |
| 319 | + [Test] |
| 320 | + public void TestThread() |
| 321 | + { |
| 322 | + //After the proposal here https://github.com/pythonnet/pythonnet/pull/419 complished, |
| 323 | + //the BeginAllowThreads statement blow and the last EndAllowThreads statement |
| 324 | + //should be removed. |
| 325 | + dynamic _ps = ps; |
| 326 | + var ts = PythonEngine.BeginAllowThreads(); |
| 327 | + using (Py.GIL()) |
| 328 | + { |
| 329 | + _ps.res = 0; |
| 330 | + _ps.bb = 100; |
| 331 | + _ps.th_cnt = 0; |
| 332 | + //add function to the scope |
| 333 | + //can be call many times, more efficient than ast |
| 334 | + ps.Exec( |
| 335 | + "def update():\n" + |
| 336 | + " global res, th_cnt\n" + |
| 337 | + " res += bb + 1\n" + |
| 338 | + " th_cnt += 1\n" |
| 339 | + ); |
| 340 | + } |
| 341 | + int th_cnt = 3; |
| 342 | + for (int i =0; i< th_cnt; i++) |
| 343 | + { |
| 344 | + System.Threading.Thread th = new System.Threading.Thread(()=> |
| 345 | + { |
| 346 | + using (Py.GIL()) |
| 347 | + { |
| 348 | + //ps.GetVariable<dynamic>("update")(); //call the scope function dynamicly |
| 349 | + _ps.update(); |
| 350 | + } |
| 351 | + }); |
| 352 | + th.Start(); |
| 353 | + } |
| 354 | + //equivalent to Thread.Join, make the main thread join the GIL competition |
| 355 | + int cnt = 0; |
| 356 | + while(cnt != th_cnt) |
| 357 | + { |
| 358 | + using (Py.GIL()) |
| 359 | + { |
| 360 | + cnt = ps.Get<int>("th_cnt"); |
| 361 | + } |
| 362 | + System.Threading.Thread.Sleep(10); |
| 363 | + } |
| 364 | + using (Py.GIL()) |
| 365 | + { |
| 366 | + var result = ps.Get<int>("res"); |
| 367 | + Assert.AreEqual(101* th_cnt, result); |
| 368 | + } |
| 369 | + PythonEngine.EndAllowThreads(ts); |
| 370 | + } |
| 371 | + } |
| 372 | +} |
0 commit comments