Skip to content

Add codecs for functions #1080

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

Closed
wants to merge 6 commits into from
Closed
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
improve function codec
  • Loading branch information
koubaa committed Mar 9, 2020
commit e231ff2987f181cd266fdfc4e38d49ca708be843
124 changes: 58 additions & 66 deletions src/embed_tests/Codecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,97 +101,89 @@ static void TupleRoundtripGeneric<T, TTuple>()
[Test]
public void FunctionAction()
{
FunctionCodec.Register();
var codec = FunctionCodec.Instance;

//decoding - python functions to C# actions
{
PyInt x = new PyInt(1);
PyDict y = new PyDict();
//non-callables can't be decoded into Action
Assert.IsFalse(codec.CanDecode(x, typeof(Action)));
Assert.IsFalse(codec.CanDecode(y, typeof(Action)));

var locals = new PyDict();
PythonEngine.Exec(@"
PyInt x = new PyInt(1);
PyDict y = new PyDict();
//non-callables can't be decoded into Action
Assert.IsFalse(codec.CanDecode(x, typeof(Action)));
Assert.IsFalse(codec.CanDecode(y, typeof(Action)));

var locals = new PyDict();
PythonEngine.Exec(@"
def foo():
return 1
def bar(a):
return 2
", null, locals.Handle);

//foo, the function with no arguments
var fooFunc = locals.GetItem("foo");
Assert.IsFalse(codec.CanDecode(fooFunc, typeof(bool)));
//foo, the function with no arguments
var fooFunc = locals.GetItem("foo");
Assert.IsFalse(codec.CanDecode(fooFunc, typeof(bool)));

//CanDecode does not work for variadic actions
//Assert.IsFalse(codec.CanDecode(fooFunc, typeof(Action<object[]>)));
Assert.IsTrue(codec.CanDecode(fooFunc, typeof(Action)));
//CanDecode does not work for variadic actions
//Assert.IsFalse(codec.CanDecode(fooFunc, typeof(Action<object[]>)));
Assert.IsTrue(codec.CanDecode(fooFunc, typeof(Action)));

Action fooAction;
Assert.IsTrue(codec.TryDecode(fooFunc, out fooAction));
Assert.DoesNotThrow(() => fooAction());
Action fooAction;
Assert.IsTrue(codec.TryDecode(fooFunc, out fooAction));
Assert.DoesNotThrow(() => fooAction());

//bar, the function with an argument
var barFunc = locals.GetItem("bar");
Assert.IsFalse(codec.CanDecode(barFunc, typeof(bool)));
//Assert.IsFalse(codec.CanDecode(barFunc, typeof(Action)));
Assert.IsTrue(codec.CanDecode(barFunc, typeof(Action<object[]>)));
//bar, the function with an argument
var barFunc = locals.GetItem("bar");
Assert.IsFalse(codec.CanDecode(barFunc, typeof(bool)));
//Assert.IsFalse(codec.CanDecode(barFunc, typeof(Action)));
Assert.IsTrue(codec.CanDecode(barFunc, typeof(Action<object[]>)));

Action<object[]> barAction;
Assert.IsTrue(codec.TryDecode(barFunc, out barAction));
Assert.DoesNotThrow(() => barAction(new[] { (object)true }));
}
Action<object[]> barAction;
Assert.IsTrue(codec.TryDecode(barFunc, out barAction));
Assert.DoesNotThrow(() => barAction(new[] { (object)true }));
}

[Test]
public void FunctionFunc()
{
FunctionCodec.Register();
var codec = FunctionCodec.Instance;

//decoding - python functions to C# funcs
{
PyInt x = new PyInt(1);
PyDict y = new PyDict();
//non-callables can't be decoded into Func
Assert.IsFalse(codec.CanDecode(x, typeof(Func<object>)));
Assert.IsFalse(codec.CanDecode(y, typeof(Func<object>)));

var locals = new PyDict();
PythonEngine.Exec(@"
PyInt x = new PyInt(1);
PyDict y = new PyDict();
//non-callables can't be decoded into Func
Assert.IsFalse(codec.CanDecode(x, typeof(Func<object>)));
Assert.IsFalse(codec.CanDecode(y, typeof(Func<object>)));

var locals = new PyDict();
PythonEngine.Exec(@"
def foo():
return 1
def bar(a):
return 2
", null, locals.Handle);

//foo, the function with no arguments
var fooFunc = locals.GetItem("foo");
Assert.IsFalse(codec.CanDecode(fooFunc, typeof(bool)));

//CanDecode does not work for variadic actions
//Assert.IsFalse(codec.CanDecode(fooFunc, typeof(Func<object[], object>)));
Assert.IsTrue(codec.CanDecode(fooFunc, typeof(Func<object>)));

Func<object> foo;
Assert.IsTrue(codec.TryDecode(fooFunc, out foo));
object res1 = null;
Assert.DoesNotThrow(() => res1 = foo());
Assert.AreEqual(res1, 1);

//bar, the function with an argument
var barFunc = locals.GetItem("bar");
Assert.IsFalse(codec.CanDecode(barFunc, typeof(bool)));
//Assert.IsFalse(codec.CanDecode(barFunc, typeof(Func<object>)));
Assert.IsTrue(codec.CanDecode(barFunc, typeof(Func<object[], object>)));

Func<object[], object> bar;
Assert.IsTrue(codec.TryDecode(barFunc, out bar));
object res2 = null;
Assert.DoesNotThrow(() => res2 = bar(new[] { (object)true }));
Assert.AreEqual(res2, 2);
}
//foo, the function with no arguments
var fooFunc = locals.GetItem("foo");
Assert.IsFalse(codec.CanDecode(fooFunc, typeof(bool)));

//CanDecode does not work for variadic actions
//Assert.IsFalse(codec.CanDecode(fooFunc, typeof(Func<object[], object>)));
Assert.IsTrue(codec.CanDecode(fooFunc, typeof(Func<object>)));

Func<object> foo;
Assert.IsTrue(codec.TryDecode(fooFunc, out foo));
object res1 = null;
Assert.DoesNotThrow(() => res1 = foo());
Assert.AreEqual(res1, 1);

//bar, the function with an argument
var barFunc = locals.GetItem("bar");
Assert.IsFalse(codec.CanDecode(barFunc, typeof(bool)));
//Assert.IsFalse(codec.CanDecode(barFunc, typeof(Func<object>)));
Assert.IsTrue(codec.CanDecode(barFunc, typeof(Func<object[], object>)));

Func<object[], object> bar;
Assert.IsTrue(codec.TryDecode(barFunc, out bar));
object res2 = null;
Assert.DoesNotThrow(() => res2 = bar(new[] { (object)true }));
Assert.AreEqual(res2, 2);
}
}
}
16 changes: 10 additions & 6 deletions src/runtime/Codecs/FunctionCodec.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Reflection;

namespace Python.Runtime.Codecs
{
Expand All @@ -22,6 +23,12 @@ from inspect import signature
return new PyInt(x).ToInt32();
}

private static int GetNumArgs(Type targetType)
{
MethodInfo invokeMethod = targetType.GetMethod("Invoke");
return invokeMethod.GetParameters().Length;
}

private static bool IsUnaryAction(Type targetType)
{
return targetType == typeof(Action);
Expand Down Expand Up @@ -54,8 +61,7 @@ private static bool IsFunc(Type targetType)

private static bool IsCallable(Type targetType)
{
//TODO - delegate, event, ...
return IsAction(targetType) || IsFunc(targetType);
return ClassManager.IsDelegate(targetType);
}

public static FunctionCodec Instance { get; } = new FunctionCodec();
Expand All @@ -68,9 +74,6 @@ public bool CanDecode(PyObject objectType, Type targetType)
if (!IsCallable(targetType))
return false;

//We don't know if it will work without the instance
//The number of arguments of a unary or variadic object callable
//is always going to be 0 or 1
return true;
}

Expand Down Expand Up @@ -140,6 +143,8 @@ public bool TryDecode<T>(PyObject pyObj, out T value)
return false;

var numArgs = GetNumArgs(pyObj);
if (numArgs != GetNumArgs(tT))
return false;

if (IsAction(tT))
{
Expand Down Expand Up @@ -181,7 +186,6 @@ public bool TryDecode<T>(PyObject pyObj, out T value)
public static void Register()
{
PyObjectConversions.RegisterDecoder(Instance);
PyObjectConversions.RegisterDecoder(Instance);
}
}
}
7 changes: 6 additions & 1 deletion src/runtime/classmanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public static void Reset()
cache = new Dictionary<Type, ClassBase>(128);
}

public static bool IsDelegate(Type type)
{
return type.IsSubclassOf(dtype);
}

/// <summary>
/// Return the ClassBase-derived instance that implements a particular
/// reflected managed type, creating it if it doesn't yet exist.
Expand Down Expand Up @@ -83,7 +88,7 @@ private static ClassBase CreateClass(Type type)
impl = new GenericType(type);
}

else if (type.IsSubclassOf(dtype))
else if (IsDelegate(type))
{
impl = new DelegateObject(type);
}
Expand Down