Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: better exception and error handling for capsules #3825

Merged
merged 2 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/pybind11/numpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,8 @@ struct npy_format_descriptor<T, enable_if_t<array_info<T>::is_array>> {
static pybind11::dtype dtype() {
list shape;
array_info<T>::append_extents(shape);
return pybind11::dtype::from_args(pybind11::make_tuple(base_descr::dtype(), shape));
return pybind11::dtype::from_args(
pybind11::make_tuple(base_descr::dtype(), std::move(shape)));
}
};

Expand Down
32 changes: 19 additions & 13 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -1574,42 +1574,50 @@ class capsule : public object {
void (*destructor)(PyObject *) = nullptr)
: object(PyCapsule_New(const_cast<void *>(value), name, destructor), stolen_t{}) {
if (!m_ptr) {
pybind11_fail("Could not allocate capsule object!");
throw error_already_set();
}
}

PYBIND11_DEPRECATED("Please pass a destructor that takes a void pointer as input")
capsule(const void *value, void (*destruct)(PyObject *))
: object(PyCapsule_New(const_cast<void *>(value), nullptr, destruct), stolen_t{}) {
if (!m_ptr) {
pybind11_fail("Could not allocate capsule object!");
throw error_already_set();
}
}

capsule(const void *value, void (*destructor)(void *)) {
m_ptr = PyCapsule_New(const_cast<void *>(value), nullptr, [](PyObject *o) {
auto destructor = reinterpret_cast<void (*)(void *)>(PyCapsule_GetContext(o));
if (destructor == nullptr) {
if (PyErr_Occurred()) {
throw error_already_set();
}
pybind11_fail("Unable to get capsule context");
}
void *ptr = PyCapsule_GetPointer(o, nullptr);
if (ptr == nullptr) {
throw error_already_set();
}
destructor(ptr);
});

if (!m_ptr) {
pybind11_fail("Could not allocate capsule object!");
}

if (PyCapsule_SetContext(m_ptr, (void *) destructor) != 0) {
pybind11_fail("Could not set capsule context!");
if (!m_ptr || PyCapsule_SetContext(m_ptr, (void *) destructor) != 0) {
throw error_already_set();
}
}

explicit capsule(void (*destructor)()) {
m_ptr = PyCapsule_New(reinterpret_cast<void *>(destructor), nullptr, [](PyObject *o) {
auto destructor = reinterpret_cast<void (*)()>(PyCapsule_GetPointer(o, nullptr));
if (destructor == nullptr) {
throw error_already_set();
}
destructor();
});

if (!m_ptr) {
pybind11_fail("Could not allocate capsule object!");
throw error_already_set();
}
}

Expand All @@ -1624,17 +1632,15 @@ class capsule : public object {
const auto *name = this->name();
T *result = static_cast<T *>(PyCapsule_GetPointer(m_ptr, name));
if (!result) {
PyErr_Clear();
pybind11_fail("Unable to extract capsule contents!");
throw error_already_set();
}
return result;
}

/// Replaces a capsule's pointer *without* calling the destructor on the existing one.
void set_pointer(const void *value) {
if (PyCapsule_SetPointer(m_ptr, const_cast<void *>(value)) != 0) {
PyErr_Clear();
pybind11_fail("Could not set capsule pointer");
throw error_already_set();
}
}

Expand Down