Skip to content

Commit a9c94e6

Browse files
Change the code to expect new to throw bad_alloc on error.
1 parent 0f94dcd commit a9c94e6

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

dpctl/apis/include/dpctl4pybind11.hpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "dpctl_capi.h"
2929
#include <complex>
30+
#include <exception>
3031
#include <memory>
3132
#include <pybind11/pybind11.h>
3233
#include <sycl/sycl.hpp>
@@ -759,9 +760,6 @@ class usm_memory : public py::object
759760
auto const &api = ::dpctl::detail::dpctl_capi::get();
760761
DPCTLSyclUSMRef usm_ref = reinterpret_cast<DPCTLSyclUSMRef>(usm_ptr);
761762
sycl::queue *q_ptr = new sycl::queue(q);
762-
if (!q_ptr) {
763-
throw std::bad_alloc();
764-
}
765763
DPCTLSyclQueueRef QRef = reinterpret_cast<DPCTLSyclQueueRef>(q_ptr);
766764

767765
auto vacuous_destructor = []() {};
@@ -777,12 +775,17 @@ class usm_memory : public py::object
777775
0);
778776

779777
if (is_ok) {
780-
std::shared_ptr<void> *opaque_ptr =
781-
new std::shared_ptr<void>(shptr);
782-
if (!opaque_ptr) {
778+
std::shared_ptr<void> *opaque_ptr = nullptr;
779+
std::exception_ptr eptr;
780+
try {
781+
opaque_ptr = new std::shared_ptr<void>(shptr);
782+
} catch (const std::exception &e) {
783+
eptr = std::make_exception_ptr(e);
784+
}
785+
if (eptr) {
783786
Py_DECREF(_memory);
784787
delete q_ptr;
785-
throw std::bad_alloc();
788+
std::rethrow_exception(eptr);
786789
}
787790
Py_MemoryObject *memobj =
788791
reinterpret_cast<Py_MemoryObject *>(_memory);

0 commit comments

Comments
 (0)