Skip to content

Commit cdd9266

Browse files
committed
Use pybind11 from conda-forge
1 parent e53718c commit cdd9266

File tree

5 files changed

+174
-130
lines changed

5 files changed

+174
-130
lines changed

.appveyor.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ install:
2424
- conda info -a
2525
- conda install pytest -c conda-forge
2626
- cd test
27-
- conda install xtensor pytest numpy -c conda-forge
28-
- pip install pybind11==1.8.1
27+
- conda install xtensor==0.1.1 pytest numpy pybind11==1.8.1 -c conda-forge
2928
- xcopy /S %APPVEYOR_BUILD_FOLDER%\include %MINICONDA%\include
3029

3130
build_script:

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ install:
5959
# Useful for debugging any issues with conda
6060
- conda info -a
6161
- cd test
62-
- conda install xtensor pytest numpy -c conda-forge
63-
- pip install pybind11==1.8.1
62+
- conda install xtensor==0.1.1 pytest numpy pybind11==1.8.1 -c conda-forge
6463
- cp -r $TRAVIS_BUILD_DIR/include/* $HOME/miniconda/include/
6564

6665
script:

conda.recipe/meta.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ build:
1717

1818
requirements:
1919
run:
20-
- xtensor
20+
- xtensor ==0.1.1
2121
- pybind11 ==1.8.1
2222

2323
test:

include/xtensor-python/pyarray.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,9 @@ namespace xt
544544
{
545545
return nullptr;
546546
}
547-
API &api = lookup_api();
548-
PyObject *descr = api.PyArray_DescrFromType_(pybind11::detail::npy_format_descriptor<T>::value);
549-
PyObject *result = api.PyArray_FromAny_(ptr, descr, 0, 0, API::NPY_ENSURE_ARRAY_ | ExtraFlags, nullptr);
547+
API& api = lookup_api();
548+
PyObject* descr = api.PyArray_DescrFromType_(pybind11::detail::npy_format_descriptor<T>::value);
549+
PyObject* result = api.PyArray_FromAny_(ptr, descr, 0, 0, API::NPY_ENSURE_ARRAY_ | ExtraFlags, nullptr);
550550
if (!result)
551551
{
552552
PyErr_Clear();

include/xtensor-python/pybind11_backport.hpp

Lines changed: 168 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -6,139 +6,185 @@
66
All rights reserved. Use of this source code is governed by a
77
BSD-style license that can be found in the LICENSE file.
88
*/
9-
#pragma once
9+
#ifndef PYBIND_BACKPORT_HPP
10+
#define PYBIND_BACKPORT_HPP
1011

1112
#include <cstddef>
1213
#include <algorithm>
1314

1415
#include "pybind11/numpy.h"
1516

