@@ -137,7 +137,7 @@ public void TestScopeClass()
137137 dynamic _ps = ps ;
138138 _ps . bb = 100 ;
139139 ps . Exec (
140- "class class1 ():\n " +
140+ "class Class1 ():\n " +
141141 " def __init__(self, value):\n " +
142142 " self.value = value\n " +
143143 " def call(self, arg):\n " +
@@ -146,8 +146,8 @@ public void TestScopeClass()
146146 " global bb\n " +
147147 " bb = self.value + arg\n " //update scope variable
148148 ) ;
149- dynamic obj1 = _ps . class1 ( 20 ) ;
150- var result = obj1 . call ( 10 ) . AsManagedObject ( typeof ( int ) ) ;
149+ dynamic obj1 = _ps . Class1 ( 20 ) ;
150+ var result = obj1 . call ( 10 ) . As < int > ( ) ;
151151 Assert . AreEqual ( 130 , result ) ;
152152
153153 obj1 . update ( 10 ) ;
@@ -170,7 +170,7 @@ public void TestImportModule()
170170
171171 ps . Exec ( "sys.attr1 = 2" ) ;
172172 var value1 = ps . Eval < int > ( "sys.attr1" ) ;
173- var value2 = ( int ) sys . attr1 . AsManagedObject ( typeof ( int ) ) ;
173+ var value2 = ( int ) sys . attr1 . As < int > ( ) ;
174174 Assert . AreEqual ( 2 , value1 ) ;
175175 Assert . AreEqual ( 2 , value2 ) ;
176176
@@ -192,9 +192,10 @@ public void TestImportScope()
192192 ps . SetVariable ( "bb" , 100 ) ;
193193 ps . SetVariable ( "cc" , 10 ) ;
194194
195- PyScope scope = ps . CreateScope ( ) ;
195+ PyScope scope = Py . CreateScope ( ) ;
196+ scope . ImportScope ( ps , "ps" ) ;
196197
197- scope . Exec ( "aa = bb + cc + 3" ) ;
198+ scope . Exec ( "aa = ps. bb + ps. cc + 3" ) ;
198199 var result = scope . GetVariable < int > ( "aa" ) ;
199200 Assert . AreEqual ( 113 , result ) ;
200201
@@ -204,6 +205,30 @@ public void TestImportScope()
204205 }
205206 }
206207
208+ /// <summary>
209+ /// Create a scope and import variables from a scope,
210+ /// exec Python statements in the scope then discard it.
211+ /// </summary>
212+ [ Test ]
213+ public void TestImportAllFromScope ( )
214+ {
215+ using ( Py . GIL ( ) )
216+ {
217+ ps . SetVariable ( "bb" , 100 ) ;
218+ ps . SetVariable ( "cc" , 10 ) ;
219+
220+ PyScope scope = ps . NewScope ( ) ;
221+
222+ scope . Exec ( "aa = bb + cc + 3" ) ;
223+ var result = scope . GetVariable < int > ( "aa" ) ;
224+ Assert . AreEqual ( 113 , result ) ;
225+
226+ scope . Dispose ( ) ;
227+
228+ Assert . IsFalse ( ps . ContainsVariable ( "aa" ) ) ;
229+ }
230+ }
231+
207232 /// <summary>
208233 /// Create a scope and import variables from a scope,
209234 /// call the function imported.
@@ -219,24 +244,24 @@ public void TestImportScopeFunction()
219244 "def func1():\n " +
220245 " return cc + bb\n " ) ;
221246
222- PyScope scope = ps . CreateScope ( ) ;
247+ PyScope scope = ps . NewScope ( ) ;
223248
224249 //'func1' is imported from the origion scope
225250 scope . Exec (
226251 "def func2():\n " +
227252 " return func1() - cc - bb\n " ) ;
228253 dynamic func2 = scope . GetVariable ( "func2" ) ;
229254
230- var result1 = func2 ( ) . AsManagedObject ( typeof ( int ) ) ;
255+ var result1 = func2 ( ) . As < int > ( ) ;
231256 Assert . AreEqual ( 0 , result1 ) ;
232257
233258 scope . SetVariable ( "cc" , 20 ) ; //it has no effect on the globals of 'func1'
234- var result2 = func2 ( ) . AsManagedObject ( typeof ( int ) ) ;
259+ var result2 = func2 ( ) . As < int > ( ) ;
235260 Assert . AreEqual ( - 10 , result2 ) ;
236261 scope . SetVariable ( "cc" , 10 ) ; //rollback
237262
238263 ps . SetVariable ( "cc" , 20 ) ;
239- var result3 = func2 ( ) . AsManagedObject ( typeof ( int ) ) ;
264+ var result3 = func2 ( ) . As < int > ( ) ;
240265 Assert . AreEqual ( 10 , result3 ) ;
241266 ps . SetVariable ( "cc" , 10 ) ; //rollback
242267
@@ -256,7 +281,8 @@ public void TestImportScopeByName()
256281 ps . SetVariable ( "bb" , 100 ) ;
257282
258283 var scope = Py . CreateScope ( ) ;
259- scope . ImportScope ( "test" ) ;
284+ scope . ImportAllFromScope ( "test" ) ;
285+ //scope.ImportModule("test");
260286
261287 Assert . IsTrue ( scope . ContainsVariable ( "bb" ) ) ;
262288 }
0 commit comments