@@ -51,7 +51,7 @@ public void TestEval()
5151 ps . Set ( "a" , 1 ) ;
5252 var result = ps . Eval < int > ( "a + 2" ) ;
5353 Assert . AreEqual ( 3 , result ) ;
54- }
54+ }
5555 }
5656
5757 /// <summary>
@@ -169,6 +169,62 @@ public void TestScopeClass()
169169 }
170170 }
171171
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+
172228 /// <summary>
173229 /// Import a python module into the session.
174230 /// Equivalent to the Python "import" statement.
@@ -194,7 +250,7 @@ public void TestImportModule()
194250 }
195251
196252 /// <summary>
197- /// Create a scope and import variables from a scope,
253+ /// Create a scope and import variables from a scope,
198254 /// exec Python statements in the scope then discard it.
199255 /// </summary>
200256 [ Test ]
@@ -218,7 +274,7 @@ public void TestImportScope()
218274 }
219275
220276 /// <summary>
221- /// Create a scope and import variables from a scope,
277+ /// Create a scope and import variables from a scope,
222278 /// exec Python statements in the scope then discard it.
223279 /// </summary>
224280 [ Test ]
@@ -241,7 +297,7 @@ public void TestImportAllFromScope()
241297 }
242298
243299 /// <summary>
244- /// Create a scope and import variables from a scope,
300+ /// Create a scope and import variables from a scope,
245301 /// call the function imported.
246302 /// </summary>
247303 [ Test ]
@@ -286,7 +342,7 @@ public void TestImportScopeFunction()
286342 public void TestVariables ( )
287343 {
288344 using ( Py . GIL ( ) )
289- {
345+ {
290346 ( ps . Variables ( ) as dynamic ) [ "ee" ] = new PyInt ( 200 ) ;
291347 var a0 = ps . Get < int > ( "ee" ) ;
292348 Assert . AreEqual ( 200 , a0 ) ;
@@ -326,8 +382,8 @@ public void TestThread()
326382 _ps . res = 0 ;
327383 _ps . bb = 100 ;
328384 _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
331387 ps . Exec (
332388 "import threading\n " +
333389 "lock = threading.Lock()\n " +
0 commit comments