Skip to content

Added ToPythonAs() extension method for explicit conversion using an arbitrary type #2419

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Fix build
  • Loading branch information
joaompneves committed Jul 18, 2024
commit 255201afbc81f382b74ebcb74ec9506ee67a83ba
4 changes: 2 additions & 2 deletions src/embed_tests/CodecGroups.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public void Encodes()
encoder2,
};

var uri = group.TryEncode(new Uri("data:"));
var uri = group.TryEncode(new Uri("data:"), typeof(Uri));
var clrObject = (CLRObject)ManagedType.GetManagedObject(uri);
Assert.AreSame(encoder1, clrObject.inst);
Assert.AreNotSame(encoder2, clrObject.inst);

var tuple = group.TryEncode(Tuple.Create(1));
var tuple = group.TryEncode(Tuple.Create(1), typeof(Tuple<int>));
clrObject = (CLRObject)ManagedType.GetManagedObject(tuple);
Assert.AreSame(encoder0, clrObject.inst);
}
Expand Down
8 changes: 4 additions & 4 deletions src/embed_tests/Codecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void TupleRoundtripObject()
static void TupleRoundtripObject<T, TTuple>()
{
var tuple = Activator.CreateInstance(typeof(T), 42.0, "42", new object());
using var pyTuple = TupleCodec<TTuple>.Instance.TryEncode(tuple);
using var pyTuple = TupleCodec<TTuple>.Instance.TryEncode(tuple, typeof(T));
Assert.IsTrue(TupleCodec<TTuple>.Instance.TryDecode(pyTuple, out object restored));
Assert.AreEqual(expected: tuple, actual: restored);
}
Expand All @@ -85,7 +85,7 @@ public void TupleRoundtripGeneric()
static void TupleRoundtripGeneric<T, TTuple>()
{
var tuple = Activator.CreateInstance(typeof(T), 42, "42", new object());
using var pyTuple = TupleCodec<TTuple>.Instance.TryEncode(tuple);
using var pyTuple = TupleCodec<TTuple>.Instance.TryEncode(tuple, typeof(T));
Assert.IsTrue(TupleCodec<TTuple>.Instance.TryDecode(pyTuple, out T restored));
Assert.AreEqual(expected: tuple, actual: restored);
}
Expand Down Expand Up @@ -438,7 +438,7 @@ public bool TryDecode<T>(PyObject pyObj, out T value)
return true;
}

public PyObject TryEncode(object value)
public PyObject TryEncode(object value, Type type)
{
var error = (ValueErrorWrapper)value;
return PythonEngine.Eval("ValueError").Invoke(error.Message.ToPython());
Expand Down Expand Up @@ -478,7 +478,7 @@ public bool TryDecode<T>(PyObject pyObj, out T value)
class ObjectToEncoderInstanceEncoder<T> : IPyObjectEncoder
{
public bool CanEncode(Type type) => type == typeof(T);
public PyObject TryEncode(object value) => PyObject.FromManagedObject(this);
public PyObject TryEncode(object value, Type type) => PyObject.FromManagedObject(this);
}

/// <summary>
Expand Down