-
Notifications
You must be signed in to change notification settings - Fork 749
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
Changes from all commits
73c40fc
500d3d3
35a7dc6
c3c1c56
6c27e84
a31c590
66864ad
93b1bda
9f1c378
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"); | ||
} | ||
|
||
} | ||
} |
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(); | ||
}); | ||
} | ||
} | ||
} |
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; | ||
|
@@ -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) | ||
{ | ||
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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could just use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
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; | ||
|
@@ -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++) | ||
{ | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice :)