Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
filmor merged 1 commit intopythonnet:masterfromlosttech:bugs/DateTimeDecode
Aug 1, 2021
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionssrc/embed_tests/Codecs.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionssrc/runtime/converter.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp