Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11
Commits
7bf90e80
Commit
7bf90e80
authored
Oct 20, 2016
by
Ivan Smirnov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a direct converter for numpy scalars
parent
c275ee6b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
0 deletions
+26
-0
include/pybind11/numpy.h
+26
-0
No files found.
include/pybind11/numpy.h
View file @
7bf90e80
...
@@ -63,6 +63,14 @@ struct PyArray_Proxy {
...
@@ -63,6 +63,14 @@ struct PyArray_Proxy {
int
flags
;
int
flags
;
};
};
struct
PyVoidScalarObject_Proxy
{
PyObject_VAR_HEAD
char
*
obval
;
PyArrayDescr_Proxy
*
descr
;
int
flags
;
PyObject
*
base
;
};
struct
npy_api
{
struct
npy_api
{
enum
constants
{
enum
constants
{
NPY_C_CONTIGUOUS_
=
0x0001
,
NPY_C_CONTIGUOUS_
=
0x0001
,
...
@@ -702,11 +710,29 @@ struct npy_format_descriptor<T, enable_if_t<is_pod_struct<T>::value>> {
...
@@ -702,11 +710,29 @@ struct npy_format_descriptor<T, enable_if_t<is_pod_struct<T>::value>> {
auto
arr
=
array
(
buffer_info
(
nullptr
,
sizeof
(
T
),
format
(),
1
));
auto
arr
=
array
(
buffer_info
(
nullptr
,
sizeof
(
T
),
format
(),
1
));
if
(
!
api
.
PyArray_EquivTypes_
(
dtype_ptr
,
arr
.
dtype
().
ptr
()))
if
(
!
api
.
PyArray_EquivTypes_
(
dtype_ptr
,
arr
.
dtype
().
ptr
()))
pybind11_fail
(
"NumPy: invalid buffer descriptor!"
);
pybind11_fail
(
"NumPy: invalid buffer descriptor!"
);
register_direct_converter
();
}
}
private
:
private
:
static
std
::
string
format_str
;
static
std
::
string
format_str
;
static
PyObject
*
dtype_ptr
;
static
PyObject
*
dtype_ptr
;
static
void
register_direct_converter
()
{
auto
converter
=
[
=
](
PyObject
*
obj
,
void
*&
value
)
{
auto
&
api
=
npy_api
::
get
();
if
(
!
PyObject_TypeCheck
(
obj
,
api
.
PyVoidArrType_Type_
))
return
false
;
if
(
auto
descr
=
object
(
api
.
PyArray_DescrFromScalar_
(
obj
),
false
))
{
if
(
api
.
PyArray_EquivTypes_
(
dtype_ptr
,
descr
.
ptr
()))
{
value
=
((
PyVoidScalarObject_Proxy
*
)
obj
)
->
obval
;
return
true
;
}
}
return
false
;
};
get_internals
().
direct_conversions
[
std
::
type_index
(
typeid
(
T
))].
push_back
(
converter
);
}
};
};
template
<
typename
T
>
template
<
typename
T
>
...
...
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