Merge branch 'pybind:master' into master

This commit is contained in:
Steve R. Sun 2023-09-28 11:14:28 +08:00 committed by GitHub
commit eebdc434af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 7 deletions

View File

@ -142,7 +142,7 @@ repos:
- id: disallow-caps - id: disallow-caps
name: Disallow improper capitalization name: Disallow improper capitalization
language: pygrep language: pygrep
entry: PyBind|Numpy|Cmake|CCache|PyTest entry: PyBind|\bNumpy\b|Cmake|CCache|PyTest
exclude: ^\.pre-commit-config.yaml$ exclude: ^\.pre-commit-config.yaml$
# PyLint has native support - not always usable, but works for us # PyLint has native support - not always usable, but works for us

View File

@ -451,6 +451,7 @@ inline object get_python_state_dict() {
#endif #endif
if (!state_dict) { if (!state_dict) {
raise_from(PyExc_SystemError, "pybind11::detail::get_python_state_dict() FAILED"); raise_from(PyExc_SystemError, "pybind11::detail::get_python_state_dict() FAILED");
throw error_already_set();
} }
return state_dict; return state_dict;
} }
@ -463,6 +464,7 @@ inline internals **get_internals_pp_from_capsule(handle obj) {
void *raw_ptr = PyCapsule_GetPointer(obj.ptr(), /*name=*/nullptr); void *raw_ptr = PyCapsule_GetPointer(obj.ptr(), /*name=*/nullptr);
if (raw_ptr == nullptr) { if (raw_ptr == nullptr) {
raise_from(PyExc_SystemError, "pybind11::detail::get_internals_pp_from_capsule() FAILED"); raise_from(PyExc_SystemError, "pybind11::detail::get_internals_pp_from_capsule() FAILED");
throw error_already_set();
} }
return static_cast<internals **>(raw_ptr); return static_cast<internals **>(raw_ptr);
} }

View File

@ -123,6 +123,20 @@ inline numpy_internals &get_numpy_internals() {
return *ptr; return *ptr;
} }
PYBIND11_NOINLINE module_ import_numpy_core_submodule(const char *submodule_name) {
module_ numpy = module_::import("numpy");
str version_string = numpy.attr("__version__");
module_ numpy_lib = module_::import("numpy.lib");
object numpy_version = numpy_lib.attr("NumpyVersion")(version_string);
int major_version = numpy_version.attr("major").cast<int>();
/* `numpy.core` was renamed to `numpy._core` in NumPy 2.0 as it officially
became a private module. */
std::string numpy_core_path = major_version >= 2 ? "numpy._core" : "numpy.core";
return module_::import((numpy_core_path + "." + submodule_name).c_str());
}
template <typename T> template <typename T>
struct same_size { struct same_size {
template <typename U> template <typename U>
@ -280,9 +294,13 @@ private:
}; };
static npy_api lookup() { static npy_api lookup() {
module_ m = module_::import("numpy.core.multiarray"); module_ m = detail::import_numpy_core_submodule("multiarray");
auto c = m.attr("_ARRAY_API"); auto c = m.attr("_ARRAY_API");
void **api_ptr = (void **) PyCapsule_GetPointer(c.ptr(), nullptr); void **api_ptr = (void **) PyCapsule_GetPointer(c.ptr(), nullptr);
if (api_ptr == nullptr) {
raise_from(PyExc_SystemError, "FAILURE obtaining numpy _ARRAY_API pointer.");
throw error_already_set();
}
npy_api api; npy_api api;
#define DECL_NPY_API(Func) api.Func##_ = (decltype(api.Func##_)) api_ptr[API_##Func]; #define DECL_NPY_API(Func) api.Func##_ = (decltype(api.Func##_)) api_ptr[API_##Func];
DECL_NPY_API(PyArray_GetNDArrayCFeatureVersion); DECL_NPY_API(PyArray_GetNDArrayCFeatureVersion);
@ -775,11 +793,8 @@ public:
private: private:
static object _dtype_from_pep3118() { static object _dtype_from_pep3118() {
static PyObject *obj = module_::import("numpy.core._internal") module_ m = detail::import_numpy_core_submodule("_internal");
.attr("_dtype_from_pep3118") static PyObject *obj = m.attr("_dtype_from_pep3118").cast<object>().release().ptr();
.cast<object>()
.release()
.ptr();
return reinterpret_borrow<object>(obj); return reinterpret_borrow<object>(obj);
} }