Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
1f54cd92
Commit
1f54cd92
authored
Jun 22, 2016
by
Ivan Smirnov
Browse files
Use object instead of ptrs in numpy descriptors
parent
2a7acb6d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
11 deletions
+12
-11
include/pybind11/numpy.h
include/pybind11/numpy.h
+12
-11
No files found.
include/pybind11/numpy.h
View file @
1f54cd92
...
...
@@ -96,7 +96,7 @@ public:
template
<
typename
Type
>
array
(
size_t
size
,
const
Type
*
ptr
)
{
API
&
api
=
lookup_api
();
PyObject
*
descr
=
object
(
detail
::
npy_format_descriptor
<
Type
>::
descr
()
,
true
)
.
release
().
ptr
();
PyObject
*
descr
=
detail
::
npy_format_descriptor
<
Type
>::
descr
().
release
().
ptr
();
Py_intptr_t
shape
=
(
Py_intptr_t
)
size
;
object
tmp
=
object
(
api
.
PyArray_NewFromDescr_
(
api
.
PyArray_Type_
,
descr
,
1
,
&
shape
,
nullptr
,
(
void
*
)
ptr
,
0
,
nullptr
),
false
);
...
...
@@ -147,7 +147,7 @@ public:
if
(
ptr
==
nullptr
)
return
nullptr
;
API
&
api
=
lookup_api
();
PyObject
*
descr
=
object
(
detail
::
npy_format_descriptor
<
T
>::
descr
()
,
true
)
.
release
().
ptr
();
PyObject
*
descr
=
detail
::
npy_format_descriptor
<
T
>::
descr
().
release
().
ptr
();
PyObject
*
result
=
api
.
PyArray_FromAny_
(
ptr
,
descr
,
0
,
0
,
API
::
NPY_ENSURE_ARRAY_
|
ExtraFlags
,
nullptr
);
if
(
!
result
)
PyErr_Clear
();
...
...
@@ -178,8 +178,8 @@ private:
array
::
API
::
NPY_INT_
,
array
::
API
::
NPY_UINT_
,
array
::
API
::
NPY_LONGLONG_
,
array
::
API
::
NPY_ULONGLONG_
};
public:
static
int
typenum
()
{
return
values
[
detail
::
log2
(
sizeof
(
T
))
*
2
+
(
std
::
is_unsigned
<
T
>::
value
?
1
:
0
)];
}
static
PyO
bject
*
descr
()
{
if
(
auto
obj
=
array
::
lookup_api
().
PyArray_DescrFromType_
(
typenum
()))
return
obj
;
static
py
::
o
bject
descr
()
{
if
(
auto
ptr
=
array
::
lookup_api
().
PyArray_DescrFromType_
(
typenum
()))
return
py
::
object
(
ptr
,
true
)
;
else
pybind11_fail
(
"Unsupported buffer format!"
);
}
template
<
typename
T2
=
T
,
typename
std
::
enable_if
<
std
::
is_signed
<
T2
>
::
value
,
int
>::
type
=
0
>
...
...
@@ -192,8 +192,8 @@ template <typename T> constexpr const int npy_format_descriptor<
#define DECL_FMT(Type, NumPyName, Name) template<> struct npy_format_descriptor<Type> { \
static int typenum() { return array::API::NumPyName; } \
static
PyO
bject
*
descr() { \
if (auto
obj
= array::lookup_api().PyArray_DescrFromType_(typenum())) return
obj
; \
static
py::o
bject descr() {
\
if (auto
ptr
= array::lookup_api().PyArray_DescrFromType_(typenum())) return
py::object(ptr, true)
; \
else pybind11_fail("Unsupported buffer format!"); \
} \
static PYBIND11_DESCR name() { return _(Name); } }
...
...
@@ -207,7 +207,7 @@ DECL_FMT(std::complex<double>, NPY_CDOUBLE_, "complex128");
struct
field_descriptor
{
const
char
*
name
;
int
offset
;
PyO
bject
*
descr
;
py
::
o
bject
descr
;
};
template
<
typename
T
>
struct
npy_format_descriptor
...
...
@@ -220,10 +220,10 @@ template <typename T> struct npy_format_descriptor
{
static
PYBIND11_DESCR
name
()
{
return
_
(
"user-defined"
);
}
static
PyO
bject
*
descr
()
{
static
py
::
o
bject
descr
()
{
if
(
!
descr_
())
pybind11_fail
(
"NumPy: unsupported buffer format!"
);
return
descr_
(
);
return
py
::
object
(
descr_
(),
true
);
}
static
const
char
*
format_str
()
{
...
...
@@ -239,7 +239,7 @@ template <typename T> struct npy_format_descriptor
pybind11_fail
(
"NumPy: unsupported field dtype"
);
names
.
append
(
py
::
str
(
field
.
name
));
offsets
.
append
(
py
::
int_
(
field
.
offset
));
formats
.
append
(
object
(
field
.
descr
,
true
)
);
formats
.
append
(
field
.
descr
);
}
args
[
"names"
]
=
names
;
args
[
"offsets"
]
=
offsets
;
...
...
@@ -265,7 +265,8 @@ private:
#define FIELD_DESCRIPTOR(Type, Field) \
::pybind11::detail::field_descriptor { \
#Field, offsetof(Type, Field), \
::pybind11::detail::npy_format_descriptor<decltype(static_cast<Type*>(0)->Field)>::descr() }
::pybind11::detail::npy_format_descriptor<decltype(static_cast<Type*>(0)->Field)>::descr() \
}
// The main idea of this macro is borrowed from https://github.com/swansontec/map-macro
// (C) William Swanson, Paul Fultz
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment