Skip to content

Fixed custom decoders not working for DateTime and Decimal #1497

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

Merged
merged 1 commit into from
Aug 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
44 changes: 44 additions & 0 deletions src/embed_tests/Codecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,24 @@ public void ExceptionDecoded()
Assert.AreEqual(TestExceptionMessage, error.Message);
}

[Test]
public void DateTimeDecoded()
{
using var scope = Py.CreateScope();
scope.Exec(@"
import clr
from datetime import datetime


from Python.EmbeddingTest import Codecs, DateTimeDecoder

DateTimeDecoder.Setup()
");
scope.Exec("Codecs.AcceptsDateTime(datetime(2021, 1, 22))");
}

public static void AcceptsDateTime(DateTime v) {}

class ValueErrorWrapper : Exception
{
public ValueErrorWrapper(string message) : base(message) { }
Expand Down Expand Up @@ -419,4 +437,30 @@ public bool TryDecode<T>(PyObject pyObj, out T value)
return true;
}
}

public class DateTimeDecoder : IPyObjectDecoder
{
public static void Setup()
{
PyObjectConversions.RegisterDecoder(new DateTimeDecoder());
}

public bool CanDecode(PyObject objectType, Type targetType)
{
return targetType == typeof(DateTime);
}

public bool TryDecode<T>(PyObject pyObj, out T value)
{
var dt = new DateTime(
pyObj.GetAttr("year").As<int>(),
pyObj.GetAttr("month").As<int>(),
pyObj.GetAttr("day").As<int>(),
pyObj.GetAttr("hour").As<int>(),
pyObj.GetAttr("minute").As<int>(),
pyObj.GetAttr("second").As<int>());
value = (T)(object)dt;
return true;
}
}
}
24 changes: 17 additions & 7 deletions src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,8 @@ internal static IntPtr ToPython(object value, Type type)
return result;
}

if (Type.GetTypeCode(type) == TypeCode.Object
&& value.GetType() != typeof(object)
&& value is not Type
|| type.IsEnum
) {
if (EncodableByUser(type, value))
{
var encoded = PyObjectConversions.TryEncode(value, type);
if (encoded != null) {
result = encoded.Handle;
Expand Down Expand Up @@ -301,6 +298,13 @@ internal static IntPtr ToPython(object value, Type type)
}
}

static bool EncodableByUser(Type type, object value)
{
TypeCode typeCode = Type.GetTypeCode(type);
return type.IsEnum
|| typeCode is TypeCode.DateTime or TypeCode.Decimal
|| typeCode == TypeCode.Object && value.GetType() != typeof(object) && value is not Type;
}

/// <summary>
/// In a few situations, we don't have any advisory type information
Expand Down Expand Up @@ -523,8 +527,7 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
return false;
}

TypeCode typeCode = Type.GetTypeCode(obType);
if (typeCode == TypeCode.Object || obType.IsEnum)
if (DecodableByUser(obType))
{
IntPtr pyType = Runtime.PyObject_TYPE(value);
if (PyObjectConversions.TryDecode(value, pyType, obType, out result))
Expand All @@ -536,6 +539,13 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
return ToPrimitive(value, obType, out result, setError);
}

static bool DecodableByUser(Type type)
{
TypeCode typeCode = Type.GetTypeCode(type);
return type.IsEnum
|| typeCode is TypeCode.Object or TypeCode.Decimal or TypeCode.DateTime;
}

internal delegate bool TryConvertFromPythonDelegate(IntPtr pyObj, out object result);

internal static int ToInt32(BorrowedReference value)
Expand Down