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

Skip to content

Commit efd3798

Browse files
committed
Rename several methods and add three methods
Referring to IronPython, change the name of the methods Get, Exists, SetLocal, DelLocal to GetVariable, ContainsVariable, SetVariable, RemoveVariable. Hidden the methods SetGlobalVariable, RemoveGlobalVariable. Add a new method 'Compile' to compile string into ast, the ast can be seen as the ScriptSource of IronPython. Add two new methods 'Execute' and 'Execute<T>' to execute an ast and obtain the result, corresponding to the 'Execute' method of IronPython.
1 parent 72003aa commit efd3798

File tree

3 files changed

+105
-26
lines changed

3 files changed

+105
-26
lines changed

src/embed_tests/pyscope.cs

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void TearDown()
2626
[Test]
2727
public void TestEval()
2828
{
29-
ps.SetLocal("a", 1);
29+
ps.SetVariable("a", 1);
3030
int result = ps.Eval<int>("a+2");
3131
Assert.AreEqual(result, 3);
3232
}
@@ -37,10 +37,40 @@ public void TestEval()
3737
[Test]
3838
public void TestExec()
3939
{
40-
ps.SetGlobal("bb", 100); //declare a global variable
41-
ps.SetLocal("cc", 10); //declare a local variable
40+
ps.SetVariable("bb", 100); //declare a global variable
41+
ps.SetVariable("cc", 10); //declare a local variable
4242
ps.Exec("aa=bb+cc+3");
43-
int result = ps.Get<System.Int32>("aa");
43+
int result = ps.GetVariable<System.Int32>("aa");
44+
Assert.AreEqual(result, 113);
45+
}
46+
47+
/// <summary>
48+
/// Compile an expression into an ast object;
49+
/// Execute the ast and obtain its return value.
50+
/// </summary>
51+
[Test]
52+
public void TestCompileExpression()
53+
{
54+
ps.SetVariable("bb", 100); //declare a global variable
55+
ps.SetVariable("cc", 10); //declare a local variable
56+
var script = ps.Compile("bb+cc+3", "", CompileMode.Eval);
57+
var result = ps.Execute<int>(script);
58+
Assert.AreEqual(result, 113);
59+
}
60+
61+
/// <summary>
62+
/// Compile Python statements into an ast object;
63+
/// Execute the ast;
64+
/// Obtain the local variables created.
65+
/// </summary>
66+
[Test]
67+
public void TestCompileStatements()
68+
{
69+
ps.SetVariable("bb", 100); //declare a global variable
70+
ps.SetVariable("cc", 10); //declare a local variable
71+
var script = ps.Compile("aa=bb+cc+3", "", CompileMode.File);
72+
ps.Execute(script);
73+
int result = ps.GetVariable<int>("aa");
4474
Assert.AreEqual(result, 113);
4575
}
4676

@@ -50,16 +80,16 @@ public void TestExec()
5080
[Test]
5181
public void TestSubScope()
5282
{
53-
ps.SetGlobal("bb", 100); //declare a global variable
54-
ps.SetLocal("cc", 10); //declare a local variable
83+
ps.SetVariable("bb", 100); //declare a global variable
84+
ps.SetVariable("cc", 10); //declare a local variable
5585

5686
PyScope scope = ps.SubScope();
5787
scope.Exec("aa=bb+cc+3");
58-
int result = scope.Get<System.Int32>("aa");
88+
int result = scope.GetVariable<System.Int32>("aa");
5989
Assert.AreEqual(result, 113); //
6090
scope.Dispose();
6191

62-
Assert.IsFalse(ps.Exists("aa"));
92+
Assert.IsFalse(ps.ContainsVariable("aa"));
6393
}
6494

6595
/// <summary>
@@ -70,7 +100,7 @@ public void TestSubScope()
70100
public void TestImport()
71101
{
72102
dynamic sys = ps.Import("sys");
73-
Assert.IsTrue(ps.Exists("sys"));
103+
Assert.IsTrue(ps.ContainsVariable("sys"));
74104

75105
ps.Exec("sys.attr1 = 2");
76106
int value1 = ps.Eval<int>("sys.attr1");
@@ -87,7 +117,7 @@ public void TestImport()
87117
public void TestImportAs()
88118
{
89119
ps.ImportAs("sys", "sys1");
90-
Assert.IsTrue(ps.Exists("sys1"));
120+
Assert.IsTrue(ps.ContainsVariable("sys1"));
91121
}
92122

93123
/// <summary>
@@ -96,8 +126,8 @@ public void TestImportAs()
96126
[Test]
97127
public void TestSuspend()
98128
{
99-
ps.SetGlobal("bb", 100);
100-
ps.SetLocal("cc", 10);
129+
ps.SetVariable("bb", 100);
130+
ps.SetVariable("cc", 10);
101131
ps.Suspend();
102132

103133
using (Py.GIL())
@@ -106,7 +136,7 @@ public void TestSuspend()
106136
}
107137

108138
ps.Exec("aa=bb+cc+3");
109-
int result = ps.Get<System.Int32>("aa");
139+
int result = ps.GetVariable<System.Int32>("aa");
110140
Assert.AreEqual(result, 113);
111141
}
112142
}

src/runtime/pythonengine.cs

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,13 @@ public class PySessionDisposedException: Exception
532532

533533
}
534534

