5
5
6
6
namespace Python . EmbeddingTest
7
7
{
8
- public class PyScopeTest
8
+ public class Modules
9
9
{
10
- private PyScope ps ;
10
+ private PyModule ps ;
11
11
12
12
[ SetUp ]
13
13
public void SetUp ( )
14
14
{
15
15
using ( Py . GIL ( ) )
16
16
{
17
17
ps = Py . CreateScope ( "test" ) ;
18
- }
18
+ }
19
19
}
20
20
21
21
[ TearDown ]
@@ -28,6 +28,18 @@ public void Dispose()
28
28
}
29
29
}
30
30
31
+ [ OneTimeSetUp ]
32
+ public void OneTimeSetUp ( )
33
+ {
34
+ PythonEngine . Initialize ( ) ;
35
+ }
36
+
37
+ [ OneTimeTearDown ]
38
+ public void OneTimeTearDown ( )
39
+ {
40
+ PythonEngine . Shutdown ( ) ;
41
+ }
42
+
31
43
/// <summary>
32
44
/// Eval a Python expression and obtain its return value.
33
45
/// </summary>
@@ -243,7 +255,7 @@ public void TestImportScopeFunction()
243
255
"def func1():\n " +
244
256
" return cc + bb\n " ) ;
245
257
246
- using ( PyScope scope = ps . NewScope ( ) )
258
+ using ( var scope = ps . NewScope ( ) )
247
259
{
248
260
//'func1' is imported from the origion scope
249
261
scope . Exec (
@@ -267,27 +279,6 @@ public void TestImportScopeFunction()
267
279
}
268
280
}
269
281
270
- /// <summary>
271
- /// Import a python module into the session with a new name.
272
- /// Equivalent to the Python "import .. as .." statement.
273
- /// </summary>
274
- [ Test ]
275
- public void TestImportScopeByName ( )
276
- {
277
- using ( Py . GIL ( ) )
278
- {
279
- ps . Set ( "bb" , 100 ) ;
280
-
281
- using ( var scope = Py . CreateScope ( ) )
282
- {
283
- scope . ImportAll ( "test" ) ;
284
- //scope.ImportModule("test");
285
-
286
- Assert . IsTrue ( scope . Contains ( "bb" ) ) ;
287
- }
288
- }
289
- }
290
-
291
282
/// <summary>
292
283
/// Use the locals() and globals() method just like in python module
293
284
/// </summary>
@@ -381,5 +372,34 @@ public void TestThread()
381
372
PythonEngine . EndAllowThreads ( ts ) ;
382
373
}
383
374
}
375
+
376
+ [ Test ]
377
+ public void TestCreate ( )
378
+ {
379
+ using var scope = Py . CreateScope ( ) ;
380
+
381
+ Assert . IsFalse ( PyModule . SysModules . HasKey ( "testmod" ) ) ;
382
+
383
+ PyModule testmod = new PyModule ( "testmod" ) ;
384
+
385
+ testmod . SetAttr ( "testattr1" , "True" . ToPython ( ) ) ;
386
+
387
+ PyModule . SysModules . SetItem ( "testmod" , testmod ) ;
388
+
389
+ using PyObject code = PythonEngine . Compile (
390
+ "import testmod\n " +
391
+ "x = testmod.testattr1"
392
+ ) ;
393
+ scope . Execute ( code ) ;
394
+
395
+ Assert . IsTrue ( scope . TryGet ( "x" , out dynamic x ) ) ;
396
+ Assert . AreEqual ( "True" , x . ToString ( ) ) ;
397
+ }
398
+
399
+ [ Test ]
400
+ public void ImportClrNamespace ( )
401
+ {
402
+ Py . Import ( GetType ( ) . Namespace ) ;
403
+ }
384
404
}
385
405
}
0 commit comments