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

Skip to content

Commit df6a49a

Browse files
committed
add several methods and rebased
Add an optional dict parameter for Eval/Exec/Execute methods Add a new field obj which point to a Python Module (same as pyobject) Add a static method New Rename the old ImportScope method to ImportAllFromScope Add a new ImportScope method and add a unit test for it Rename the CreateScope method to NewScope
1 parent dd492f4 commit df6a49a

File tree

5 files changed

+153
-92
lines changed

5 files changed

+153
-92
lines changed

src/embed_tests/TestPyScope.cs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public void TestScopeClass()
137137
dynamic _ps = ps;
138138
_ps.bb = 100;
139139
ps.Exec(
140-
"class class1():\n" +
140+
"class Class1():\n" +
141141
" def __init__(self, value):\n" +
142142
" self.value = value\n" +
143143
" def call(self, arg):\n" +
@@ -146,8 +146,8 @@ public void TestScopeClass()
146146
" global bb\n" +
147147
" bb = self.value + arg\n" //update scope variable
148148
);
149-
dynamic obj1 = _ps.class1(20);
150-
var result = obj1.call(10).AsManagedObject(typeof(int));
149+
dynamic obj1 = _ps.Class1(20);
150+
var result = obj1.call(10).As<int>();
151151
Assert.AreEqual(130, result);
152152

153153
obj1.update(10);
@@ -170,7 +170,7 @@ public void TestImportModule()
170170

171171
ps.Exec("sys.attr1 = 2");
172172
var value1 = ps.Eval<int>("sys.attr1");
173-
var value2 = (int)sys.attr1.AsManagedObject(typeof(int));
173+
var value2 = (int)sys.attr1.As<int>();
174174
Assert.AreEqual(2, value1);
175175
Assert.AreEqual(2, value2);
176176

@@ -192,9 +192,10 @@ public void TestImportScope()
192192
ps.SetVariable("bb", 100);
193193
ps.SetVariable("cc", 10);
194194

195-
PyScope scope = ps.CreateScope();
195+
PyScope scope = Py.CreateScope();
196+
scope.ImportScope(ps, "ps");
196197

197-
scope.Exec("aa = bb + cc + 3");
198+
scope.Exec("aa = ps.bb + ps.cc + 3");
198199
var result = scope.GetVariable<int>("aa");
199200
Assert.AreEqual(113, result);
200201

@@ -204,6 +205,30 @@ public void TestImportScope()
204205
}
205206
}
206207

208+
/// <summary>
209+
/// Create a scope and import variables from a scope,
210+
/// exec Python statements in the scope then discard it.
211+
/// </summary>
212+
[Test]
213+
public void TestImportAllFromScope()
214+
{
215+
using (Py.GIL())
216+
{
217+
ps.SetVariable("bb", 100);
218+
ps.SetVariable("cc", 10);
219+
220+
PyScope scope = ps.NewScope();
221+
222+
scope.Exec("aa = bb + cc + 3");
223+
var result = scope.GetVariable<int>("aa");
224+
Assert.AreEqual(113, result);
225+
226+
scope.Dispose();
227+
228+
Assert.IsFalse(ps.ContainsVariable("aa"));
229+
}
230+
}
231+
207232
/// <summary>
208233
/// Create a scope and import variables from a scope,
209234
/// call the function imported.
@@ -219,24 +244,24 @@ public void TestImportScopeFunction()
219244
"def func1():\n" +
220245
" return cc + bb\n");
221246

222-
PyScope scope = ps.CreateScope();
247+
PyScope scope = ps.NewScope();
223248

224249
//'func1' is imported from the origion scope
225250
scope.Exec(
226251
"def func2():\n" +
227252
" return func1() - cc - bb\n");
228253
dynamic func2 = scope.GetVariable("func2");
229254

230-
var result1 = func2().AsManagedObject(typeof(int));
255+
var result1 = func2().As<int>();
231256
Assert.AreEqual(0, result1);
232257

233258
scope.SetVariable("cc", 20);//it has no effect on the globals of 'func1'
234-
var result2 = func2().AsManagedObject(typeof(int));
259+
var result2 = func2().As<int>();
235260
Assert.AreEqual(-10, result2);
236261
scope.SetVariable("cc", 10); //rollback
237262

238263
ps.SetVariable("cc", 20);
239-
var result3 = func2().AsManagedObject(typeof(int));
264+
var result3 = func2().As<int>();
240265
Assert.AreEqual(10, result3);
241266
ps.SetVariable("cc", 10); //rollback
242267

@@ -256,7 +281,8 @@ public void TestImportScopeByName()
256281
ps.SetVariable("bb", 100);
257282

258283
var scope = Py.CreateScope();
259-
scope.ImportScope("test");
284+
scope.ImportAllFromScope("test");
285+
//scope.ImportModule("test");
260286

261287
Assert.IsTrue(scope.ContainsVariable("bb"));
262288
}

src/runtime/Python.Runtime.csproj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@
7070
<Optimize>false</Optimize>
7171
<DebugType>full</DebugType>
7272
</PropertyGroup>
73-
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'DebugWinPY3|x64'">
74-
<DefineConstants>TRACE;DEBUG;PYTHON3;PYTHON36;UCS2</DefineConstants>
75-
</PropertyGroup>
7673
<ItemGroup>
7774
<Reference Include="System" />
7875
</ItemGroup>
@@ -167,4 +164,4 @@
167164
<Copy SourceFiles="$(TargetAssembly)" DestinationFolder="$(PythonBuildDir)" />
168165
<Copy SourceFiles="$(TargetAssemblyPdb)" Condition="Exists('$(TargetAssemblyPdb)')" DestinationFolder="$(PythonBuildDir)" />
169166
</Target>
170-
</Project>
167+
</Project>

src/runtime/pyobject.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,27 @@ public object AsManagedObject(Type t)
9696
}
9797
return result;
9898
}
99+
100+
/// <summary>
101+
/// As Method
102+
/// </summary>
103+
/// <remarks>
104+
/// Return a managed object of the given type, based on the
105+
/// value of the Python object.
106+
/// </remarks>
107+
public T As<T>()
108+
{
109+
if (typeof(T) == typeof(PyObject) || typeof(T) == typeof(object))
110+
{
111+
return (T)(this as object);
112+
}
113+
object result;
114+
if (!Converter.ToManaged(obj, typeof(T), out result, false))
115+
{
116+
throw new InvalidCastException("cannot convert object to target type");
117+
}
118+
return (T)result;
119+
}
99120

100121

101122
/// <summary>

0 commit comments

Comments
 (0)