| 16 | use crate::npyffi::*; |
| 17 | |
| 18 | pub(crate) fn numpy_core_name(py: Python<'_>) -> PyResult<&'static str> { |
| 19 | static MOD_NAME: PyOnceLock<&'static str> = PyOnceLock::new(); |
| 20 | |
| 21 | MOD_NAME |
| 22 | .get_or_try_init(py, || { |
| 23 | // numpy 2 renamed to numpy._core |
| 24 | |
| 25 | // strategy mirrored from https://github.com/pybind/pybind11/blob/af67e87393b0f867ccffc2702885eea12de063fc/include/pybind11/numpy.h#L175-L195 |
| 26 | |
| 27 | let numpy = PyModule::import(py, "numpy")?; |
| 28 | let version_string = numpy.getattr("__version__")?; |
| 29 | |
| 30 | let numpy_lib = PyModule::import(py, "numpy.lib")?; |
| 31 | let numpy_version = numpy_lib |
| 32 | .getattr("NumpyVersion")? |
| 33 | .call1((version_string,))?; |
| 34 | let major_version: u8 = numpy_version.getattr("major")?.extract()?; |
| 35 | |
| 36 | Ok(if major_version >= 2 { |
| 37 | "numpy._core" |
| 38 | } else { |
| 39 | "numpy.core" |
| 40 | }) |
| 41 | }) |
| 42 | .copied() |
| 43 | } |
| 44 | |
| 45 | pub(crate) fn mod_name(py: Python<'_>) -> PyResult<&'static str> { |
| 46 | static MOD_NAME: PyOnceLock<String> = PyOnceLock::new(); |