Skip to content

Commit 68ac0e1

Browse files
committed
move py::object, ditch some needles try-catch blocks
1 parent a3328ba commit 68ac0e1

3 files changed

Lines changed: 14 additions & 32 deletions

File tree

include/openPMD/binding/python/Numpy.hpp

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ inline Datatype dtype_from_numpy(pybind11::dtype const dt)
9898
}
9999
}
100100

101-
inline Datatype dtype_from_numpy(pybind11::object const &dt)
101+
inline Datatype dtype_from_numpy(pybind11::object dt)
102102
{
103103
if (py::isinstance<pybind11::dtype>(dt))
104104
{
105-
return dtype_from_numpy(py::cast<pybind11::dtype>(dt));
105+
return dtype_from_numpy(py::cast<pybind11::dtype>(std::move(dt)));
106106
}
107107
else
108108
{
@@ -119,22 +119,11 @@ inline Datatype dtype_from_numpy(pybind11::object const &dt)
119119
"datatype without numpy, failed importing: ") +
120120
e.what());
121121
}
122-
pybind11::object create_dtype;
123-
try
124-
{
125-
create_dtype = numpy.attr("dtype");
126-
}
127-
catch (std::exception const &e)
128-
{
129-
throw std::runtime_error(
130-
std::string(
131-
"dtype_from_numpy: Failed to access numpy.dtype: ") +
132-
e.what());
133-
}
122+
pybind11::object create_dtype = numpy.attr("dtype");
134123
pybind11::object dtype_obj;
135124
try
136125
{
137-
dtype_obj = create_dtype(dt);
126+
dtype_obj = create_dtype(std::move(dt));
138127
}
139128
catch (std::exception const &e)
140129
{
@@ -143,16 +132,7 @@ inline Datatype dtype_from_numpy(pybind11::object const &dt)
143132
pybind11::str(dt).cast<std::string>() +
144133
std::string(", error: ") + e.what());
145134
}
146-
try
147-
{
148-
return dtype_from_numpy(py::cast<pybind11::dtype>(dtype_obj));
149-
}
150-
catch (std::exception const &e)
151-
{
152-
throw std::runtime_error(
153-
std::string("dtype_from_numpy: Failed to cast to dtype: ") +
154-
e.what());
155-
}
135+
return dtype_from_numpy(py::cast<pybind11::dtype>(dtype_obj));
156136
}
157137
}
158138

src/binding/python/Dataset.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "openPMD/binding/python/auxiliary.hpp"
2626

2727
#include <string>
28+
#include <utility>
2829

2930
void init_Dataset(py::module &m)
3031
{
@@ -37,11 +38,10 @@ void init_Dataset(py::module &m)
3738
py::arg("extent"),
3839
py::arg("options") = "{}")
3940
.def(
40-
py::init(
41-
[](py::object const &dt, Extent e, std::string options) {
42-
auto const d = dtype_from_numpy(dt);
43-
return new Dataset{d, std::move(e), std::move(options)};
44-
}),
41+
py::init([](py::object dt, Extent e, std::string options) {
42+
auto const d = dtype_from_numpy(std::move(dt));
43+
return new Dataset{d, std::move(e), std::move(options)};
44+
}),
4545
py::arg("dtype"),
4646
py::arg("extent"),
4747
py::arg("options") = "{}")

src/binding/python/RecordComponent.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include <string>
5151
#include <tuple>
5252
#include <type_traits>
53+
#include <utility>
5354
#include <vector>
5455

5556
/** Convert a py::tuple of py::slices to Offset & Extent
@@ -995,9 +996,10 @@ void init_RecordComponent(py::module &m)
995996
.def(
996997
"make_empty",
997998
[](RecordComponent &rc,
998-
pybind11::object const &dt,
999+
pybind11::object dt,
9991000
uint8_t dimensionality) {
1000-
return rc.makeEmpty(dtype_from_numpy(dt), dimensionality);
1001+
return rc.makeEmpty(
1002+
dtype_from_numpy(std::move(dt)), dimensionality);
10011003
})
10021004

10031005
// deprecated: pass-through C++ API

0 commit comments

Comments
 (0)