8000 Rename several methods and add three methods · pythonnet/pythonnet@9981756 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9981756

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 4508a02 commit 9981756

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
@@ -470,6 +470,13 @@ public class PySessionDisposedException: Exception
470470

471471
}
472472

473+
public enum CompileMode
474+
{
475+
Single = 256,
476+
File = 257,
477+
Eval = 258
478+
}
479+
473480
public class PyScope : IDisposable
474481
{
475482
public class GILState : IDisposable
@@ -629,10 +636,47 @@ public PyObject ImportAs(string name, string asname)
629636
{
630637
asname = name;
631638
}
632-
SetLocal(asname, module);
639+
SetVariable(asname, module);
633640
return module;
634641
}
635642

643+
public PyObject Execute(PyObject script)
644+
{
645+
IntPtr ptr = Runtime.PyEval_EvalCode(script.Handle, globals, locals);
646+
Py.Throw();
647+
if(ptr == Runtime.PyNone)
648+
{
649+
Runtime.XDecref(ptr);
650+
return null;
651+
}
652+
return new PyObject(ptr);
653+
}
654+
655+
public T Execute<T>(PyObject script)
656+
{
657+
var pyObj = this.Execute(script);
658+
if(pyObj == null)
659+
{
660+
return default(T);
661+
}
662+
T obj = (T)pyObj.AsManagedObject(typeof(T));
663+
return obj;
664+
}
665+
666+
/// <summary>
667+
/// Compile Method
668+
/// </summary>
669+
/// <remarks>
670+
/// Compile Python expression/statements into ast.
671+
/// </remarks>
672+
public PyObject Compile(string code, string filename = "", CompileMode mode = CompileMode.File)
673+
{
674+
IntPtr flag = (IntPtr)mode;
675+
IntPtr ptr = Runtime.Py_CompileString(code, filename, flag);
676+
Py.Throw();
677+
return new PyObject(ptr);
678+
}
679+
636680
/// <summary>
637681
/// Evaluate a Python expression
638682
/// </summary>
@@ -689,15 +733,15 @@ private void Exec(string code, IntPtr _globals, IntPtr _locals)
689733
}
690734
Runtime.XDecref(ptr);
691735
}
692-
736+
693737
/// <summary>
694-
/// SetGlobal Method
738+
/// SetGlobalVariable Method
695739
/// </summary>
696740
/// <remarks>
697741
/// Add a new variable to global variable dict if it not exists
698742
/// or set the value of the global variable if it exists.
699743
/// </remarks>
700-
public void SetGlobal(string name, object value)
744+
internal void SetGlobalVariable(string name, object value)
701745
{
702746
this.AcquireLock();
703747
using (var pyKey = new PyString(name))
@@ -713,12 +757,12 @@ public void SetGlobal(string name, object value)
713757
}
714758

715759
/// <summary>
716-
/// DelGlobal Method
760+
/// RemoveGlobalVariable Method
717761
/// </summary>
718762
/// <remarks>
719763
/// Remove a variable from the global variable dict.
720764
/// </remarks>
721-
public void DelGlobal(string name)
765+
internal void RemoveGlobalVariable(string name)
722766
{
723767
this.AcquireLock();
724768
using (var pyKey = new PyString(name))
@@ -738,7 +782,7 @@ public void DelGlobal(string name)
738782
/// Add a new variable to local variable dict if it not exists
739783
/// or set the value of the local variable if it exists.
740784
/// </remarks>
741-
public void SetLocal(string name, object value)
785+
public void SetVariable(string name, object value)
742786
{
743787
this.AcquireLock();
744788
using (var pyKey = new PyString(name))
@@ -759,7 +803,7 @@ public void SetLocal(string name, object value)
759803
/// <remarks>
760804
/// Remove a variable from the local variable dict.
761805
/// </remarks>
762-
public void DelLocal(string name)
806+
public void RemoveVariable(string name)
763807
{
764808
this.AcquireLock();
765809
using (var pyKey = new PyString(name))
@@ -778,7 +822,7 @@ public void DelLocal(string name)
778822
/// <remarks>
779823
/// Returns true if the variable appears in the local variable dict or the global variable dict.
780824
/// </remarks>
781-
public bool Exists(string name)
825+
public bool ContainsVariable(string name)
782826
{
783827
this.AcquireLock();
784828
using (var pyKey = new PyString(name))
@@ -798,7 +842,7 @@ public bool Exists(string name)
798842
/// Returns the value of the variable, local variable first.
799843
/// If the variable is not exists, return null.
800844
/// </remarks>
801-
public PyObject Get(string name)
845+
public PyObject GetVariable(string name)
802846
{
803847
this.AcquireLock();
804848
using (var pyKey = new PyString(name))
@@ -824,9 +868,9 @@ public PyObject Get(string name)
824868
}
825869
}
826870

827-
public T Get<T>(string name)
871+
public T GetVariable<T>(string name)
828872
{
829-
PyObject obj = this.Get(name);
873+
PyObject obj = this.GetVariable(name);
830874
return (T)obj.AsManagedObject(typeof(T));
831875
}
832876

src/runtime/runtime.cs

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

833+
[DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl,
834+
ExactSpelling = true, CharSet = CharSet.Ansi)]
835+
internal unsafe static extern IntPtr
836+
PyEval_EvalCode(IntPtr co, IntPtr globals, IntPtr locals);
837+
833838
[DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl,
834839
ExactSpelling = true, CharSet = CharSet.Ansi)]
835840
internal unsafe static extern IntPtr

0 commit comments

Comments
 (0)
0