-
Notifications
You must be signed in to change notification settings - Fork 774
Expand file tree
/
Copy pathpyrunstring.cs
More file actions
62 lines (53 loc) · 1.51 KB
/
pyrunstring.cs
File metadata and controls
62 lines (53 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using NUnit.Framework;
using Python.Runtime;
namespace Python.EmbeddingTest
{
public class RunStringTest
{
[Test]
public void TestRunSimpleString()
{
int aa = PythonEngine.RunSimpleString("import sys");
Assert.That(aa, Is.EqualTo(0));
int bb = PythonEngine.RunSimpleString("import 1234");
Assert.That(bb, Is.EqualTo(-1));
}
[Test]
public void TestEval()
{
dynamic sys = Py.Import("sys");
sys.attr1 = 100;
var locals = new PyDict();
locals.SetItem("sys", sys);
locals.SetItem("a", new PyInt(10));
object b = PythonEngine.Eval("sys.attr1 + a + 1", null, locals)
.AsManagedObject(typeof(int));
Assert.That(b, Is.EqualTo(111));
}
[Test]
public void TestExec()
{
dynamic sys = Py.Import("sys");
sys.attr1 = 100;
var locals = new PyDict();
locals.SetItem("sys", sys);
locals.SetItem("a", new PyInt(10));
PythonEngine.Exec("c = sys.attr1 + a + 1", null, locals);
object c = locals.GetItem("c").AsManagedObject(typeof(int));
Assert.That(c, Is.EqualTo(111));
}
[Test]
public void TestExec2()
{
string code = @"
class Test1():
pass
class Test2():
def __init__(self):
Test1()
Test2()";
PythonEngine.Exec(code);
}
}
}