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
Next Next commit
Encoders no support specifying the target encoding type
  • Loading branch information
joaompneves committed Jul 17, 2024
commit 1ef1e7ba5be974c4cc500c5d134641ceb96f6b9d
4 changes: 2 additions & 2 deletions src/runtime/Codecs/EncoderGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public void Add(IPyObjectEncoder item)
/// <inheritdoc />
public bool CanEncode(Type type) => this.encoders.Any(encoder => encoder.CanEncode(type));
/// <inheritdoc />
public PyObject? TryEncode(object value)
public PyObject? TryEncode(object value, Type type)
{
if (value is null) throw new ArgumentNullException(nameof(value));

foreach (var encoder in this.GetEncoders(value.GetType()))
{
var result = encoder.TryEncode(value);
var result = encoder.TryEncode(value, type);
if (result != null)
{
return result;
Expand Down
7 changes: 3 additions & 4 deletions src/runtime/Codecs/EnumPyIntCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ public bool TryDecode<T>(PyObject pyObj, out T? value)
return false;
}

public PyObject? TryEncode(object value)
public PyObject? TryEncode(object value, Type type)
{
if (value is null) return null;

var enumType = value.GetType();
if (!enumType.IsEnum) return null;

if (!type.IsEnum) return null;

try
{
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/Codecs/IPyObjectEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public interface IPyObjectEncoder
bool CanEncode(Type type);
/// <summary>
/// Attempts to encode CLR object <paramref name="value"/> into Python object
/// using the specified <paramref name="type"/>
/// </summary>
PyObject? TryEncode(object value);
PyObject? TryEncode(object value, Type type);
}
2 changes: 1 addition & 1 deletion src/runtime/Codecs/PyObjectConversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static void RegisterDecoder(IPyObjectDecoder decoder)

foreach (var encoder in clrToPython.GetOrAdd(type, GetEncoders))
{
var result = encoder.TryEncode(obj);
var result = encoder.TryEncode(obj, type);
if (result != null) return result;
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/Codecs/RawProxyEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Python.Runtime.Codecs
/// </summary>
public class RawProxyEncoder: IPyObjectEncoder
{
public PyObject TryEncode(object value)
public PyObject TryEncode(object value, Type type)
{
if (value is null) throw new ArgumentNullException(nameof(value));

Expand Down
13 changes: 6 additions & 7 deletions src/runtime/Codecs/TupleCodecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@ public bool CanEncode(Type type)
&& type.Name.StartsWith(typeof(TTuple).Name + '`');
}

public PyObject? TryEncode(object value)
public PyObject? TryEncode(object value, Type type)
{
if (value == null) return null;

var tupleType = value.GetType();
if (tupleType == typeof(object)) return null;
if (!this.CanEncode(tupleType)) return null;
if (tupleType == typeof(TTuple)) return new PyTuple();
if (type == typeof(object)) return null;
if (!this.CanEncode(type)) return null;
if (type == typeof(TTuple)) return new PyTuple();

nint fieldCount = tupleType.GetGenericArguments().Length;
nint fieldCount = type.GetGenericArguments().Length;
using var tuple = Runtime.PyTuple_New(fieldCount);
PythonException.ThrowIfIsNull(tuple);
int fieldIndex = 0;
foreach (FieldInfo field in tupleType.GetFields())
foreach (FieldInfo field in type.GetFields())
{
var item = field.GetValue(value);
using var pyItem = Converter.ToPython(item, field.FieldType);
Expand Down
6 changes: 6 additions & 0 deletions src/runtime/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -987,5 +987,11 @@ public static PyObject ToPythonAs<T>(this T? o)
if (o is null) return Runtime.None;
return Converter.ToPython(o, typeof(T)).MoveToPyObject();
}

public static PyObject ToPythonAs(this object o, Type type)
{
if (o is null) return Runtime.None;
return Converter.ToPython(o, type).MoveToPyObject();
}
}
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy