8000 fixup! fixup! fixup! add a scope class to manage the context of inter… · pythonnet/pythonnet@354fdd4 · GitHub
[go: up one dir, main page]

Skip to content
65F2

Commit 354fdd4

Browse files
committed
fixup! fixup! fixup! add a scope class to manage the context of interaction with Python and simplify the variable exchanging
1 parent b997a08 commit 354fdd4

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/embed_tests/pyscope.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void TestImport()
7373
Assert.IsTrue(ps.Exists("sys"));
7474

7575
ps.Exec("sys.attr1 = 2");
76-
int value1 = (int)ps.Eval("sys.attr1").AsManagedObject(typeof(int));
76+
int value1 = ps.Eval<int>("sys.attr1");
7777
int value2 = (int)sys.attr1.AsManagedObject(typeof(int));
7878
Assert.AreEqual(value1, 2);
7979
Assert.AreEqual(value2, 2);

src/runtime/pythonengine.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,19 @@ public PyObject Eval(string code)
651651
return new PyObject(ptr);
652652
}
653653

654+
/// <summary>
655+
/// Evaluate a Python expression
656+
/// </summary>
657+
/// <remarks>
658+
/// Evaluate a Python expression and convert the result to Managed Object.
659+
/// </remarks>
660+
public T Eval<T>(string code)
661+
{
662+
PyObject pyObj = Eval(code);
663+
T obj = (T)pyObj.AsManagedObject(typeof(T));
664+
return obj;
665+
}
666+
654667
/// <summary>
655668
/// Exec Method
656669
/// </summary>

0 commit comments

Comments
 (0)
0