GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — pythonnet/src/embed_tests/Modules.cs at master · pythonnet/pythonnet Expand file tree/ Copy pathModules.csMore file actions 449 lines (403 loc) · 14.7 KB / Copy pathModules.csFile metadata and controls449 lines (403 loc) · 14.7 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 using System; using System.Threading; using NUnit.Framework; using Python.Runtime; namespace Python.EmbeddingTest { public class Modules { private PyModule ps; [SetUp] public void SetUp() { using (Py.GIL()) { ps = Py.CreateScope("test"); } } [TearDown] public void Dispose() { using (Py.GIL()) { ps.Dispose(); ps = null; } } /// <summary> /// Eval a Python expression and obtain its return value. /// </summary> [Test] public void TestEval() { using (Py.GIL()) { ps.Set("a", 1); var result = ps.Eval<int>("a + 2"); Assert.That(result, Is.EqualTo(3)); } } /// <summary> /// Exec Python statements and obtain the variables created. /// </summary> [Test] public void TestExec() { using (Py.GIL()) { ps.Set("bb", 100); //declare a global variable ps.Set("cc", 10); //declare a local variable ps.Exec("aa = bb + cc + 3"); var result = ps.Get<int>("aa"); Assert.That(result, Is.EqualTo(113)); } } /// <summary> /// Compile an expression into an ast object; /// Execute the ast and obtain its return value. /// </summary> [Test] public void TestCompileExpression() { using (Py.GIL()) { ps.Set("bb", 100); //declare a global variable ps.Set("cc", 10); //declare a local variable PyObject script = PythonEngine.Compile("bb + cc + 3", "", RunFlagType.Eval); var result = ps.Execute<int>(script); Assert.That(result, Is.EqualTo(113)); } } /// <summary> /// Compile Python statements into an ast object; /// Execute the ast; /// Obtain the local variables created. /// </summary> [Test] public void TestCompileStatements() { using (Py.GIL()) { ps.Set("bb", 100); //declare a global variable ps.Set("cc", 10); //declare a local variable PyObject script = PythonEngine.Compile("aa = bb + cc + 3", "", RunFlagType.File); ps.Execute(script); var result = ps.Get<int>("aa"); Assert.That(result, Is.EqualTo(113)); } } /// <summary> /// Create a function in the scope, then the function can read variables in the scope. /// It cannot write the variables unless it uses the 'global' keyword. /// </summary> [Test] public void TestScopeFunction() { using (Py.GIL()) { ps.Set("bb", 100); ps.Set("cc", 10); ps.Exec( "def func1():\n" + " bb = cc + 10\n"); dynamic func1 = ps.Get("func1"); func1(); //call the function, it can be called any times var result = ps.Get<int>("bb"); Assert.That(result, Is.EqualTo(100)); ps.Set("bb", 100); ps.Set("cc", 10); ps.Exec( "def func2():\n" + " global bb\n" + " bb = cc + 10\n"); dynamic func2 = ps.Get("func2"); func2(); result = ps.Get<int>("bb"); Assert.That(result, Is.EqualTo(20)); } } /// <summary> /// Create a class in the scope, the class can read variables in the scope. /// Its methods can write the variables with the help of 'global' keyword. /// </summary> [Test] public void TestScopeClass() { using (Py.GIL()) { dynamic _ps = ps; _ps.bb = 100; ps.Exec( "class Class1():\n" + " def __init__(self, value):\n" + " self.value = value\n" + " def call(self, arg):\n" + " return self.value + bb + arg\n" + //use scope variables " def update(self, arg):\n" + " global bb\n" + " bb = self.value + arg\n" //update scope variable ); dynamic obj1 = _ps.Class1(20); var result = obj1.call(10).As<int>(); Assert.That(result, Is.EqualTo(130)); obj1.update(10); result = ps.Get<int>("bb"); Assert.That(result, Is.EqualTo(30)); } } /// <summary> /// Create a class in the scope, the class can read variables in the scope. /// Its methods can write the variables with the help of 'global' keyword. /// </summary> [Test] public void TestCreateVirtualPackageStructure() { using (Py.GIL()) { using var _p1 = PyModule.FromString("test", ""); // Sub-module using var _p2 = PyModule.FromString("test.scope", "class Class1():\n" + " def __init__(self, value):\n" + " self.value = value\n" + " def call(self, arg):\n" + " return self.value + bb + arg\n" + // use scope variables " def update(self, arg):\n" + " global bb\n" + " bb = self.value + arg\n", // update scope variable "test" ); dynamic ps2 = Py.Import("test.scope"); ps2.bb = 100; dynamic obj1 = ps2.Class1(20); var result = obj1.call(10).As<int>(); Assert.That(result, Is.EqualTo(130)); obj1.update(10); result = ps2.Get<int>("bb"); Assert.That(result, Is.EqualTo(30)); } } /// <summary> /// Test setting the file attribute via a FromString parameter /// </summary> [Test] public void TestCreateModuleWithFilename() { using var _gil = Py.GIL(); using var mod = PyModule.FromString("mod", ""); using var modWithoutName = PyModule.FromString("mod_without_name", "", " "); using var modNullName = PyModule.FromString("mod_null_name", "", null); using var modWithName = PyModule.FromString("mod_with_name", "", "some_filename"); Assert.That(mod.Get<string>("__file__"), Is.EqualTo("none")); Assert.That(modWithoutName.Get<string>("__file__"), Is.EqualTo("none")); Assert.That(modNullName.Get<string>("__file__"), Is.EqualTo("none")); Assert.That(modWithName.Get<string>("__file__"), Is.EqualTo("some_filename")); } /// <summary> /// Import a python module into the session. /// Equivalent to the Python "import" statement. /// </summary> [Test] public void TestImportModule() { using (Py.GIL()) { dynamic sys = ps.Import("sys"); Assert.That(ps.Contains("sys"), Is.True); ps.Exec("sys.attr1 = 2"); var value1 = ps.Eval<int>("sys.attr1"); var value2 = sys.attr1.As<int>(); Assert.That(value1, Is.EqualTo(2)); Assert.That(value2, Is.EqualTo(2)); //import as ps.Import("sys", "sys1"); Assert.That(ps.Contains("sys1"), Is.True); } } /// <summary> /// Create a scope and import variables from a scope, /// exec Python statements in the scope then discard it. /// </summary> [Test] public void TestImportScope() { using (Py.GIL()) { ps.Set("bb", 100); ps.Set("cc", 10); using (var scope = Py.CreateScope()) { scope.Import(ps, "ps"); scope.Exec("aa = ps.bb + ps.cc + 3"); var result = scope.Get<int>("aa"); Assert.That(result, Is.EqualTo(113)); } Assert.That(ps.Contains("aa"), Is.False); } } /// <summary> /// Create a scope and import variables from a scope, /// exec Python statements in the scope then discard it. /// </summary> [Test] public void TestImportAllFromScope() { using (Py.GIL()) { ps.Set("bb", 100); ps.Set("cc", 10); using (var scope = ps.NewScope()) { scope.Exec("aa = bb + cc + 3"); var result = scope.Get<int>("aa"); Assert.That(result, Is.EqualTo(113)); } Assert.That(ps.Contains("aa"), Is.False); } } /// <summary> /// Create a scope and import variables from a scope, /// call the function imported. /// </summary> [Test] public void TestImportScopeFunction() { using (Py.GIL()) { ps.Set("bb", 100); ps.Set("cc", 10); ps.Exec( "def func1():\n" + " return cc + bb\n"); using (var scope = ps.NewScope()) { //'func1' is imported from the origion scope scope.Exec( "def func2():\n" + " return func1() - cc - bb\n"); dynamic func2 = scope.Get("func2"); var result1 = func2().As<int>(); Assert.That(result1, Is.EqualTo(0)); scope.Set("cc", 20);//it has no effect on the globals of 'func1' var result2 = func2().As<int>(); Assert.That(result2, Is.EqualTo(-10)); scope.Set("cc", 10); //rollback ps.Set("cc", 20); var result3 = func2().As<int>(); Assert.That(result3, Is.EqualTo(10)); ps.Set("cc", 10); //rollback } } } /// <summary> /// Use the locals() and globals() method just like in python module /// </summary> [Test] public void TestVariables() { using (Py.GIL()) { (ps.Variables() as dynamic)["ee"] = new PyInt(200); var a0 = ps.Get<int>("ee"); Assert.That(a0, Is.EqualTo(200)); ps.Exec("locals()['ee'] = 210"); var a1 = ps.Get<int>("ee"); Assert.That(a1, Is.EqualTo(210)); ps.Exec("globals()['ee'] = 220"); var a2 = ps.Get<int>("ee"); Assert.That(a2, Is.EqualTo(220)); using (var item = ps.Variables()) { item["ee"] = new PyInt(230); } var a3 = ps.Get<int>("ee"); Assert.That(a3, Is.EqualTo(230)); } } /// <summary> /// Share a pyscope by multiple threads. /// </summary> [Test] public void TestThread() { //After the proposal here https://github.com/pythonnet/pythonnet/pull/419 complished, //the BeginAllowThreads statement blow and the last EndAllowThreads statement //should be removed. dynamic _ps = ps; var ts = PythonEngine.BeginAllowThreads(); try { using (Py.GIL()) { _ps.res = 0; _ps.bb = 100; _ps.th_cnt = 0; //add function to the scope //can be call many times, more efficient than ast ps.Exec( "import threading\n"+ "lock = threading.Lock()\n"+ "def update():\n" + " global res, th_cnt\n" + " with lock:\n" + " res += bb + 1\n" + " th_cnt += 1\n" ); } int th_cnt = 100; for (int i = 0; i < th_cnt; i++) { System.Threading.Thread th = new System.Threading.Thread(() => { using (Py.GIL()) { //ps.GetVariable<dynamic>("update")(); //call the scope function dynamicly _ps.update(); } }); th.Start(); } //equivalent to Thread.Join, make the main thread join the GIL competition int cnt = 0; while (cnt != th_cnt) { using (Py.GIL()) { cnt = ps.Get<int>("th_cnt"); } Thread.Yield(); } using (Py.GIL()) { var result = ps.Get<int>("res"); Assert.That(result, Is.EqualTo(101 * th_cnt)); } } finally { PythonEngine.EndAllowThreads(ts); } } [Test] public void TestCreate() { using var scope = Py.CreateScope(); Assert.That(PyModule.SysModules.HasKey("testmod"), Is.False); PyModule testmod = new PyModule("testmod"); testmod.SetAttr("testattr1", "True".ToPython()); PyModule.SysModules.SetItem("testmod", testmod); using PyObject code = PythonEngine.Compile( "import testmod\n" + "x = testmod.testattr1" ); scope.Execute(code); Assert.That(scope.TryGet("x", out dynamic x), Is.True); Assert.That(x.ToString(), Is.EqualTo("True")); } [Test] public void ImportClrNamespace() { Py.Import(GetType().Namespace); } } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |