From 1e171c820c5d663b2dfa415bf985040fd773bbbd Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Mon, 7 Feb 2022 11:42:47 -0600 Subject: [PATCH] pybind11 type_caster for queue/device convert Py_None to default constructed instances This automates interpretation of None as default-selected device/queue in pybin11 functions. For example, one can now default queue argument to None to allow it to use default-selected queue by default. --- dpctl/apis/include/dpctl4pybind11.hpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/dpctl/apis/include/dpctl4pybind11.hpp b/dpctl/apis/include/dpctl4pybind11.hpp index eb563b3f84..80d01af721 100644 --- a/dpctl/apis/include/dpctl4pybind11.hpp +++ b/dpctl/apis/include/dpctl4pybind11.hpp @@ -55,8 +55,12 @@ template <> struct type_caster value = *q; return true; } + else if (source == Py_None) { + value = sycl::queue{}; + return true; + } else { - throw std::runtime_error( + throw py::type_error( "Input is of unexpected type, expected dpctl.SyclQueue"); } } @@ -87,8 +91,12 @@ template <> struct type_caster value = *d; return true; } + else if (source == Py_None) { + value = sycl::device{}; + return true; + } else { - throw std::runtime_error( + throw py::type_error( "Input is of unexpected type, expected dpctl.SyclDevice"); } } @@ -120,7 +128,7 @@ template <> struct type_caster return true; } else { - throw std::runtime_error( + throw py::type_error( "Input is of unexpected type, expected dpctl.SyclContext"); } } @@ -153,7 +161,7 @@ template <> struct type_caster return true; } else { - throw std::runtime_error( + throw py::type_error( "Input is of unexpected type, expected dpctl.SyclEvent"); } }