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

Skip to content
Open
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
Some additional stuff to finalize the pull request
  • Loading branch information
Romout committed Feb 6, 2024
commit 96992fbd0870b3ae5aa3f390d14a198f13d397a1
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
- Mohamed Koubaa ([@koubaa](https://github.com/koubaa))
- Patrick Stewart ([@patstew](https://github.com/patstew))
- Peter Kese ([@pkese](https://github.com/pkese))
- Ramin Dalkouhi ([@Romout](https://github.com/Romout))
- Raphael Nestler ([@rnestler](https://github.com/rnestler))
- Rickard Holmberg ([@rickardraysearch](https://github.com/rickardraysearch))
- Sam Winstanley ([@swinstanley](https://github.com/swinstanley))
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].

### Added

- Added a decoder to ease use of C# Func<> method arguments.
### Changed

### Fixed
Expand Down
19 changes: 19 additions & 0 deletions src/embed_tests/Codecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,25 @@ static void TupleRoundtripGeneric<T, TTuple>()

static PyObject GetPythonIterable() => PythonEngine.Eval("map(lambda x: x, [1,2,3])");

[Test]
public void FuncDecoderTest()
{
IPyObjectDecoder decoder = FuncDecoder.Instance;
var scope = Py.CreateScope();
var func = scope.Eval("lambda x: x + 30");
var funcType = func.GetPythonType();
var wrongType = new PyList(new PyObject[0]).GetPythonType();
// Check some good conversions...
Assert.IsTrue(decoder.CanDecode(funcType, typeof(Func<int, int>)));
Assert.IsTrue(decoder.CanDecode(funcType, typeof(Func<int>)));
// ...and some bad
Assert.IsFalse(decoder.CanDecode(funcType, typeof(List<string>)));
Assert.IsFalse(decoder.CanDecode(wrongType, typeof(Func<int>)));
// Create a wrapper for the lambda function above and invoke it
Assert.IsTrue(decoder.TryDecode(func, out Func<int, int> handler));
Assert.AreEqual(handler(12), 42);
}

[Test]
public void ListDecoderTest()
{
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/Codecs/FuncDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ bool IPyObjectDecoder.TryDecode<T1>(PyObject pyObj, out T1 value)
}
// Invoke Invoke-method of _pyObject
generator.Emit(OpCodes.Callvirt, FindMethod(typeof(PyObject), nameof(PyObject.Invoke), BindingFlags.Public | BindingFlags.Instance, typeof(PyObject[])));
// As<bool> was hard, this is a try for AsManagedObject but also not convienent
// As<bool> was hard, this is a try for AsManagedObject but also not convenient
generator.Emit(OpCodes.Ldtoken, returnType);
generator.Emit(OpCodes.Call, typeof(Type).GetMethod(nameof(Type.GetTypeFromHandle), BindingFlags.Public | BindingFlags.Static));
generator.Emit(OpCodes.Callvirt, typeof(PyObject).GetMethod(nameof(PyObject.AsManagedObject), BindingFlags.Public | BindingFlags.Instance));
Expand Down