@@ -137,7 +137,7 @@ public void TestScopeClass()
137
137
dynamic _ps = ps ;
138
138
_ps . bb = 100 ;
139
139
ps . Exec (
140
- "class class1 ():\n " +
140
+ "class Class1 ():\n " +
141
141
" def __init__(self, value):\n " +
142
142
" self.value = value\n " +
143
143
" def call(self, arg):\n " +
@@ -146,8 +146,8 @@ public void TestScopeClass()
146
146
" global bb\n " +
147
147
" bb = self.value + arg\n " //update scope variable
148
148
) ;
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 > ( ) ;
151
151
Assert . AreEqual ( 130 , result ) ;
152
152
153
153
obj1 . update ( 10 ) ;
@@ -170,7 +170,7 @@ public void TestImportModule()
170
170
171
171
ps . Exec ( "sys.attr1 = 2" ) ;
172
172
var value1 = ps . Eval < int > ( "sys.attr1" ) ;
173
- var value2 = ( int ) sys . attr1 . AsManagedObject ( typeof ( int ) ) ;
173
+ var value2 = ( int ) sys . attr1 . As < int > ( ) ;
174
174
Assert . AreEqual ( 2 , value1 ) ;
175
175
Assert . AreEqual ( 2 , value2 ) ;
176
176
@@ -192,9 +192,10 @@ public void TestImportScope()
192
192
ps . SetVariable ( "bb" , 100 ) ;
193
193
ps . SetVariable ( "cc" , 10 ) ;
194
194
195
- PyScope scope = ps . CreateScope ( ) ;
195
+ PyScope scope = Py . CreateScope ( ) ;
196
+ scope . ImportScope ( ps , "ps" ) ;
196
197
197
- scope . Exec ( "aa = bb + cc + 3" ) ;
198
+ scope . Exec ( "aa = ps. bb + ps. cc + 3" ) ;
198
199
var result = scope . GetVariable < int > ( "aa" ) ;
199
200
Assert . AreEqual ( 113 , result ) ;
200
201
@@ -204,6 +205,30 @@ public void TestImportScope()
204
205
}
205
206
}
206
207
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
+
207
232
/// <summary>
208
233
/// Create a scope and import variables from a scope,
209
234
/// call the function imported.
@@ -219,24 +244,24 @@ public void TestImportScopeFunction()
219
244
"def func1():\n " +
220
245
" return cc + bb\n " ) ;
221
246
222
- PyScope scope = ps . CreateScope ( ) ;
247
+ PyScope scope = ps . NewScope ( ) ;
223
248
224
249
//'func1' is imported from the origion scope
225
250
scope . Exec (
226
251
"def func2():\n " +
227
252
" return func1() - cc - bb\n " ) ;
228
253
dynamic func2 = scope . GetVariable ( "func2" ) ;
229
254
230
- var result1 = func2 ( ) . AsManagedObject ( typeof ( int ) ) ;
255
+ var result1 = func2 ( ) . As < int > ( ) ;
231
256
Assert . AreEqual ( 0 , result1 ) ;
232
257
233
258
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 > ( ) ;
235
260
Assert . AreEqual ( - 10 , result2 ) ;
236
261
scope . SetVariable ( "cc" , 10 ) ; //rollback
237
262
238
263
ps . SetVariable ( "cc" , 20 ) ;
239
- var result3 = func2 ( ) . AsManagedObject ( typeof ( int ) ) ;
264
+ var result3 = func2 ( ) . As < int > ( ) ;
240
265
Assert . AreEqual ( 10 , result3 ) ;
241
266
ps . SetVariable ( "cc" , 10 ) ; //rollback
242
267
@@ -256,7 +281,8 @@ public void TestImportScopeByName()
256
281
ps . SetVariable ( "bb" , 100 ) ;
257
282
258
283
var scope = Py . CreateScope ( ) ;
259
- scope . ImportScope ( "test" ) ;
284
+ scope . ImportAllFromScope ( "test" ) ;
285
+ //scope.ImportModule("test");
260
286
261
287
Assert . IsTrue ( scope . ContainsVariable ( "bb" ) ) ;
262
288
}
0 commit comments