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
cbbb7830
Commit
cbbb7830
authored
Oct 20, 2016
by
Ivan Smirnov
Browse files
Add a test for NumPy scalar conversion
parent
85e16262
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
0 deletions
+33
-0
tests/test_numpy_dtypes.cpp
tests/test_numpy_dtypes.cpp
+11
-0
tests/test_numpy_dtypes.py
tests/test_numpy_dtypes.py
+22
-0
No files found.
tests/test_numpy_dtypes.cpp
View file @
cbbb7830
...
@@ -298,6 +298,9 @@ test_initializer numpy_dtypes([](py::module &m) {
...
@@ -298,6 +298,9 @@ test_initializer numpy_dtypes([](py::module &m) {
return
;
return
;
}
}
// typeinfo may be registered before the dtype descriptor for scalar casts to work...
py
::
class_
<
SimpleStruct
>
(
m
,
"SimpleStruct"
);
PYBIND11_NUMPY_DTYPE
(
SimpleStruct
,
x
,
y
,
z
);
PYBIND11_NUMPY_DTYPE
(
SimpleStruct
,
x
,
y
,
z
);
PYBIND11_NUMPY_DTYPE
(
PackedStruct
,
x
,
y
,
z
);
PYBIND11_NUMPY_DTYPE
(
PackedStruct
,
x
,
y
,
z
);
PYBIND11_NUMPY_DTYPE
(
NestedStruct
,
a
,
b
);
PYBIND11_NUMPY_DTYPE
(
NestedStruct
,
a
,
b
);
...
@@ -306,6 +309,11 @@ test_initializer numpy_dtypes([](py::module &m) {
...
@@ -306,6 +309,11 @@ test_initializer numpy_dtypes([](py::module &m) {
PYBIND11_NUMPY_DTYPE
(
StringStruct
,
a
,
b
);
PYBIND11_NUMPY_DTYPE
(
StringStruct
,
a
,
b
);
PYBIND11_NUMPY_DTYPE
(
EnumStruct
,
e1
,
e2
);
PYBIND11_NUMPY_DTYPE
(
EnumStruct
,
e1
,
e2
);
// ... or after...
py
::
class_
<
PackedStruct
>
(
m
,
"PackedStruct"
);
// ... or not at all
m
.
def
(
"create_rec_simple"
,
&
create_recarray
<
SimpleStruct
>
);
m
.
def
(
"create_rec_simple"
,
&
create_recarray
<
SimpleStruct
>
);
m
.
def
(
"create_rec_packed"
,
&
create_recarray
<
PackedStruct
>
);
m
.
def
(
"create_rec_packed"
,
&
create_recarray
<
PackedStruct
>
);
m
.
def
(
"create_rec_nested"
,
&
create_nested
);
m
.
def
(
"create_rec_nested"
,
&
create_nested
);
...
@@ -324,6 +332,9 @@ test_initializer numpy_dtypes([](py::module &m) {
...
@@ -324,6 +332,9 @@ test_initializer numpy_dtypes([](py::module &m) {
m
.
def
(
"test_array_ctors"
,
&
test_array_ctors
);
m
.
def
(
"test_array_ctors"
,
&
test_array_ctors
);
m
.
def
(
"test_dtype_ctors"
,
&
test_dtype_ctors
);
m
.
def
(
"test_dtype_ctors"
,
&
test_dtype_ctors
);
m
.
def
(
"test_dtype_methods"
,
&
test_dtype_methods
);
m
.
def
(
"test_dtype_methods"
,
&
test_dtype_methods
);
m
.
def
(
"f_simple"
,
[](
SimpleStruct
s
)
{
return
s
.
y
*
10
;
});
m
.
def
(
"f_packed"
,
[](
PackedStruct
s
)
{
return
s
.
y
*
10
;
});
m
.
def
(
"f_nested"
,
[](
NestedStruct
s
)
{
return
s
.
a
.
y
*
10
;
});
});
});
#undef PYBIND11_PACKED
#undef PYBIND11_PACKED
tests/test_numpy_dtypes.py
View file @
cbbb7830
...
@@ -174,3 +174,25 @@ def test_signature(doc):
...
@@ -174,3 +174,25 @@ def test_signature(doc):
from
pybind11_tests
import
create_rec_nested
from
pybind11_tests
import
create_rec_nested
assert
doc
(
create_rec_nested
)
==
"create_rec_nested(arg0: int) -> numpy.ndarray[NestedStruct]"
assert
doc
(
create_rec_nested
)
==
"create_rec_nested(arg0: int) -> numpy.ndarray[NestedStruct]"
@
pytest
.
requires_numpy
def
test_scalar_conversion
():
from
pybind11_tests
import
(
create_rec_simple
,
f_simple
,
create_rec_packed
,
f_packed
,
create_rec_nested
,
f_nested
,
create_enum_array
)
n
=
3
arrays
=
[
create_rec_simple
(
n
),
create_rec_packed
(
n
),
create_rec_nested
(
n
),
create_enum_array
(
n
)]
funcs
=
[
f_simple
,
f_packed
,
f_nested
]
for
i
,
func
in
enumerate
(
funcs
):
for
j
,
arr
in
enumerate
(
arrays
):
if
i
==
j
:
assert
[
func
(
arr
[
k
])
for
k
in
range
(
n
)]
==
[
k
*
10
for
k
in
range
(
n
)]
else
:
with
pytest
.
raises
(
TypeError
)
as
excinfo
:
func
(
arr
[
0
])
assert
'incompatible function arguments'
in
str
(
excinfo
.
value
)
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