@@ -51,7 +51,7 @@ public void TestEval()
51
51
ps . Set ( "a" , 1 ) ;
52
52
var result = ps . Eval < int > ( "a + 2" ) ;
53
53
Assert . AreEqual ( 3 , result ) ;
54
- }
54
+ }
55
55
}
56
56
57
57
/// <summary>
@@ -169,6 +169,62 @@ public void TestScopeClass()
169
169
}
170
170
}
171
171
172
+ /// <summary>
173
+ /// Create a class in the scope, the class can read variables in the scope.
174
+ /// Its methods can write the variables with the help of 'global' keyword.
175
+ /// </summary>
176
+ [ Test ]
177
+ public void TestCreateVirtualPackageStructure ( )
178
+ {
179
+ using ( Py . GIL ( ) )
180
+ {
181
+ using var _p1 = PyModule . FromString ( "test" , "" ) ;
182
+ // Sub-module
183
+ using var _p2 = PyModule . FromString ( "test.scope" ,
184
+ "class Class1():\n " +
185
+ " def __init__(self, value):\n " +
186
+ " self.value = value\n " +
187
+ " def call(self, arg):\n " +
188
+ " return self.value + bb + arg\n " + // use scope variables
189
+ " def update(self, arg):\n " +
190
+ " global bb\n " +
191
+ " bb = self.value + arg\n " , // update scope variable
192
+ "test"
193
+ ) ;
194
+
195
+ dynamic ps2 = Py . Import ( "test.scope" ) ;
196
+ ps2 . bb = 100 ;
197
+
198
+ dynamic obj1 = ps2 . Class1 ( 20 ) ;
199
+ var result = obj1 . call ( 10 ) . As < int > ( ) ;
200
+ Assert . AreEqual ( 130 , result ) ;
201
+
202
+ obj1 . update ( 10 ) ;
203
+ result = ps2 . Get < int > ( "bb" ) ;
204
+ Assert . AreEqual ( 30 , result ) ;
205
+ }
206
+ }
207
+
208
+ /// <summary>
209
+ /// Test setting the file attribute via a FromString parameter
210
+ /// </summary>
211
+ [ Test ]
212
+ public void TestCreateModuleWithFilename ( )
213
+ {
214
+ using var _gil = Py . GIL ( ) ;
215
+
216
+ using var mod = PyModule . FromString ( "mod" , "" ) ;
217
+ using var modWithoutName = PyModule . FromString ( "mod_without_name" , "" , " " ) ;
218
+ using var modNullName = PyModule . FromString ( "mod_null_name" , "" , null ) ;
219
+
220
+ using var modWithName = PyModule . FromString ( "mod_with_name" , "" , "some_filename" ) ;
221
+
222
+ Assert . AreEqual ( "none" , mod . Get < string > ( "__file__" ) ) ;
223
+ Assert . AreEqual ( "none" , modWithoutName . Get < string > ( "__file__" ) ) ;
224
+ Assert . AreEqual ( "none" , modNullName . Get < string > ( "__file__" ) ) ;
225
+ Assert . AreEqual ( "some_filename" , modWithName . Get < string > ( "__file__" ) ) ;
226
+ }
227
+
172
228
/// <summary>
173
229
/// Import a python module into the session.
174
230
/// Equivalent to the Python "import" statement.
@@ -194,7 +250,7 @@ public void TestImportModule()
194
250
}
195
251
196
252
/// <summary>
197
- /// Create a scope and import variables from a scope,
253
+ /// Create a scope and import variables from a scope,
198
254
/// exec Python statements in the scope then discard it.
199
255
/// </summary>
200
256
[ Test ]
@@ -218,7 +274,7 @@ public void TestImportScope()
218
274
}
219
275
220
276
/// <summary>
221
- /// Create a scope and import variables from a scope,
277
+ /// Create a scope and import variables from a scope,
222
278
/// exec Python statements in the scope then discard it.
223
279
/// </summary>
224
280
[ Test ]
@@ -241,7 +297,7 @@ public void TestImportAllFromScope()
241
297
}
242
298
243
299
/// <summary>
244
- /// Create a scope and import variables from a scope,
300
+ /// Create a scope and import variables from a scope,
245
301
/// call the function imported.
246
302
/// </summary>
247
303
[ Test ]
@@ -286,7 +342,7 @@ public void TestImportScopeFunction()
286
342
public void TestVariables ( )
287
343
{
288
344
using ( Py . GIL ( ) )
289
- {
345
+ {
290
346
( ps . Variables ( ) as dynamic ) [ "ee" ] = new PyInt ( 200 ) ;
291
347
var a0 = ps . Get < int > ( "ee" ) ;
292
348
Assert . AreEqual ( 200 , a0 ) ;
@@ -326,8 +382,8 @@ public void TestThread()
326
382
_ps . res = 0 ;
327
383
_ps . bb = 100 ;
328
384
_ps . th_cnt = 0 ;
329
- //add function to the scope
330
- //can be call many times, more efficient than ast
385
+ //add function to the scope
386
+ //can be call many times, more efficient than ast
331
387
ps . Exec (
332
388
"import threading\n " +
333
389
"lock = threading.Lock()\n " +
0 commit comments