Skip to content

Commit 61c367a

Browse files
Simplified impl of tensor::usm_ndarray and memory::usm_memory classes
Used fix contributed to pybind11 that is part of pybind11 2.10 release
1 parent 9b6b413 commit 61c367a

File tree

1 file changed

+10
-93
lines changed

1 file changed

+10
-93
lines changed

dpctl/apis/include/dpctl4pybind11.hpp

Lines changed: 10 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -305,55 +305,13 @@ namespace memory
305305
class usm_memory : public py::object
306306
{
307307
public:
308-
// Use macro once Pybind11 2.9.3 is released instead of code bewteen
309-
// START_TOKEN and END_TOKEN
310-
/*
311-
PYBIND11_OBJECT_CVT(
312-
usm_memory,
313-
py::object,
314-
[](PyObject *o) -> bool{ return PyObject_TypeCheck(o, &Py_MemoryType)
315-
!= 0;},
316-
[](PyObject *o) -> PyObject* { return as_usm_memory(o); }
317-
)
318-
*/
319-
// START_TOKEN
320-
321-
// these constructors do not validate, but since borrowed_t and stolen_t are
322-
// protected struct members of the object, they can only be called
323-
// internally.
324-
usm_memory(py::handle h, borrowed_t) : py::object(h, borrowed_t{}) {}
325-
usm_memory(py::handle h, stolen_t) : py::object(h, stolen_t{}) {}
326-
327-
static bool check_(py::handle h)
328-
{
329-
return h.ptr() != nullptr &&
330-
PyObject_TypeCheck(h.ptr(), &Py_MemoryType);
331-
}
332-
333-
template <typename Policy_>
334-
/* NOLINTNEXTLINE(google-explicit-constructor) */
335-
usm_memory(const py::detail::accessor<Policy_> &a)
336-
: usm_memory(py::object(a))
337-
{
338-
}
339-
340-
usm_memory(const py::object &o)
341-
: py::object(check_(o) ? o.inc_ref().ptr() : as_usm_memory(o.ptr()),
342-
stolen_t{})
343-
{
344-
if (!m_ptr)
345-
throw py::error_already_set();
346-
}
347-
348-
/* NOLINTNEXTLINE(google-explicit-constructor) */
349-
usm_memory(py::object &&o)
350-
: py::object(check_(o) ? o.release().ptr() : as_usm_memory(o.ptr()),
351-
stolen_t{})
352-
{
353-
if (!m_ptr)
354-
throw py::error_already_set();
355-
}
356-
// END_TOKEN
308+
PYBIND11_OBJECT_CVT(
309+
usm_memory,
310+
py::object,
311+
[](PyObject *o) -> bool {
312+
return PyObject_TypeCheck(o, &Py_MemoryType) != 0;
313+
},
314+
[](PyObject *o) -> PyObject * { return as_usm_memory(o); })
357315

358316
usm_memory()
359317
: py::object(::dpctl::detail::dpctl_api::get().default_usm_memory_(),
@@ -412,50 +370,9 @@ namespace tensor
412370
class usm_ndarray : public py::object
413371
{
414372
public:
415-
// In Pybind11 2.9.3 replace code between START_TOKEN and END_TOKEN with
416-
// macro
417-
/*
418-
PYBIND11_OBJECT(
419-
usm_ndarray,
420-
py::object,
421-
[](PyObject *o) -> bool {return PyObject_TypeCheck(o, &PyUSMArrayType)
422-
!= 0;}
423-
)
424-
*/
425-
426-
// START_TOKEN
427-
static bool check_(py::handle h)
428-
{
429-
return h.ptr() != nullptr &&
430-
PyObject_TypeCheck(h.ptr(), &PyUSMArrayType);
431-
}
432-
433-
// these constructors do not validate, but since borrowed_t and stolen_t are
434-
// protected struct members of the object, they can only be called
435-
// internally.
436-
usm_ndarray(py::handle h, borrowed_t) : py::object(h, borrowed_t{}) {}
437-
usm_ndarray(py::handle h, stolen_t) : py::object(h, stolen_t{}) {}
438-
439-
template <typename Policy_>
440-
/* NOLINTNEXTLINE(google-explicit-constructor) */
441-
usm_ndarray(const py::detail::accessor<Policy_> &a)
442-
: usm_ndarray(py::object(a))
443-
{
444-
}
445-
446-
usm_ndarray(const py::object &o) : py::object(o)
447-
{
448-
if (m_ptr && !check_(m_ptr))
449-
throw PYBIND11_OBJECT_CHECK_FAILED(usm_ndarray, m_ptr);
450-
}
451-
452-
/* NOLINTNEXTLINE(google-explicit-constructor) */
453-
usm_ndarray(py::object &&o) : py::object(std::move(o))
454-
{
455-
if (m_ptr && !check_(m_ptr))
456-
throw PYBIND11_OBJECT_CHECK_FAILED(usm_ndarray, m_ptr);
457-
}
458-
// END_TOKEN
373+
PYBIND11_OBJECT(usm_ndarray, py::object, [](PyObject *o) -> bool {
374+
return PyObject_TypeCheck(o, &PyUSMArrayType) != 0;
375+
})
459376

460377
usm_ndarray()
461378
: py::object(::dpctl::detail::dpctl_api::get().default_usm_ndarray_(),

0 commit comments

Comments
 (0)