8000 Encoders no support specifying the target encoding type · joaompneves/pythonnet@1ef1e7b · GitHub
[go: up one dir, main page]

Skip to content

Commit 1ef1e7b

Browse files
committed
Encoders no support specifying the target encoding type
1 parent 563e369 commit 1ef1e7b

File tree

7 files changed

+21
-16
lines changed

7 files changed

+21
-16
lines changed

src/runtime/Codecs/EncoderGroup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ public void Add(IPyObjectEncoder item)
2828
/// <inheritdoc />
2929
public bool CanEncode(Type type) => this.encoders.Any(encoder => encoder.CanEncode(type));
3030
/// <inheritdoc />
31-
public PyObject? TryEncode(object value)
31+
public PyObject? TryEncode(object value, Type type)
3232
{
3333
if (value is null) throw new ArgumentNullException(nameof(value));
3434

3535
foreach (var encoder in this.GetEncoders(value.GetType()))
3636
{
37-
var result = encoder.TryEncode(value);
37+
var result = encoder.TryEncode(value, type);
3838
if (result != null)
3939
{
4040
return result;

src/runtime/Codecs/EnumPyIntCodec.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,11 @@ public bool TryDecode<T>(PyObject pyObj, out T? value)
4646
return false;
4747
}
4848

49-
public PyObject? TryEncode(object value)
49+
public PyObject? TryEncode(object value, Type type)
5050
{
5151
if (value is null) return null;
52-
53-
var enumType = value.GetType();
54-
if (!enumType.IsEnum) return null;
52+
53+
if (!type.IsEnum) return null;
5554

5655
try
5756
{

src/runtime/Codecs/IPyObjectEncoder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public interface IPyObjectEncoder
1313
bool CanEncode(Type type);
1414
/// <summary>
1515
/// Attempts to encode CLR object <paramref name="value"/> into Python object
16+
/// using the specified <paramref name="type"/>
1617
/// </summary>
17-
PyObject? TryEncode(object value);
18+
PyObject? TryEncode(object value, Type type);
1819
}

src/runtime/Codecs/PyObjectConversions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static void RegisterDecoder(IPyObjectDecoder decoder)
5454

5555
foreach (var encoder in clrToPython.GetOrAdd(type, GetEncoders))
5656
{
57-
var result = encoder.TryEncode(obj);
57+
var result = encoder.TryEncode(obj, type);
5858
if (result != null) return result;
5959
}
6060

src/runtime/Codecs/RawProxyEncoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Python.Runtime.Codecs
88
/// </summary>
99
public class RawProxyEncoder: IPyObjectEncoder
1010
{
11-
public PyObject TryEncode(object value)
11+
public PyObject TryEncode(object value, Type type)
1212
{
1313
if (value is null) throw new ArgumentNullException(nameof(value));
1414

src/runtime/Codecs/TupleCodecs.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,19 @@ public bool CanEncode(Type type)
1818
&& type.Name.StartsWith(typeof(TTuple).Name + '`');
1919
}
2020

21-
public PyObject? TryEncode(object value)
21+
public PyObject? TryEncode(object value, Type type)
2222
{
2323
if (value == null) return null;
2424

25-
var tupleType = value.GetType();
26-
if (tupleType == typeof(object)) return null;
27-
if (!this.CanEncode(tupleType)) return null;
28-
if (tupleType == typeof(TTuple)) return new PyTuple();
25+
if (type == typeof(object)) return null;
26+
if (!this.CanEncode(type)) return null;
27+
if (type == typeof(TTuple)) return new PyTuple();
2928

30-
nint fieldCount = tupleType.GetGenericArguments().Length;
29+
nint fieldCount = type.GetGenericArguments().Length;
3130
using var tuple = Runtime.PyTuple_New(fieldCount);
3231
PythonException.ThrowIfIsNull(tuple);
3332
int fieldIndex = 0;
34-
foreach (FieldInfo field in tupleType.GetFields())
33+
foreach (FieldInfo field in type.GetFields())
3534
{
3635
var item = field.GetValue(value);
3736
using var pyItem = Converter.ToPython(item, field.FieldType);

src/runtime/Converter.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,5 +987,11 @@ public static PyObject ToPythonAs<T>(this T? o)
987987
if (o is null) return Runtime.None;
988988
return Converter.ToPython(o, typeof(T)).MoveToPyObject();
989989
}
990+
991+
public static PyObject ToPythonAs(this object o, Type type)
992+
{
993+
if (o is null) return Runtime.None;
994+
return Converter.ToPython(o, type).MoveToPyObject();
995+
}
990996
}
991997
}

0 commit comments

Comments
 (0)
0