16-
namespace pybind11 { namespace backport {
17-
struct PyArray_Proxy {
18-
PyObject_HEAD
19-
char *data;
20-
int nd;
21-
ssize_t *dimensions;
22-
ssize_t *strides;
23-
PyObject *base;
24-
PyObject *descr;
25-
int flags;
26-
};
27-
28-
struct PyArrayDescr_Proxy {
29-
PyObject_HEAD
30-
PyObject *typeobj;
31-
char kind;
32-
char type;
33-
char byteorder;
34-
char flags;
35-
int type_num;
36-
int elsize;
37-
int alignment;
38-
char *subarray;
39-
PyObject *fields;
40-
PyObject *names;
41-
};
17+
namespace pybind11
18+
{
19+
namespace backport
20+
{
21+
struct PyArray_Proxy
22+
{
23+
PyObject_HEAD
24+
char* data;
25+
int nd;
26+
ssize_t* dimensions;
27+
ssize_t* strides;
28+
PyObject* base;
29+
PyObject* descr;
30+
int flags;
31+
};
32+
33+
struct PyArrayDescr_Proxy
34+
{
35+
PyObject_HEAD
36+
PyObject* typeobj;
37+
char kind;
38+
char type;
39+
char byteorder;
40+
char flags;
41+
int type_num;
42+
int elsize;
43+
int alignment;
44+
char* subarray;
45+
PyObject* fields;
46+
PyObject* names;
47+
};
4248

4349
#ifndef PyArray_GET_
44-
# define PyArray_GET_(ptr, attr) \
45-
(reinterpret_cast<::pybind11::backport::PyArray_Proxy*>(ptr)->attr)
50+
#define PyArray_GET_(ptr, attr) \
51+
(reinterpret_cast<::pybind11::backport::PyArray_Proxy*>(ptr)->attr)
4652
#endif
4753
#ifndef PyArrayDescr_GET_
48-
# define PyArrayDescr_GET_(ptr, attr) \
49-
(reinterpret_cast<::pybind11::backport::PyArrayDescr_Proxy*>(ptr)->attr)
54+
#define PyArrayDescr_GET_(ptr, attr) \
55+
(reinterpret_cast<::pybind11::backport::PyArrayDescr_Proxy*>(ptr)->attr)
5056
#endif
5157

52-
class array : public pybind11::array {
53-
public:
54-
using size_type = std::size_t;
58+
class array : public pybind11::array
59+
{
60+
public:
61+
using size_type = std::size_t;
62+
63+
using pybind11::array::array;
64+
65+
array() = default;
66+
67+
template<typename T>
68+
array(const std::vector<size_type>& shape,
69+
const std::vector<size_type>& strides,
70+
const T* ptr, handle base = {})
71+
: array(buffer_info(const_cast<T*>(ptr),
72+
sizeof(T),
73+
format_descriptor<T>::value,
74+
shape.size(), shape, strides))
75+
{
76+
if (base) throw std::runtime_error("array base is not supported yet");
77+
}
5578

56-
using pybind11::array::array;
79+
template<typename T>
80+
array(const std::vector<size_type> &shape,
81+
const T* ptr, handle base = {})
82+
: array(shape, default_strides(shape, sizeof(T)), ptr, base)
83+
{
84+
}
5785

58-
array() = default;
59-
60-
template<typename T> array(const std::vector<size_type>& shape,
61-
const std::vector<size_type>& strides,
62-
const T* ptr, handle base = {})
63-
: array(buffer_info(const_cast<T*>(ptr), sizeof(T), format_descriptor<T>::value,
64-
shape.size(), shape, strides)) {
65-
if (base) throw std::runtime_error("array base is not supported yet");
66-
}
67-
68-
template<typename T> array(const std::vector<size_type> &shape,
69-
const T *ptr, handle base = {})
70-
: array(shape, default_strides(shape, sizeof(T)), ptr, base) { }
71-
72-
template<typename T> array(size_type size, const T *ptr, handle base)
73-
: array(std::vector<size_type>{size}, ptr) { }
74-
75-
size_type size() const {
76-
return std::accumulate(shape(), shape() + ndim(), size_type{1}, std::multiplies<size_type>());
77-
}
78-
79-
size_type itemsize() const {
80-
return static_cast<size_type>(PyArrayDescr_GET_(PyArray_GET_(m_ptr, descr), elsize));
81-
}
82-
83-
size_type ndim() const {
84-
return static_cast<size_type>(PyArray_GET_(m_ptr, nd));
85-
}
86-
87-
const size_type* shape() const {
88-
return reinterpret_cast<const size_type *>(PyArray_GET_(m_ptr, dimensions));
89-
}
90-
91-
const size_type* strides() const {
92-
return reinterpret_cast<const size_type *>(PyArray_GET_(m_ptr, strides));
93-
}
94-
95-
template<typename... Ix> void* data() {
96-
return static_cast<void *>(PyArray_GET_(m_ptr, data));
97-
}
98-
99-
template<typename... Ix> void* mutable_data() {
100-
// check_writeable();
101-
return static_cast<void *>(PyArray_GET_(m_ptr, data));
102-
}
103-
104-
template<typename... Ix> size_type offset_at(Ix... index) const {
105-
if (sizeof...(index) > ndim())
106-
fail_dim_check(sizeof...(index), "too many indices for an array");
107-
return get_byte_offset(index...);
108-
}
109-
110-
size_type offset_at() const { return 0; }
111-
112-
protected:
113-
void fail_dim_check(size_type dim, const std::string& msg) const {
114-
throw index_error(msg + ": " + std::to_string(dim) +
115-
" (ndim = " + std::to_string(ndim()) + ")");
116-
}
117-
118-
template<typename... Ix> size_type get_byte_offset(Ix... index) const {
119-
const size_type idx[] = { static_cast<size_type>(index)... };
120-
if (!std::equal(idx + 0, idx + sizeof...(index), shape(), std::less<size_type>{})) {
121-
auto mismatch = std::mismatch(idx + 0, idx + sizeof...(index), shape(), std::less<size_type>{});
122-
throw index_error(std::string("index ") + std::to_string(*mismatch.first) +
123-
" is out of bounds for axis " + std::to_string(mismatch.first - idx) +
124-
" with size " + std::to_string(*mismatch.second));
125-
}
126-
return std::inner_product(idx + 0, idx + sizeof...(index), strides(), size_type{0});
127-
}
128-
129-
size_type get_byte_offset() const { return 0; }
130-
131-
static std::vector<size_type> default_strides(const std::vector<size_type>& shape,
132-
size_type itemsize) {
133-
auto ndim = shape.size();
134-
std::vector<size_type> strides(ndim);
135-
if (ndim) {
136-
std::fill(strides.begin(), strides.end(), itemsize);
137-
for (size_type i = 0; i < ndim - 1; i++)
138-
for (size_type j = 0; j < ndim - 1 - i; j++)
139-
strides[j] *= shape[ndim - 1 - i];
140-
}
141-
return strides;
142-
}
143-
};
144-
}} // namespace pybind11::backport
86+
template<typename T>
87+
array(size_type size, const T *ptr, handle base)
88+
: array(std::vector<size_type>{size}, ptr)
89+
{
90+
}
91+
92+
size_type size() const
93+
{
94+
return std::accumulate(shape(), shape() + ndim(), size_type{1}, std::multiplies<size_type>());
95+
}
96+
97+
size_type itemsize() const
98+
{
99+
return static_cast<size_type>(PyArrayDescr_GET_(PyArray_GET_(m_ptr, descr), elsize));
100+
}
101+
102+
size_type ndim() const
103+
{
104+
return static_cast<size_type>(PyArray_GET_(m_ptr, nd));
105+
}
106+
107+
const size_type* shape() const
108+
{
109+
return reinterpret_cast<const size_type*>(PyArray_GET_(m_ptr, dimensions));
110+
}
111+
112+
const size_type* strides() const
113+
{
114+
return reinterpret_cast<const size_type*>(PyArray_GET_(m_ptr, strides));
115+
}
116+
117+
template<typename... Ix>
118+
void* data()
119+
{
120+
return static_cast<void*>(PyArray_GET_(m_ptr, data));
121+
}
122+
123+
template<typename... Ix>
124+
void* mutable_data()
125+
{
126+
// check_writeable();
127+
return static_cast<void *>(PyArray_GET_(m_ptr, data));
128+
}
129+
130+
template<typename... Ix>
131+
size_type offset_at(Ix... index) const
132+
{
133+
if (sizeof...(index) > ndim())
134+
{
135+
fail_dim_check(sizeof...(index), "too many indices for an array");
136+
}
137+
return get_byte_offset(index...);
138+
}
139+
140+
size_type offset_at() const
141+
{
142+
return 0;
143+
}
144+
145+
protected:
146+
147+
void fail_dim_check(size_type dim, const std::string& msg) const
148+
{
149+
throw index_error(msg + ": " + std::to_string(dim) +
150+
" (ndim = " + std::to_string(ndim()) + ")");
151+
}
152+
153+
template<typename... Ix>
154+
size_type get_byte_offset(Ix... index) const
155+
{
156+
const size_type idx[] = { static_cast<size_type>(index)... };
157+
if (!std::equal(idx + 0, idx + sizeof...(index), shape(), std::less<size_type>{}))
158+
{
159+
auto mismatch = std::mismatch(idx + 0, idx + sizeof...(index), shape(), std::less<size_type>{});
160+
throw index_error(std::string("index ") + std::to_string(*mismatch.first) +
161+
" is out of bounds for axis " + std::to_string(mismatch.first - idx) +
162+
" with size " + std::to_string(*mismatch.second));
163+
}
164+
return std::inner_product(idx + 0, idx + sizeof...(index), strides(), size_type{0});
165+
}
166+
167+
size_type get_byte_offset() const
168+
{
169+
return 0;
170+
}
171+
172+
static std::vector<size_type>
173+
default_strides(const std::vector<size_type>& shape, size_type itemsize)
174+
{
175+
auto ndim = shape.size();
176+
std::vector<size_type> strides(ndim);
177+
if (ndim)
178+
{
179+
std::fill(strides.begin(), strides.end(), itemsize);
180+
for (size_type i = 0; i < ndim - 1; i++)
181+
for (size_type j = 0; j < ndim - 1 - i; j++)
182+
strides[j] *= shape[ndim - 1 - i];
183+
}
184+
return strides;
185+
}
186+
};
187+
}
188+
}
189+
190+
#endif

0 commit comments

Comments
 (0)