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

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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
  • Loading branch information
yagweb committed Apr 7, 2017
commit df6a49aced3f4040f97625c32f611da43695e041
48 changes: 37 additions & 11 deletions src/embed_tests/TestPyScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void TestScopeClass()
dynamic _ps = ps;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this dynamic scope necessary?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I see below, you dynamically accessed the class

_ps.bb = 100;
ps.Exec(
"class class1():\n" +
"class Class1():\n" +
" def __init__(self, value):\n" +
" self.value = value\n" +
" def call(self, arg):\n" +
Expand All @@ -146,8 +146,8 @@ public void TestScopeClass()
" global bb\n" +
" bb = self.value + arg\n" //update scope variable
);
dynamic obj1 = _ps.class1(20);
var result = obj1.call(10).AsManagedObject(typeof(int));
dynamic obj1 = _ps.Class1(20);
var result = obj1.call(10).As<int>();
Assert.AreEqual(130, result);

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

ps.Exec("sys.attr1 = 2");
var value1 = ps.Eval<int>("sys.attr1");
var value2 = (int)sys.attr1.AsManagedObject(typeof(int));
var value2 = (int)sys.attr1.As<int>();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the cast.

Assert.AreEqual(2, value1);
Assert.AreEqual(2, value2);

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

PyScope scope = ps.CreateScope();
PyScope scope = Py.CreateScope();
scope.ImportScope(ps, "ps");

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

Expand All @@ -204,6 +205,30 @@ public void TestImportScope()
}
}

/// <summary>
/// Create a scope and import variables from a scope,
/// exec Python statements in the scope then discard it.
/// </summary>
[Test]
public void TestImportAllFromScope()
{
using (Py.GIL())
{
ps.SetVariable("bb", 100);
ps.SetVariable("cc", 10);

PyScope scope = ps.NewScope();

scope.Exec("aa = bb + cc + 3");
var result = scope.GetVariable<int>("aa");
Assert.AreEqual(113, result);

scope.Dispose();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use using to give a good example?


Assert.IsFalse(ps.ContainsVariable("aa"));
}
}

/// <summary>
/// Create a scope and import variables from a scope,
/// call the function imported.
Expand All @@ -219,24 +244,24 @@ public void TestImportScopeFunction()
"def func1():\n" +
" return cc + bb\n");

PyScope scope = ps.CreateScope();
PyScope scope = ps.NewScope();

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

var result1 = func2().AsManagedObject(typeof(int));
var result1 = func2().As<int>();
Assert.AreEqual(0, result1);

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

ps.SetVariable("cc", 20);
var result3 = func2().AsManagedObject(typeof(int));
var result3 = func2().As<int>();
Assert.AreEqual(10, result3);
ps.SetVariable("cc", 10); //rollback

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

var scope = Py.CreateScope();
scope.ImportScope("test");
scope.ImportAllFromScope("test");
//scope.ImportModule("test");

Assert.IsTrue(scope.ContainsVariable("bb"));
}
Expand Down
5 changes: 1 addition & 4 deletions src/runtime/Python.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@
<Optimize>false</Optimize>
<DebugType>full</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'DebugWinPY3|x64'">
<DefineConstants>TRACE;DEBUG;PYTHON3;PYTHON36;UCS2</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
Expand Down Expand Up @@ -167,4 +164,4 @@
<Copy SourceFiles="$(TargetAssembly)" DestinationFolder="$(PythonBuildDir)" />
<Copy SourceFiles="$(TargetAssemblyPdb)" Condition="Exists('$(TargetAssemblyPdb)')" DestinationFolder="$(PythonBuildDir)" />
</Target>
</Project>
</Project>
21 changes: 21 additions & 0 deletions src/runtime/pyobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,27 @@ public object AsManagedObject(Type t)
}
return result;
}

/// <summary>
/// As Method
/// </summary>
/// <remarks>
/// Return a managed object of the given type, based on the
/// value of the Python object.
/// </remarks>
public T As<T>()
{
if (typeof(T) == typeof(PyObject) || typeof(T) == typeof(object))
{
return (T)(this as object);
}
object result;
if (!Converter.ToManaged(obj, typeof(T), out result, false))
{
throw new InvalidCastException("cannot convert object to target type");
}
return (T)result;
}


/// <summary>
Expand Down
Loading