- Notifications
You must be signed in to change notification settings - Fork750
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Closed
Uh oh!
There was an error while loading.Please reload this page.
Closed
Changes fromall commits
Commits
Show all changes
6 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
133 changes: 118 additions & 15 deletionssrc/embed_tests/Codecs.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -6,28 +6,34 @@ namespace Python.EmbeddingTest { | ||
using Python.Runtime; | ||
using Python.Runtime.Codecs; | ||
public class Codecs | ||
koubaa marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
{ | ||
[SetUp] | ||
public void SetUp() | ||
{ | ||
PythonEngine.Initialize(); | ||
} | ||
[TearDown] | ||
public void Dispose() | ||
{ | ||
PythonEngine.Shutdown(); | ||
} | ||
[Test] | ||
public void ConversionsGeneric() | ||
{ | ||
ConversionsGeneric<ValueTuple<int, string, object>, ValueTuple>(); | ||
} | ||
static void ConversionsGeneric<T, TTuple>() | ||
{ | ||
TupleCodec<TTuple>.Register(); | ||
var tuple = Activator.CreateInstance(typeof(T), 42, "42", new object()); | ||
T restored = default; | ||
using (Py.GIL()) | ||
using (var scope = Py.CreateScope()) | ||
{ | ||
void Accept(T value) => restored = value; | ||
var accept = new Action<T>(Accept).ToPython(); | ||
scope.Set(nameof(tuple), tuple); | ||
@@ -38,15 +44,18 @@ static void ConversionsGeneric<T, TTuple>() { | ||
} | ||
[Test] | ||
public void ConversionsObject() | ||
{ | ||
ConversionsObject<ValueTuple<int, string, object>, ValueTuple>(); | ||
} | ||
static void ConversionsObject<T, TTuple>() | ||
{ | ||
TupleCodec<TTuple>.Register(); | ||
var tuple = Activator.CreateInstance(typeof(T), 42, "42", new object()); | ||
T restored = default; | ||
using (Py.GIL()) | ||
using (var scope = Py.CreateScope()) | ||
{ | ||
void Accept(object value) => restored = (T)value; | ||
var accept = new Action<object>(Accept).ToPython(); | ||
scope.Set(nameof(tuple), tuple); | ||
@@ -57,30 +66,124 @@ static void ConversionsObject<T, TTuple>() { | ||
} | ||
[Test] | ||
public void TupleRoundtripObject() | ||
{ | ||
TupleRoundtripObject<ValueTuple<int, string, object>, ValueTuple>(); | ||
} | ||
static void TupleRoundtripObject<T, TTuple>() | ||
{ | ||
var tuple = Activator.CreateInstance(typeof(T), 42, "42", new object()); | ||
using (Py.GIL()) | ||
{ | ||
var pyTuple = TupleCodec<TTuple>.Instance.TryEncode(tuple); | ||
Assert.IsTrue(TupleCodec<TTuple>.Instance.TryDecode(pyTuple, out object restored)); | ||
Assert.AreEqual(expected: tuple, actual: restored); | ||
} | ||
} | ||
[Test] | ||
public void TupleRoundtripGeneric() | ||
{ | ||
TupleRoundtripGeneric<ValueTuple<int, string, object>, ValueTuple>(); | ||
} | ||
static void TupleRoundtripGeneric<T, TTuple>() | ||
{ | ||
var tuple = Activator.CreateInstance(typeof(T), 42, "42", new object()); | ||
using (Py.GIL()) | ||
{ | ||
var pyTuple = TupleCodec<TTuple>.Instance.TryEncode(tuple); | ||
Assert.IsTrue(TupleCodec<TTuple>.Instance.TryDecode(pyTuple, out T restored)); | ||
Assert.AreEqual(expected: tuple, actual: restored); | ||
} | ||
} | ||
[Test] | ||
public void FunctionAction() | ||
{ | ||
var codec = FunctionCodec.Instance; | ||
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))); | ||
//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()); | ||
//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 })); | ||
} | ||
[Test] | ||
public void FunctionFunc() | ||
{ | ||
var codec = FunctionCodec.Instance; | ||
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); | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletionssrc/embed_tests/TestCallbacks.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.