Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dd492f4

Browse files
committed
add unit test for Variables() method
1 parent 2e063c2 commit dd492f4

File tree

2 files changed

+53
-21
lines changed

2 files changed

+53
-21
lines changed

src/embed_tests/TestPyScope.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,31 @@ public void TestImportScopeByName()
262262
}
263263
}
264264

265+
/// <summary>
266+
/// Use the locals() and globals() method just like in python module
267+
/// </summary>
268+
[Test]
269+
public void TestVariables()
270+
{
271+
(ps.Variables() as dynamic)["ee"] = new PyInt(200);
272+
var a0 = ps.GetVariable<int>("ee");
273+
Assert.AreEqual(200, a0);
274+
275+
ps.Exec("locals()['ee'] = 210");
276+
var a1 = ps.GetVariable<int>("ee");
277+
Assert.AreEqual(210, a1);
278+
279+
ps.Exec("globals()['ee'] = 220");
280+
var a2 = ps.GetVariable<int>("ee");
281+
Assert.AreEqual(220, a2);
282+
283+
var item = (ps as dynamic).locals();
284+
item["ee"] = new PyInt(230);
285+
item.Dispose();
286+
var a3 = ps.GetVariable<int>("ee");
287+
Assert.AreEqual(230, a3);
288+
}
289+
265290
/// <summary>
266291
/// Share a pyscope by multiple threads.
267292
/// </summary>

src/runtime/pyscope.cs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@ internal PyScope(string name)
4444
InitVariables();
4545
}
4646

47-
//To compitable with the python module
47+
//To compitable with the python module.
48+
//Enable locals() and glocals() usage in the python script.
4849
private void InitVariables()
4950
{
5051
SetVariable("locals", (Func<PyDict>)Variables);
5152
SetVariable("globals", (Func<PyDict>)Variables);
5253
}
5354

5455
/// <summary>
55-
/// the dict for local variables
56+
/// the dict for scope variables
5657
/// </summary>
5758
internal IntPtr variables { get; private set; }
5859

@@ -96,23 +97,23 @@ public void ImportScope(PyScope scope)
9697
}
9798

9899
/// <summary>
99-
/// Import Method
100+
/// ImportModule Method
100101
/// </summary>
101102
/// <remarks>
102103
/// 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.
104105
/// </remarks>
105106
public PyObject ImportModule(string name)
106107
{
107108
return ImportModule(name, name);
108109
}
109110

110111
/// <summary>
111-
/// Import Method
112+
/// ImportModule Method
112113
/// </summary>
113114
/// <remarks>
114115
/// 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.
116117
/// </remarks>
117118
public PyObject ImportModule(string name, string asname)
118119
{
@@ -126,6 +127,13 @@ public PyObject ImportModule(string name, string asname)
126127
return module;
127128
}
128129

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>
129137
public PyObject Execute(PyObject script)
130138
{
131139
Check();
@@ -156,9 +164,9 @@ public T ExecuteVariable<T>(string name)
156164
PyObject script = GetVariable(name);
157165
return Execute<T>(script);
158166
}
159-
167+
160168
/// <summary>
161-
/// Evaluate a Python expression
169+
/// Eval method
162170
/// </summary>
163171
/// <remarks>
164172
/// Evaluate a Python expression and return the result as a PyObject
@@ -193,7 +201,7 @@ public T Eval<T>(string code)
193201
/// Exec Method
194202
/// </summary>
195203
/// <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.
197205
/// </remarks>
198206
public void Exec(string code)
199207
{
@@ -214,13 +222,13 @@ private void Exec(string code, IntPtr _globals, IntPtr _locals)
214222
}
215223
Runtime.XDecref(ptr);
216224
}
217-
225+
218226
/// <summary>
219-
/// SetLocal Method
227+
/// SetVariable Method
220228
/// </summary>
221229
/// <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.
224232
/// </remarks>
225233
public void SetVariable(string name, object value)
226234
{
@@ -238,10 +246,10 @@ public void SetVariable(string name, object value)
238246
}
239247

240248
/// <summary>
241-
/// DelLocal Method
249+
/// RemoveVariable Method
242250
/// </summary>
243251
/// <remarks>
244-
/// Remove a variable from the local variable dict.
252+
/// Remove a variable from the variables dict.
245253
/// </remarks>
246254
public void RemoveVariable(string name)
247255
{
@@ -257,10 +265,10 @@ public void RemoveVariable(string name)
257265
}
258266

259267
/// <summary>
260-
/// Exists Method
268+
/// ContainsVariable Method
261269
/// </summary>
262270
/// <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.
264272
/// </remarks>
265273
public bool ContainsVariable(string name)
266274
{
@@ -270,13 +278,13 @@ public bool ContainsVariable(string name)
270278
return Runtime.PyMapping_HasKey(variables, pyKey.obj) != 0;
271279
}
272280
}
273-
281+
274282
/// <summary>
275283
/// GetVariable Method
276284
/// </summary>
277285
/// <remarks>
278286
/// 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.
280288
/// </remarks>
281289
public PyObject GetVariable(string name)
282290
{
@@ -304,7 +312,7 @@ public PyObject GetVariable(string name)
304312
}
305313

306314
/// <summary>
307-
/// GetVariable Method
315+
/// TryGetVariable Method
308316
/// </summary>
309317
/// <remarks>
310318
/// Returns the value of the variable, local variable first.
@@ -380,7 +388,6 @@ public override bool TrySetMember(SetMemberBinder binder, object value)
380388
return true;
381389
}
382390

383-
// Currently, AsManagedObject method cannot accept 'dynamic' for the T parameter
384391
private object ToManagedObject<T>(PyObject pyObj)
385392
{
386393
if(typeof(T) == typeof(PyObject) || typeof(T) == typeof(object))

0 commit comments

Comments
 (0)