Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb95acc3

Browse files
committed
add eval and exec
1 parent1d583c7 commitb95acc3

File tree

3 files changed

+121
-25
lines changed

3 files changed

+121
-25
lines changed

‎src/embed_tests/Python.EmbeddingTest.csproj

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<ProjectDefaultTargets="Build"xmlns="http://schemas.microsoft.com/developer/msbuild/2003"ToolsVersion="4.0">
33
<PropertyGroup>
44
<ConfigurationCondition=" '$(Configuration)' == ''">Debug</Configuration>
@@ -30,7 +30,8 @@
3030
<DebugType>full</DebugType>
3131
</PropertyGroup>
3232
<PropertyGroupCondition=" '$(Configuration)' == 'ReleaseMono'">
33-
<DefineConstantsCondition="'$(DefineConstants)' == ''"></DefineConstants>
33+
<DefineConstantsCondition="'$(DefineConstants)' == ''">
34+
</DefineConstants>
3435
<Optimize>true</Optimize>
3536
<DebugType>pdbonly</DebugType>
3637
</PropertyGroup>
@@ -40,7 +41,8 @@
4041
<DebugType>full</DebugType>
4142
</PropertyGroup>
4243
<PropertyGroupCondition=" '$(Configuration)' == 'ReleaseWin'">
43-
<DefineConstantsCondition="'$(DefineConstants)' == ''"></DefineConstants>
44+
<DefineConstantsCondition="'$(DefineConstants)' == ''">
45+
</DefineConstants>
4446
<Optimize>true</Optimize>
4547
<DebugType>pdbonly</DebugType>
4648
</PropertyGroup>
@@ -50,7 +52,8 @@
5052
<DebugType>full</DebugType>
5153
</PropertyGroup>
5254
<PropertyGroupCondition=" '$(Configuration)' == 'ReleaseMonoPY3'">
53-
<DefineConstantsCondition="'$(DefineConstants)' == ''"></DefineConstants>
55+
<DefineConstantsCondition="'$(DefineConstants)' == ''">
56+
</DefineConstants>
5457
<Optimize>true</Optimize>
5558
<DebugType>pdbonly</DebugType>
5659
</PropertyGroup>
@@ -60,7 +63,8 @@
6063
<DebugType>full</DebugType>
6164
</PropertyGroup>
6265
<PropertyGroupCondition=" '$(Configuration)' == 'ReleaseWinPY3'">
63-
<DefineConstantsCondition="'$(DefineConstants)' == ''"></DefineConstants>
66+
<DefineConstantsCondition="'$(DefineConstants)' == ''">
67+
</DefineConstants>
6468
<Optimize>true</Optimize>
6569
<DebugType>pdbonly</DebugType>
6670
</PropertyGroup>
@@ -82,6 +86,7 @@
8286
<CompileInclude="pyiter.cs" />
8387
<CompileInclude="pylong.cs" />
8488
<CompileInclude="pyobject.cs" />
89+
<CompileInclude="pyrunstring.cs" />
8590
<CompileInclude="pythonexception.cs" />
8691
<CompileInclude="pytuple.cs" />
8792
</ItemGroup>
@@ -103,4 +108,4 @@
103108
<CopySourceFiles="$(TargetAssembly)"DestinationFolder="$(PythonBuildDir)" />
104109
<CopySourceFiles="$(TargetAssemblyPdb)"Condition="Exists('$(TargetAssemblyPdb)')"DestinationFolder="$(PythonBuildDir)" />
105110
</Target>
106-
</Project>
111+
</Project>

‎src/embed_tests/pyrunstring.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
usingNUnit.Framework;
2+
usingPython.Runtime;
3+
usingSystem;
4+
usingSystem.Collections.Generic;
5+
usingSystem.Linq;
6+
usingSystem.Text;
7+
8+
namespacePython.EmbeddingTest
9+
{
10+
[TestFixture]
11+
publicclassRunStringTest
12+
{
13+
privatePy.GILStategil;
14+
15+
[SetUp]
16+
publicvoidSetUp()
17+
{
18+
gil=Py.GIL();
19+
}
20+
21+
[TearDown]
22+
publicvoidTearDown()
23+
{
24+
gil.Dispose();
25+
}
26+
27+
[Test]
28+
publicvoidTestRunSimpleString()
29+
{
30+
intaa=PythonEngine.RunSimpleString("import sys");
31+
Assert.AreEqual(aa,0);
32+
33+
intbb=PythonEngine.RunSimpleString("import 1234");
34+
Assert.AreEqual(bb,-1);
35+
}
36+
37+
[Test]
38+
publicvoidTestEval()
39+
{
40+
dynamicsys=Py.Import("sys");
41+
sys.attr1=100;
42+
PyDictlocals=newPyDict();
43+
locals.SetItem("sys",sys);
44+
locals.SetItem("a",newPyInt(10));
45+
46+
varb=PythonEngine.Eval("sys.attr1 + a + 1",null,locals.Handle)
47+
.AsManagedObject(typeof(Int32));
48+
Assert.AreEqual(b,111);
49+
}
50+
51+
[Test]
52+
publicvoidTestExec()
53+
{
54+
dynamicsys=Py.Import("sys");
55+
sys.attr1=100;
56+
PyDictlocals=newPyDict();
57+
locals.SetItem("sys",sys);
58+
locals.SetItem("a",newPyInt(10));
59+
60+
PythonEngine.Exec("c = sys.attr1 + a + 1",null,locals.Handle);
61+
varc=locals.GetItem("c").AsManagedObject(typeof(Int32));
62+
Assert.AreEqual(c,111);
63+
}
64+
}
65+
}

