@@ -44,15 +44,16 @@ internal PyScope(string name)
44
44
InitVariables ( ) ;
45
45
}
46
46
47
- //To compitable with the python module
47
+ //To compitable with the python module.
48
+ //Enable locals() and glocals() usage in the python script.
48
49
private void InitVariables ( )
49
50
{
50
51
SetVariable ( "locals" , ( Func < PyDict > ) Variables ) ;
51
52
SetVariable ( "globals" , ( Func < PyDict > ) Variables ) ;
52
53
}
53
54
54
55
/// <summary>
55
- /// the dict for local variables
56
+ /// the dict for scope variables
56
57
/// </summary>
57
58
internal IntPtr variables { get ; private set ; }
58
59
@@ -96,23 +97,23 @@ public void ImportScope(PyScope scope)
96
97
}
97
98
98
99
/// <summary>
99
- /// Import Method
100
+ /// ImportModule Method
100
101
/// </summary>
101
102
/// <remarks>
102
103
/// The import .. as .. statement in Python.
103
- /// Import a module ,add it to the local variable dict and return the resulting module object as a PyObject.
104
+ /// Import a module ,add it to the variables dict and return the resulting module object as a PyObject.
104
105
/// </remarks>
105
106
public PyObject ImportModule ( string name )
106
107
{
107
108
return ImportModule ( name , name ) ;
108
109
}
109
110
110
111
/// <summary>
111
- /// Import Method
112
+ /// ImportModule Method
112
113
/// </summary>
113
114
/// <remarks>
114
115
/// The import .. as .. statement in Python.
115
- /// Import a module ,add it to the local variable dict and return the resulting module object as a PyObject.
116
+ /// Import a module ,add it to the variables dict and return the resulting module object as a PyObject.
116
117
/// </remarks>
117
118
public PyObject ImportModule ( string name , string asname )
118
119
{
@@ -126,6 +127,13 @@ public PyObject ImportModule(string name, string asname)
126
127
return module ;
127
128
}
128
129
130
+ /// <summary>
131
+ /// Execute method
132
+ /// </summary>
133
+ /// <remarks>
134
+ /// Execute a Python ast and return the result as a PyObject.
135
+ /// The ast can be either an expression or stmts.
136
+ /// </remarks>
129
137
public PyObject Execute ( PyObject script )
130
138
{
131
139
Check ( ) ;
@@ -156,9 +164,9 @@ public T ExecuteVariable<T>(string name)
156
164
PyObject script = GetVariable ( name ) ;
157
165
return Execute < T > ( script ) ;
158
166
}
159
-
167
+
160
168
/// <summary>
161
- /// Evaluate a Python expression
169
+ /// Eval method
162
170
/// </summary>
163
171
/// <remarks>
164
172
/// Evaluate a Python expression and return the result as a PyObject
@@ -193,7 +201,7 @@ public T Eval<T>(string code)
193
201
/// Exec Method
194
202
/// </summary>
195
203
/// <remarks>
196
- /// Evaluate a Python script and save its local variables in the current local variable dict.
204
+ /// Exec a Python script and save its local variables in the current local variable dict.
197
205
/// </remarks>
198
206
public void Exec ( string code )
199
207
{
@@ -214,13 +222,13 @@ private void Exec(string code, IntPtr _globals, IntPtr _locals)
214
222
}
215
223
Runtime . XDecref ( ptr ) ;
216
224
}
217
-
225
+
218
226
/// <summary>
219
- /// SetLocal Method
227
+ /// SetVariable Method
220
228
/// </summary>
221
229
/// <remarks>
222
- /// Add a new variable to local variable dict if it not exists
223
- /// or set the value of the local variable if it exists.
230
+ /// Add a new variable to the variables dict if it not exists
231
+ /// or update its value if the variable exists.
224
232
/// </remarks>
225
233
public void SetVariable ( string name , object value )
226
234
{
@@ -238,10 +246,10 @@ public void SetVariable(string name, object value)
238
246
}
239
247
240
248
/// <summary>
241
- /// DelLocal Method
249
+ /// RemoveVariable Method
242
250
/// </summary>
243
251
/// <remarks>
244
- /// Remove a variable from the local variable dict.
252
+ /// Remove a variable from the variables dict.
245
253
/// </remarks>
246
254
public void RemoveVariable ( string name )
247
255
{
@@ -257,10 +265,10 @@ public void RemoveVariable(string name)
257
265
}
258
266
259
267
/// <summary>
260
- /// Exists Method
268
+ /// ContainsVariable Method
261
269
/// </summary>
262
270
/// <remarks>
263
- /// Returns true if the variable appears in the local variable dict or the global variable dict.
271
+ /// Returns true if the variable exists in the variables dict.
264
272
/// </remarks>
265
273
public bool ContainsVariable ( string name )
266
274
{
@@ -270,13 +278,13 @@ public bool ContainsVariable(string name)
270
278
return Runtime . PyMapping_HasKey ( variables , pyKey . obj ) != 0 ;
271
279
}
272
280
}
273
-
281
+
274
282
/// <summary>
275
283
/// GetVariable Method
276
284
/// </summary>
277
285
/// <remarks>
278
286
/// Returns the value of the variable, local variable first.
279
- /// If the variable is not exists, return null .
287
+ /// If the variable is not exists, throw an Exception .
280
288
/// </remarks>
281
289
public PyObject GetVariable ( string name )
282
290
{
@@ -304,7 +312,7 @@ public PyObject GetVariable(string name)
304
312
}
305
313
306
314
/// <summary>
307
- /// GetVariable Method
315
+ /// TryGetVariable Method
308
316
/// </summary>
309
317
/// <remarks>
310
318
/// Returns the value of the variable, local variable first.
@@ -380,7 +388,6 @@ public override bool TrySetMember(SetMemberBinder binder, object value)
380
388
return true ;
381
389
}
382
390
383
- // Currently, AsManagedObject method cannot accept 'dynamic' for the T parameter
384
391
private object ToManagedObject < T > ( PyObject pyObj )
385
392
{
386
393
if ( typeof ( T ) == typeof ( PyObject ) || typeof ( T ) == typeof ( object ) )
0 commit comments