535+
public enum CompileMode
536+
{
537+
Single = 256,
538+
File = 257,
539+
Eval = 258
540+
}
541+
535542
public class PyScope : IDisposable
536543
{
537544
public class GILState : IDisposable
@@ -691,10 +698,47 @@ public PyObject ImportAs(string name, string asname)
691698
{
692699
asname = name;
693700
}
694-
SetLocal(asname, module);
701+
SetVariable(asname, module);
695702
return module;
696703
}
697704

705+
public PyObject Execute(PyObject script)
706+
{
707+
IntPtr ptr = Runtime.PyEval_EvalCode(script.Handle, globals, locals);
708+
Py.Throw();
709+
if(ptr == Runtime.PyNone)
710+
{
711+
Runtime.XDecref(ptr);
712+
return null;
713+
}
714+
return new PyObject(ptr);
715+
}
716+
717+
public T Execute<T>(PyObject script)
718+
{
719+
var pyObj = this.Execute(script);
720+
if(pyObj == null)
721+
{
722+
return default(T);
723+
}
724+
T obj = (T)pyObj.AsManagedObject(typeof(T));
725+
return obj;
726+
}
727+
728+
/// <summary>
729+
/// Compile Method
730+
/// </summary>
731+
/// <remarks>
732+
/// Compile Python expression/statements into ast.
733+
/// </remarks>
734+
public PyObject Compile(string code, string filename = "", CompileMode mode = CompileMode.File)
735+
{
736+
IntPtr flag = (IntPtr)mode;
737+
IntPtr ptr = Runtime.Py_CompileString(code, filename, flag);
738+
Py.Throw();
739+
return new PyObject(ptr);
740+
}
741+
698742
/// <summary>
699743
/// Evaluate a Python expression
700744
/// </summary>
@@ -751,15 +795,15 @@ private void Exec(string code, IntPtr _globals, IntPtr _locals)
751795
}
752796
Runtime.XDecref(ptr);
753797
}
754-
798+
755799
/// <summary>
756-
/// SetGlobal Method
800+
/// SetGlobalVariable Method
757801
/// </summary>
758802
/// <remarks>
759803
/// Add a new variable to global variable dict if it not exists
760804
/// or set the value of the global variable if it exists.
761805
/// </remarks>
762-
public void SetGlobal(string name, object value)
806+
internal void SetGlobalVariable(string name, object value)
763807
{
764808
this.AcquireLock();
765809
using (var pyKey = new PyString(name))
@@ -775,12 +819,12 @@ public void SetGlobal(string name, object value)
775819
}
776820

777821
/// <summary>
778-
/// DelGlobal Method
822+
/// RemoveGlobalVariable Method
779823
/// </summary>
780824
/// <remarks>
781825
/// Remove a variable from the global variable dict.
782826
/// </remarks>
783-
public void DelGlobal(string name)
827+
internal void RemoveGlobalVariable(string name)
784828
{
785829
this.AcquireLock();
786830
using (var pyKey = new PyString(name))
@@ -800,7 +844,7 @@ public void DelGlobal(string name)
800844
/// Add a new variable to local variable dict if it not exists
801845
/// or set the value of the local variable if it exists.
802846
/// </remarks>
803-
public void SetLocal(string name, object value)
847+
public void SetVariable(string name, object value)
804848
{
805849
this.AcquireLock();
806850
using (var pyKey = new PyString(name))
@@ -821,7 +865,7 @@ public void SetLocal(string name, object value)
821865
/// <remarks>
822866
/// Remove a variable from the local variable dict.
823867
/// </remarks>
824-
public void DelLocal(string name)
868+
public void RemoveVariable(string name)
825869
{
826870
this.AcquireLock();
827871
using (var pyKey = new PyString(name))
@@ -840,7 +884,7 @@ public void DelLocal(string name)
840884
/// <remarks>
841885
/// Returns true if the variable appears in the local variable dict or the global variable dict.
842886
/// </remarks>
843-
public bool Exists(string name)
887+
public bool ContainsVariable(string name)
844888
{
845889
this.AcquireLock();
846890
using (var pyKey = new PyString(name))
@@ -860,7 +904,7 @@ public bool Exists(string name)
860904
/// Returns the value of the variable, local variable first.
861905
/// If the variable is not exists, return null.
862906
/// </remarks>
863-
public PyObject Get(string name)
907+
public PyObject GetVariable(string name)
864908
{
865909
this.AcquireLock();
866910
using (var pyKey = new PyString(name))
@@ -886,9 +930,9 @@ public PyObject Get(string name)
886930
}
887931
}
888932

889-
public T Get<T>(string name)
933+
public T GetVariable<T>(string name)
890934
{
891-
PyObject obj = this.Get(name);
935+
PyObject obj = this.GetVariable(name);
892936
return (T)obj.AsManagedObject(typeof(T));
893937
}
894938

src/runtime/runtime.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,12 @@ public static extern int Py_Main(
707707
[DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)]
708708
internal static extern IntPtr PyRun_String(string code, IntPtr st, IntPtr globals, IntPtr locals);
709709

710-
[DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)]
710+
[DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl,
711+
ExactSpelling = true, CharSet = CharSet.Ansi)]
712+
internal unsafe static extern IntPtr
713+
PyEval_EvalCode(IntPtr co, IntPtr globals, IntPtr locals);
714+
715+
[DllImport(PythonDll)]
711716
internal static extern IntPtr Py_CompileString(string code, string file, IntPtr tok);
712717

713718
[DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)]

0 commit comments

Comments
 (0)