‎src/runtime/pythonengine.cs

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,7 @@ public static void Initialize(IEnumerable<string> args)
156156
stringcode=
157157
"import atexit, clr\n"+
158158
"atexit.register(clr._AtExit)\n";
159-
PyObjectr=PythonEngine.RunString(code);
160-
if(r!=null)
161-
{
162-
r.Dispose();
163-
}
159+
PythonEngine.Exec(code);
164160

165161
// Load the clr.py resource into the clr module
166162
IntPtrclr=Python.Runtime.ImportHook.GetCLRModule();
@@ -180,12 +176,7 @@ public static void Initialize(IEnumerable<string> args)
180176
{
181177
// add the contents of clr.py to the module
182178
stringclr_py=reader.ReadToEnd();
183-
PyObjectresult=RunString(clr_py,module_globals,locals.Handle);
184-
if(null==result)
185-
{
186-
thrownewPythonException();
187-
}
188-
result.Dispose();
179+
Exec(clr_py,module_globals,locals.Handle);
189180
}
190181

191182
// add the imported module to the clr module, and copy the API functions
@@ -253,11 +244,7 @@ public static void InitExt()
253244
" exec(line)\n"+
254245
" break\n";
255246

256-
PyObjectr=PythonEngine.RunString(code);
257-
if(r!=null)
258-
{
259-
r.Dispose();
260-
}
247+
PythonEngine.Exec(code);
261248
}
262249
catch(PythonExceptione)
263250
{
@@ -405,6 +392,38 @@ public static PyObject ModuleFromString(string name, string code)
405392
}
406393

407394

395+
/// <summary>
396+
/// Eval Method
397+
/// </summary>
398+
/// <remarks>
399+
/// Evaluate a Python expression and returns the result.
400+
/// It's a subset of Python eval function.
401+
/// </remarks>
402+
publicstaticPyObjectEval(stringcode,IntPtr?globals=null,IntPtr?locals=null)
403+
{
404+
PyObjectresult=RunString(code,globals,locals,RunFlagType.Eval);
405+
returnresult;
406+
}
407+
408+
409+
/// <summary>
410+
/// Exec Method
411+
/// </summary>
412+
/// <remarks>
413+
/// Run a string containing Python code.
414+
/// It's a subset of Python exec function.
415+
/// </remarks>
416+
publicstaticvoidExec(stringcode,IntPtr?globals=null,IntPtr?locals=null)
417+
{
418+
PyObjectresult=RunString(code,globals,locals,RunFlagType.File);
419+
if(result.obj!=Runtime.PyNone)
420+
{
421+
thrownewPythonException();
422+
}
423+
result.Dispose();
424+
}
425+
426+
408427
/// <summary>
409428
/// RunString Method
410429
/// </summary>
@@ -413,8 +432,8 @@ public static PyObject ModuleFromString(string name, string code)
413432
/// executing the code string as a PyObject instance, or null if
414433
/// an exception was raised.
415434
/// </remarks>
416-
publicstaticPyObjectRunString(
417-
stringcode,IntPtr?globals=null,IntPtr?locals=null
435+
internalstaticPyObjectRunString(
436+
stringcode,IntPtr?globals=null,IntPtr?locals=null,RunFlagType_flag=RunFlagType.File
418437
)
419438
{
420439
varborrowedGlobals=true;
@@ -439,7 +458,7 @@ public static PyObject RunString(
439458
borrowedLocals=false;
440459
}
441460

442-
varflag=(IntPtr)257;/* Py_file_input */
461+
varflag=(IntPtr)_flag;
443462

444463
try
445464
{
@@ -465,6 +484,13 @@ public static PyObject RunString(
465484
}
466485
}
467486

487+
publicenumRunFlagType
488+
{
489+
Single=256,
490+
File=257,/* Py_file_input */
491+
Eval=258
492+
}
493+
468494
publicstaticclassPy
469495
{
470496
publicstaticGILStateGIL()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp