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

Skip to content

Implement named arguments and With semantics in C# embedding side #461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 24, 2017
Merged
6 changes: 4 additions & 2 deletions src/embed_tests/Python.EmbeddingTest.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -97,6 +97,8 @@
<Compile Include="TestPythonException.cs" />
<Compile Include="TestPythonEngineProperties.cs" />
<Compile Include="TestPyTuple.cs" />
<Compile Include="TestNamedArguments.cs" />
<Compile Include="TestPyWith.cs" />
<Compile Include="TestRuntime.cs" />
</ItemGroup>
<ItemGroup>
Expand All @@ -117,4 +119,4 @@
<Copy SourceFiles="$(TargetAssembly)" DestinationFolder="$(PythonBuildDir)" />
<!--Copy SourceFiles="$(TargetAssemblyPdb)" Condition="Exists('$(TargetAssemblyPdb)')" DestinationFolder="$(PythonBuildDir)" /-->
</Target>
</Project>
</Project>
64 changes: 64 additions & 0 deletions src/embed_tests/TestNamedArguments.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using NUnit.Framework;
using Python.Runtime;

namespace Python.EmbeddingTest
{
public class TestNamedArguments
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}

/// <summary>
/// Test named arguments support through Py.kw method
/// </summary>
[Test]
public void TestKeywordArgs()
{
dynamic a = CreateTestClass();
var result = (int)a.Test3(2, Py.kw("a4", 8));

Assert.AreEqual(12, result);
}


/// <summary>
/// Test keyword arguments with .net named arguments
/// </summary>
[Test]
public void TestNamedArgs()
{
dynamic a = CreateTestClass();
var result = (int)a.Test3(2, a4: 8);

Assert.AreEqual(12, result);
}



private static PyObject CreateTestClass()
{
var locals = new PyDict();

PythonEngine.Exec(@"
class cmTest3:
def Test3(self, a1 = 1, a2 = 1, a3 = 1, a4 = 1):
return a1 + a2 + a3 + a4

a = cmTest3()
", null, locals.Handle);

return locals.GetItem("a");
}

}
}
88 changes: 88 additions & 0 deletions src/embed_tests/TestPyWith.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using NUnit.Framework;
using Python.Runtime;

namespace Python.EmbeddingTest
{
public class TestPyWith
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}

/// <summary>
/// Test that exception is raised in context manager that ignores it.
/// </summary>
[Test]
public void TestWithPositive()
{
var locals = new PyDict();

PythonEngine.Exec(@"
class CmTest:
def __enter__(self):
print('Enter')
return self
def __exit__(self, t, v, tb):
# Exception not handled, return will be False
print('Exit')
def fail(self):
return 5 / 0

a = CmTest()
", null, locals.Handle);

var a = locals.GetItem("a");

try
{
Py.With(a, cmTest =>
{
cmTest.fail();
});
}
catch (PythonException e)
{
Assert.IsTrue(e.Message.Contains("ZeroDivisionError"));
}
}


/// <summary>
/// Test that exception is not raised in context manager that handles it
/// </summary>
[Test]
public void TestWithNegative()
{
var locals = new PyDict();

PythonEngine.Exec(@"
class CmTest:
def __enter__(self):
print('Enter')
return self
def __exit__(self, t, v, tb):
# Signal exception is handled by returning true
return True
def fail(self):
return 5 / 0

a = CmTest()
", null, locals.Handle);

var a = locals.GetItem("a");
Py.With(a, cmTest =>
{
cmTest.fail();
});
}
}
}
76 changes: 59 additions & 17 deletions src/runtime/pyobject.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.Dynamic;
using System.Linq.Expressions;
Expand Down Expand Up @@ -915,6 +915,34 @@ public override bool TrySetMember(SetMemberBinder binder, object value)
return true;
}

private void GetArgs(object[] inargs, CallInfo callInfo, out PyTuple args, out PyDict kwargs)
Copy link
Member

Choose a reason for hiding this comment

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

Very nice :)

