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

Skip to content

Commit 0125d29

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 3305fa0 commit 0125d29

File tree

3 files changed

+104
-25
lines changed

3 files changed

+104
-25
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
@@ -496,6 +496,13 @@ public class PySessionDisposedException: Exception
496496

497497
}
498498

499+
public enum CompileMode
500+
{
501+
Single = 256,
502+
File = 257,
503+
Eval = 258
504+
}
505+
499506
public class PyScope : IDisposable
500507
{
501508
public class GILState : IDisposable
@@ -655,10 +662,47 @@ public PyObject ImportAs(string name, string asname)
655662
{
656663
asname = name;
657664
}
658-
SetLocal(asname, module);
665+
SetVariable(asname, module);
659666
return module;
660667
}
661668

669+
public PyObject Execute(PyObject script)
670+
{
671+
IntPtr ptr = Runtime.PyEval_EvalCode(script.Handle, globals, locals);
672+
Py.Throw();
673+
if(ptr == Runtime.PyNone)
674+
{
675+
Runtime.XDecref(ptr);
676+
return null;
677+
}
678+
return new PyObject(ptr);
679+
}
680+
681+
public T Execute<T>(PyObject script)
682+
{
683+
var pyObj = this.Execute(script);
684+
if(pyObj == null)
685+
{
686+
return default(T);
687+
}
688+
T obj = (T)pyObj.AsManagedObject(typeof(T));
689+
return obj;
690+
}
691+
692+
/// <summary>
693+
/// Compile Method
694+
/// </summary>
695+
/// <remarks>
696+
/// Compile Python expression/statements into ast.
697+
/// </remarks>
698+
public PyObject Compile(string code, string filename = "", CompileMode mode = CompileMode.File)
699+
{
700+
IntPtr flag = (IntPtr)mode;
701+
IntPtr ptr = Runtime.Py_CompileString(code, filename, flag);
702+
Py.Throw();
703+
return new PyObject(ptr);
704+
}
705+
662706
/// <summary>
663707
/// Evaluate a Python expression
664708
/// </summary>
@@ -715,15 +759,15 @@ private void Exec(string code, IntPtr _globals, IntPtr _locals)
715759
}
716760
Runtime.XDecref(ptr);
717761
}
718-
762+
719763
/// <summary>
720-
/// SetGlobal Method
764+
/// SetGlobalVariable Method
721765
/// </summary>
722766
/// <remarks>
723767
/// Add a new variable to global variable dict if it not exists
724768
/// or set the value of the global variable if it exists.
725769
/// </remarks>
726-
public void SetGlobal(string name, object value)
770+
internal void SetGlobalVariable(string name, object value)
727771
{
728772
this.AcquireLock();
729773
using (var pyKey = new PyString(name))
@@ -739,12 +783,12 @@ public void SetGlobal(string name, object value)
739783
}
740784

741785
/// <summary>
742-
/// DelGlobal Method
786+
/// RemoveGlobalVariable Method
743787
/// </summary>
744788
/// <remarks>
745789
/// Remove a variable from the global variable dict.
746790
/// </remarks>
747-
public void DelGlobal(string name)
791+
internal void RemoveGlobalVariable(string name)
748792
{
749793
this.AcquireLock();
750794
using (var pyKey = new PyString(name))
@@ -764,7 +808,7 @@ public void DelGlobal(string name)
764808
/// Add a new variable to local variable dict if it not exists
765809
/// or set the value of the local variable if it exists.
766810
/// </remarks>
767-
public void SetLocal(string name, object value)
811+
public void SetVariable(string name, object value)
768812
{
769813
this.AcquireLock();
770814
using (var pyKey = new PyString(name))
@@ -785,7 +829,7 @@ public void SetLocal(string name, object value)
785829
/// <remarks>
786830
/// Remove a variable from the local variable dict.
787831
/// </remarks>
788-
public void DelLocal(string name)
832+
public void RemoveVariable(string name)
789833
{
790834
this.AcquireLock();
791835
using (var pyKey = new PyString(name))
@@ -804,7 +848,7 @@ public void DelLocal(string name)
804848
/// <remarks>
805849
/// Returns true if the variable appears in the local variable dict or the global variable dict.
806850
/// </remarks>
807-
public bool Exists(string name)
851+
public bool ContainsVariable(string name)
808852
{
809853
this.AcquireLock();
810854
using (var pyKey = new PyString(name))
@@ -824,7 +868,7 @@ public bool Exists(string name)
824868
/// Returns the value of the variable, local variable first.
825869
/// If the variable is not exists, return null.
826870
/// </remarks>
827-
public PyObject Get(string name)
871+
public PyObject GetVariable(string name)
828872
{
829873
this.AcquireLock();
830874
using (var pyKey = new PyString(name))
@@ -850,9 +894,9 @@ public PyObject Get(string name)
850894
}
851895
}
852896

853-
public T Get<T>(string name)
897+
public T GetVariable<T>(string name)
854898
{
855-
PyObject obj = this.Get(name);
899+
PyObject obj = this.GetVariable(name);
856900
return (T)obj.AsManagedObject(typeof(T));
857901
}
858902

src/runtime/runtime.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,11 @@ internal unsafe static extern int
832832
internal unsafe static extern IntPtr
833833
PyRun_String(string code, IntPtr st, IntPtr globals, IntPtr locals);
834834

835+
[DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl,
836+
ExactSpelling = true, CharSet = CharSet.Ansi)]
837+
internal unsafe static extern IntPtr
838+
PyEval_EvalCode(IntPtr co, IntPtr globals, IntPtr locals);
839+
835840
[DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl,
836841
ExactSpelling = true, CharSet = CharSet.Ansi)]
837842
internal unsafe static extern IntPtr

0 commit comments

Comments
 (0)