Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9fa1186

Browse files
committed
Use pybind11's usual namespace alias in extensions
1 parent 1446d1c commit 9fa1186

File tree

5 files changed

+59
-52
lines changed

5 files changed

+59
-52
lines changed

src/_image_wrapper.cpp

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "_image_resample.h"
55
#include "py_converters_11.h"
66

7+
namespace py = pybind11;
8+
using namespace pybind11::literals;
79

810
/**********************************************************************
911
* Free functions
@@ -46,8 +48,8 @@ radius: float, default: 1
4648
)""";
4749

4850

49-
static pybind11::array_t<double>
50-
_get_transform_mesh(const pybind11::object& transform, const pybind11::ssize_t *dims)
51+
static py::array_t<double>
52+
_get_transform_mesh(const py::object& transform, const py::ssize_t *dims)
5153
{
5254
/* TODO: Could we get away with float, rather than double, arrays here? */
5355

@@ -58,8 +60,8 @@ _get_transform_mesh(const pybind11::object& transform, const pybind11::ssize_t *
5860
// If attribute doesn't exist, raises Python AttributeError
5961
auto inverse = transform.attr("inverted")();
6062

61-
pybind11::ssize_t mesh_dims[2] = {dims[0]*dims[1], 2};
62-
pybind11::array_t<double> input_mesh(mesh_dims);
63+
py::ssize_t mesh_dims[2] = {dims[0]*dims[1], 2};
64+
py::array_t<double> input_mesh(mesh_dims);
6365
auto p = input_mesh.mutable_data();
6466

6567
for (auto y = 0; y < dims[0]; ++y) {
@@ -72,7 +74,7 @@ _get_transform_mesh(const pybind11::object& transform, const pybind11::ssize_t *
7274
auto output_mesh = inverse.attr("transform")(input_mesh);
7375

7476
auto output_mesh_array =
75-
pybind11::array_t<double, pybind11::array::c_style | pybind11::array::forcecast>(output_mesh);
77+
py::array_t<double, py::array::c_style | py::array::forcecast>(output_mesh);
7678

7779
if (output_mesh_array.ndim() != 2) {
7880
throw std::runtime_error(
@@ -84,12 +86,12 @@ _get_transform_mesh(const pybind11::object& transform, const pybind11::ssize_t *
8486
}
8587

8688

87-
// Using generic pybind::array for input and output arrays rather than the more usual
88-
// pybind::array_t<type> as function supports multiple array dtypes.
89+
// Using generic py::array for input and output arrays rather than the more usual
90+
// py::array_t<type> as this function supports multiple array dtypes.
8991
static void
90-
image_resample(pybind11::array input_array,
91-
pybind11::array& output_array,
92-
const pybind11::object& transform,
92+
image_resample(py::array input_array,
93+
py::array& output_array,
94+
const py::object& transform,
9395
interpolation_e interpolation,
9496
bool resample_, // Avoid name clash with resample() function
9597
float alpha,
@@ -111,7 +113,7 @@ image_resample(pybind11::array input_array,
111113
}
112114

113115
// Ensure input array is contiguous, regardless of dtype
114-
input_array = pybind11::array::ensure(input_array, pybind11::array::c_style);
116+
input_array = py::array::ensure(input_array, py::array::c_style);
115117

116118
// Validate output array
117119
auto out_ndim = output_array.ndim();
@@ -132,7 +134,7 @@ image_resample(pybind11::array input_array,
132134
throw std::invalid_argument("Input and output arrays have mismatched types");
133135
}
134136

135-
if ((output_array.flags() & pybind11::array::c_style) == 0) {
137+
if ((output_array.flags() & py::array::c_style) == 0) {
136138
throw std::invalid_argument("Output array must be C-contiguous");
137139
}
138140

@@ -150,14 +152,14 @@ image_resample(pybind11::array input_array,
150152

151153
// Only used if transform is not affine.
152154
// Need to keep it in scope for the duration of this function.
153-
pybind11::array_t<double> transform_mesh;
155+
py::array_t<double> transform_mesh;
154156

155157
// Validate transform
156158
if (transform.is_none()) {
157159
params.is_affine = true;
158160
} else {
159161
// Raises Python AttributeError if no such attribute or TypeError if cast fails
160-
bool is_affine = pybind11::cast<bool>(transform.attr("is_affine"));
162+
bool is_affine = py::cast<bool>(transform.attr("is_affine"));
161163

162164
if (is_affine) {
163165
convert_trans_affine(transform, params.affine);
@@ -171,20 +173,20 @@ image_resample(pybind11::array input_array,
171173

172174
if (auto resampler =
173175
(ndim == 2) ? (
174-
(dtype.is(pybind11::dtype::of<std::uint8_t>())) ? resample<agg::gray8> :
175-
(dtype.is(pybind11::dtype::of<std::int8_t>())) ? resample<agg::gray8> :
176-
(dtype.is(pybind11::dtype::of<std::uint16_t>())) ? resample<agg::gray16> :
177-
(dtype.is(pybind11::dtype::of<std::int16_t>())) ? resample<agg::gray16> :
178-
(dtype.is(pybind11::dtype::of<float>())) ? resample<agg::gray32> :
179-
(dtype.is(pybind11::dtype::of<double>())) ? resample<agg::gray64> :
176+
(dtype.is(py::dtype::of<std::uint8_t>())) ? resample<agg::gray8> :
177+
(dtype.is(py::dtype::of<std::int8_t>())) ? resample<agg::gray8> :
178+
(dtype.is(py::dtype::of<std::uint16_t>())) ? resample<agg::gray16> :
179+
(dtype.is(py::dtype::of<std::int16_t>())) ? resample<agg::gray16> :
180+
(dtype.is(py::dtype::of<float>())) ? resample<agg::gray32> :
181+
(dtype.is(py::dtype::of<double>())) ? resample<agg::gray64> :
180182
nullptr) : (
181183
// ndim == 3
182-
(dtype.is(pybind11::dtype::of<std::uint8_t>())) ? resample<agg::rgba8> :
183-
(dtype.is(pybind11::dtype::of<std::int8_t>())) ? resample<agg::rgba8> :
184-
(dtype.is(pybind11::dtype::of<std::uint16_t>())) ? resample<agg::rgba16> :
185-
(dtype.is(pybind11::dtype::of<std::int16_t>())) ? resample<agg::rgba16> :
186-
(dtype.is(pybind11::dtype::of<float>())) ? resample<agg::rgba32> :
187-
(dtype.is(pybind11::dtype::of<double>())) ? resample<agg::rgba64> :
184+
(dtype.is(py::dtype::of<std::uint8_t>())) ? resample<agg::rgba8> :
185+
(dtype.is(py::dtype::of<std::int8_t>())) ? resample<agg::rgba8> :
186+
(dtype.is(py::dtype::of<std::uint16_t>())) ? resample<agg::rgba16> :
187+
(dtype.is(py::dtype::of<std::int16_t>())) ? resample<agg::rgba16> :
188+
(dtype.is(py::dtype::of<float>())) ? resample<agg::rgba32> :
189+
(dtype.is(py::dtype::of<double>())) ? resample<agg::rgba64> :
188190
nullptr)) {
189191
Py_BEGIN_ALLOW_THREADS
190192
resampler(
@@ -199,7 +201,7 @@ image_resample(pybind11::array input_array,
199201

200202

201203
PYBIND11_MODULE(_image, m) {
202-
pybind11::enum_<interpolation_e>(m, "_InterpolationType")
204+
py::enum_<interpolation_e>(m, "_InterpolationType")
203205
.value("NEAREST", NEAREST)
204206
.value("BILINEAR", BILINEAR)
205207
.value("BICUBIC", BICUBIC)
@@ -220,13 +222,13 @@ PYBIND11_MODULE(_image, m) {
220222
.export_values();
221223

222224
m.def("resample", &image_resample,
223-
pybind11::arg("input_array"),
224-
pybind11::arg("output_array"),
225-
pybind11::arg("transform"),
226-
pybind11::arg("interpolation") = interpolation_e::NEAREST,
227-
pybind11::arg("resample") = false,
228-
pybind11::arg("alpha") = 1,
229-
pybind11::arg("norm") = false,
230-
pybind11::arg("radius") = 1,
225+
"input_array"_a,
226+
"output_array"_a,
227+
"transform"_a,
228+
"interpolation"_a = interpolation_e::NEAREST,
229+
"resample"_a = false,
230+
"alpha"_a = 1,
231+
"norm"_a = false,
232+
"radius"_a = 1,
231233
image_resample__doc__);
232234
}

src/_ttconv.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <vector>
1313

1414
namespace py = pybind11;
15+
using namespace pybind11::literals;
1516

1617
/**
1718
* An implementation of TTStreamWriter that writes to a Python
@@ -75,10 +76,10 @@ PYBIND11_MODULE(_ttconv, m) {
7576
"fonts to Postscript Type 3, Postscript Type 42 and "
7677
"Pdf Type 3 fonts.";
7778
m.def("convert_ttf_to_ps", &convert_ttf_to_ps,
78-
py::arg("filename"),
79-
py::arg("output"),
80-
py::arg("fonttype"),
81-
py::arg("glyph_ids") = py::none(),
79+
"filename"_a,
80+
"output"_a,
81+
"fonttype"_a,
82+
"glyph_ids"_a = py::none(),
8283
"Converts the Truetype font into a Type 3 or Type 42 Postscript font, "
8384
"optionally subsetting the font to only the desired set of characters.\n"
8485
"\n"

src/py_converters_11.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#include "py_converters_11.h"
22

3-
void convert_trans_affine(const pybind11::object& transform, agg::trans_affine& affine)
3+
void convert_trans_affine(const py::object& transform, agg::trans_affine& affine)
44
{
55
// If None assume identity transform so leave affine unchanged
66
if (transform.is_none()) {
77
return;
88
}
99

10-
auto array = pybind11::array_t<double, pybind11::array::c_style>::ensure(transform);
10+
auto array = py::array_t<double, py::array::c_style>::ensure(transform);
1111
if (!array || array.ndim() != 2 || array.shape(0) != 3 || array.shape(1) != 3) {
1212
throw std::invalid_argument("Invalid affine transformation matrix");
1313
}

src/py_converters_11.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
#include <pybind11/pybind11.h>
77
#include <pybind11/numpy.h>
88

9+
namespace py = pybind11;
10+
911
#include "agg_trans_affine.h"
1012

11-
void convert_trans_affine(const pybind11::object& transform, agg::trans_affine& affine);
13+
void convert_trans_affine(const py::object& transform, agg::trans_affine& affine);
1214

1315
#endif

src/tri/_tri_wrapper.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "_tri.h"
22

3+
using namespace pybind11::literals;
4+
35
PYBIND11_MODULE(_tri, m) {
46
py::class_<Triangulation>(m, "Triangulation")
57
.def(py::init<const Triangulation::CoordinateArray&,
@@ -9,13 +11,13 @@ PYBIND11_MODULE(_tri, m) {
911
const Triangulation::EdgeArray&,
1012
const Triangulation::NeighborArray&,
1113
bool>(),
12-
py::arg("x"),
13-
py::arg("y"),
14-
py::arg("triangles"),
15-
py::arg("mask"),
16-
py::arg("edges"),
17-
py::arg("neighbors"),
18-
py::arg("correct_triangle_orientations"),
14+
"x"_a,
15+
"y"_a,
16+
"triangles"_a,
17+
"mask"_a,
18+
"edges"_a,
19+
"neighbors"_a,
20+
"correct_triangle_orientations"_a,
1921
"Create a new C++ Triangulation object.\n"
2022
"This should not be called directly, use the python class\n"
2123
"matplotlib.tri.Triangulation instead.\n")
@@ -31,8 +33,8 @@ PYBIND11_MODULE(_tri, m) {
3133
py::class_<TriContourGenerator>(m, "TriContourGenerator")
3234
.def(py::init<Triangulation&,
3335
const TriContourGenerator::CoordinateArray&>(),
34-
py::arg("triangulation"),
35-
py::arg("z"),
36+
"triangulation"_a,
37+
"z"_a,
3638
"Create a new C++ TriContourGenerator object.\n"
3739
"This should not be called directly, use the functions\n"
3840
"matplotlib.axes.tricontour and tricontourf instead.\n")
@@ -43,7 +45,7 @@ PYBIND11_MODULE(_tri, m) {
4345

4446
py::class_<TrapezoidMapTriFinder>(m, "TrapezoidMapTriFinder")
4547
.def(py::init<Triangulation&>(),
46-
py::arg("triangulation"),
48+
"triangulation"_a,
4749
"Create a new C++ TrapezoidMapTriFinder object.\n"
4850
"This should not be called directly, use the python class\n"
4951
"matplotlib.tri.TrapezoidMapTriFinder instead.\n")

0 commit comments

Comments
 (0)