{
if (callInfo == null || callInfo.ArgumentNames.Count == 0)
{
GetArgs(inargs, out args, out kwargs);
return;
}

// Support for .net named arguments
var namedArgumentCount = callInfo.ArgumentNames.Count;
var regularArgumentCount = callInfo.ArgumentCount - namedArgumentCount;

var argTuple = Runtime.PyTuple_New(regularArgumentCount);
for (int i = 0; i < regularArgumentCount; ++i)
{
AddArgument(argTuple, i, inargs[i]);
}
args = new PyTuple(argTuple);

var namedArgs = new object[namedArgumentCount * 2];
Copy link
Member

Choose a reason for hiding this comment

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

You could just use PyDict here, couldn't you?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, but this way I'm reusing the validation and conversion in the kw method:

...
if (kv[i + 1] is PyObject)
{
    value = ((PyObject)kv[i + 1]).Handle;
}
else
{
    value = Converter.ToPython(kv[i + 1], kv[i + 1]?.GetType());
}
...

Maybe the conversion/validation code could be refactored and called by the 2 methods, I have to look into it.

for (int i = 0; i < namedArgumentCount; ++i)
{
namedArgs[i * 2] = callInfo.ArgumentNames[i];
namedArgs[i * 2 + 1] = inargs[regularArgumentCount + i];
}
kwargs = Py.kw(namedArgs);
}

private void GetArgs(object[] inargs, out PyTuple args, out PyDict kwargs)
{
int arg_count;
Expand All @@ -925,22 +953,10 @@ private void GetArgs(object[] inargs, out PyTuple args, out PyDict kwargs)
IntPtr argtuple = Runtime.PyTuple_New(arg_count);
for (var i = 0; i < arg_count; i++)
{
IntPtr ptr;
if (inargs[i] is PyObject)
{
ptr = ((PyObject)inargs[i]).Handle;
Runtime.XIncref(ptr);
}
else
{
ptr = Converter.ToPython(inargs[i], inargs[i]?.GetType());
}
if (Runtime.PyTuple_SetItem(argtuple, i, ptr) < 0)
{
throw new PythonException();
}
AddArgument(argtuple, i, inargs[i]);
}
args = new PyTuple(argtuple);

kwargs = null;
for (int i = arg_count; i < inargs.Length; i++)
{
Expand All @@ -959,6 +975,32 @@ private void GetArgs(object[] inargs, out PyTuple args, out PyDict kwargs)
}
}

private static void AddArgument(IntPtr argtuple, int i, object target)
{
IntPtr ptr = GetPythonObject(target);

if (Runtime.PyTuple_SetItem(argtuple, i, ptr) < 0)
{
throw new PythonException();
}
}

private static IntPtr GetPythonObject(object target)
Copy link
Member

Choose a reason for hiding this comment

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

I'm pretty sure that we already have a function like this, I'll check.

{
IntPtr ptr;
if (target is PyObject)
{
ptr = ((PyObject)target).Handle;
Runtime.XIncref(ptr);
}
else
{
ptr = Converter.ToPython(target, target?.GetType());
}

return ptr;
}

public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
if (this.HasAttr(binder.Name) && this.GetAttr(binder.Name).IsCallable())
Expand All @@ -967,7 +1009,7 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
PyDict kwargs = null;
try
{
GetArgs(args, out pyargs, out kwargs);
GetArgs(args, binder.CallInfo, out pyargs, out kwargs);
result = CheckNone(InvokeMethod(binder.Name, pyargs, kwargs));
}
finally
Expand Down Expand Up @@ -997,7 +1039,7 @@ public override bool TryInvoke(InvokeBinder binder, object[] args, out object re
PyDict kwargs = null;
try
{
GetArgs(args, out pyargs, out kwargs);
GetArgs(args, binder.CallInfo, out pyargs, out kwargs);
result = CheckNone(Invoke(pyargs, kwargs));
}
finally
Expand Down
31 changes: 30 additions & 1 deletion src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -632,5 +632,34 @@ public static void SetArgv(IEnumerable<string> argv)
Runtime.CheckExceptionOccurred();
}
}

public static void With(PyObject obj, Action<dynamic> Body)
{
// Behavior described here:
// https://docs.python.org/2/reference/datamodel.html#with-statement-context-managers

IntPtr type = Runtime.PyNone;
IntPtr val = Runtime.PyNone;
IntPtr traceBack = Runtime.PyNone;
PythonException ex = null;

try
{
PyObject enterResult = obj.InvokeMethod("__enter__");

Body(enterResult);
}
catch (PythonException e)
{
ex = e;
type = ex.PyType;
val = ex.PyValue;
traceBack = ex.PyTB;
}

var exitResult = obj.InvokeMethod("__exit__", new PyObject(type), new PyObject(val), new PyObject(traceBack));

if (ex != null && !exitResult.IsTrue()) throw ex;
}
}
}