| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| 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; | ||
| 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) | ||
| { | ||
| 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]; | ||
|
Comment thread
Copy link
Copy Markdown
Member
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityYou could just use PyDict here, couldn't you?
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Contributor
Author
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Qualityyes, 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.
Sorry, something went wrong.
All reactions
|
||
| 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) | ||
|
Comment thread
Copy link
Copy Markdown
Member
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI'm pretty sure that we already have a function like this, I'll check.
Sorry, something went wrong.
All reactions
|
||
| { | ||
| 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 | ||
| Back | FazBrowse Home | New Git URL |
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityVery nice :)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.