Closed
Description
Environment
- Pythonnet version: 2.5.2
- Python version: 3.8.10
- Operating System: Windows 10
- .NET Runtime: Framework 4.7
Details
- Trying to pass a python datetime.datetime to a c# method accepting a System.DateTime.
C# code
using System;
using Python.Runtime;
namespace MyNamespace
{
public static class MyClass
{
public static void TestInt(int i)
{
Console.WriteLine(i);
}
public static void TestDateTime(DateTime dateTime)
{
Console.WriteLine(dateTime);
}
}
public class MyCodecs : IPyObjectDecoder
{
public static void Setup()
{
Console.WriteLine("Setup Called");
PyObjectConversions.RegisterDecoder(new MyCodecs());
}
public bool CanDecode(PyObject objectType, Type targetType)
{
Console.WriteLine("CanDecode Called");
return targetType == typeof(DateTime);
}
public bool TryDecode<T>(PyObject pyObj, out T value)
{
Console.WriteLine("TryDecode Called");
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;
}
}
}
Python code
import clr
from datetime import datetime
clr.AddReference('MyLibrary')
from MyNamespace import MyClass, MyCodecs
#these two work
MyCodecs.Setup()
MyClass.TestInt(22)
#error here
MyClass.TestDateTime(datetime(2021, 1, 22))
The exception is:
TypeError: No method matches given arguments for TestDateTime: (<class 'datetime.datetime'>)
Its doent appear that any attempt is made to call MyCodecs.CanDecode or MyCodecs.TryDecode. The WriteLines in those methods never get hit
Metadata
Metadata
Assignees
Labels
No labels