| 173 | } |
| 174 | |
| 175 | PYBIND11_NOINLINE module_ import_numpy_core_submodule(const char *submodule_name) { |
| 176 | module_ numpy = module_::import("numpy"); |
| 177 | str version_string = numpy.attr("__version__"); |
| 178 | |
| 179 | module_ numpy_lib = module_::import("numpy.lib"); |
| 180 | object numpy_version = numpy_lib.attr("NumpyVersion")(version_string); |
| 181 | int major_version = numpy_version.attr("major").cast<int>(); |
| 182 | |
| 183 | #ifdef PYBIND11_NUMPY_1_ONLY |
| 184 | if (major_version >= 2) { |
| 185 | throw std::runtime_error( |
| 186 | "This extension was built with PYBIND11_NUMPY_1_ONLY defined, " |
| 187 | "but NumPy 2 is used in this process. For NumPy2 compatibility, " |
| 188 | "this extension needs to be rebuilt without the PYBIND11_NUMPY_1_ONLY define."); |
| 189 | } |
| 190 | #endif |
| 191 | /* `numpy.core` was renamed to `numpy._core` in NumPy 2.0 as it officially |
| 192 | became a private module. */ |
| 193 | std::string numpy_core_path = major_version >= 2 ? "numpy._core" : "numpy.core"; |
| 194 | return module_::import((numpy_core_path + "." + submodule_name).c_str()); |
| 195 | } |
| 196 | |
| 197 | template <typename T> |
| 198 | struct same_size { |