Skip to content

Commit 20a6878

Browse files
author
peng.li24
committed
test: update module.cpp
1 parent 9717973 commit 20a6878

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

tests/module.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,32 @@ PYBIND11_MODULE(numpycpp, m) {
4949
#endif
5050
});
5151

52+
// -- astype dtype 匹配诊断 -------------------------------------------------
53+
// 返回 arr 的 dtype 匹配详情,用于验证 py::dtype::of<T>() vs kind()+itemsize 行为
54+
m.def("_diag_astype_dtype", [](const py::array& arr) -> py::dict {
55+
auto buf = arr.request();
56+
auto dt = arr.dtype();
57+
58+
char kind = dt.kind();
59+
bool is_f32 = (kind == 'f' && buf.itemsize == 4);
60+
bool is_f64 = (kind == 'f' && buf.itemsize == 8);
61+
62+
bool dt_of_f32 = dt.is(py::dtype::of<float>());
63+
bool dt_of_f64 = dt.is(py::dtype::of<double>());
64+
65+
py::dict result;
66+
result["dtype_str"] = py::str(dt);
67+
result["kind"] = std::string(1, kind);
68+
result["itemsize"] = buf.itemsize;
69+
result["format"] = buf.format;
70+
result["is_f32(kind)"] = is_f32;
71+
result["is_f64(kind)"] = is_f64;
72+
result["dtype.is(float)"] = dt_of_f32;
73+
result["dtype.is(double)"]= dt_of_f64;
74+
result["fallback_works"] = (is_f32 || is_f64 || dt_of_f32 || dt_of_f64);
75+
return result;
76+
});
77+
5278
// -- linalg submodule --------------------------------------------------
5379
py::module_ la = m.def_submodule("linalg", "numpy.linalg equivalents");
5480
la.def("norm", static_cast<float(*)(const py::array_t<float>&)>(&numpy::linalg::norm));

0 commit comments

Comments
 (0)