rust 判断对象是PyDatetime还是Pydate的示例?

enxuqcxy  于 2023-03-30  发布在  其他
关注(0)|答案(1)|浏览(121)

下面是我的main.rs文件:

use pyo3::prelude::*;
use pyo3::types::{PyDateTime, PyDate};

fn main() -> () {
    Python::with_gil(|py| {
        let datetime = py.import("datetime").unwrap();
        let my_datetime = datetime.call_method1("datetime", (2022, 3, 28, 0, 0, 0, 0)).unwrap();
        println!("isinstance pydatetime: {:?}", my_datetime.is_instance_of::<PyDateTime>().unwrap());
        println!("isinstance pydate: {:?}", my_datetime.is_instance_of::<PyDate>().unwrap());

        let my_date = datetime.call_method1("date", (2022, 3, 28)).unwrap();
        println!("isinstance pydatetime: {:?}", my_date.is_instance_of::<PyDateTime>().unwrap());
        println!("isinstance pydate: {:?}", my_date.is_instance_of::<PyDate>().unwrap());
    })
}

下面是我的Cargo.toml文件:

[package]
name = "tmp"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
pyo3 = { version = "0.18.0", features = ["auto-initialize"] }

如果我运行上面的代码,我得到

isinstance pydatetime: true
isinstance pydate: true
isinstance pydatetime: true
isinstance pydate: true

我觉得这有点奇怪,因为直接在Python中运行,我得到:

In [27]: import datetime as dt

In [28]: isinstance(my_datetime, dt.datetime)
Out[28]: True

In [29]: isinstance(my_datetime, dt.date)
Out[29]: True

In [30]: isinstance(my_date, dt.datetime)
Out[30]: False

In [31]: isinstance(my_date, dt.date)
Out[31]: True

所以我本以为

my_date.is_instance_of::<PyDateTime>().unwrap()

也是false
为什么不是这样呢?我如何区分一个对象是datetime还是date,而不需要求助于像检查dt.get_type().name()这样的更简单的方法?

xoshrz7s

xoshrz7s1#

我已经向PyO3提交了一个pull请求,应该可以解决这个问题:https://github.com/PyO3/pyo3/pull/3071

diff --git a/src/types/datetime.rs b/src/types/datetime.rs
index 85860728568..2c995356e2c 100644
--- a/src/types/datetime.rs
+++ b/src/types/datetime.rs
@@ -228,7 +228,7 @@ pub struct PyDateTime(PyAny);
 pyobject_native_type!(
     PyDateTime,
     crate::ffi::PyDateTime_DateTime,
-    *ensure_datetime_api(Python::assume_gil_acquired()).DateType,
+    *ensure_datetime_api(Python::assume_gil_acquired()).DateTimeType,
     #module=Some("datetime"),
     #checkfunction=PyDateTime_Check
 );

相关问题