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
669e1426
Commit
669e1426
authored
Jun 21, 2016
by
Ivan Smirnov
Browse files
Add test for a function accepting recarray (WIP)
parent
f5b166d0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
1 deletion
+31
-1
example/example20.cpp
example/example20.cpp
+22
-0
example/example20.py
example/example20.py
+9
-1
No files found.
example/example20.cpp
View file @
669e1426
...
@@ -21,17 +21,29 @@ struct Struct {
...
@@ -21,17 +21,29 @@ struct Struct {
float
z
;
float
z
;
};
};
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
Struct
&
v
)
{
return
os
<<
v
.
x
<<
","
<<
v
.
y
<<
","
<<
v
.
z
;
}
struct
PackedStruct
{
struct
PackedStruct
{
bool
x
;
bool
x
;
uint32_t
y
;
uint32_t
y
;
float
z
;
float
z
;
}
__attribute__
((
packed
));
}
__attribute__
((
packed
));
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
PackedStruct
&
v
)
{
return
os
<<
v
.
x
<<
","
<<
v
.
y
<<
","
<<
v
.
z
;
}
struct
NestedStruct
{
struct
NestedStruct
{
Struct
a
;
Struct
a
;
PackedStruct
b
;
PackedStruct
b
;
}
__attribute__
((
packed
));
}
__attribute__
((
packed
));
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
NestedStruct
&
v
)
{
return
os
<<
v
.
a
<<
"|"
<<
v
.
b
;
}
template
<
typename
T
>
template
<
typename
T
>
py
::
array
mkarray_via_buffer
(
size_t
n
)
{
py
::
array
mkarray_via_buffer
(
size_t
n
)
{
return
py
::
array
(
py
::
buffer_info
(
nullptr
,
sizeof
(
T
),
return
py
::
array
(
py
::
buffer_info
(
nullptr
,
sizeof
(
T
),
...
@@ -59,6 +71,13 @@ py::array_t<NestedStruct> create_nested(size_t n) {
...
@@ -59,6 +71,13 @@ py::array_t<NestedStruct> create_nested(size_t n) {
return
arr
;
return
arr
;
}
}
template
<
typename
S
>
void
print_recarray
(
py
::
array_t
<
S
>
arr
)
{
auto
buf
=
arr
.
request
();
auto
ptr
=
static_cast
<
S
*>
(
buf
.
ptr
);
for
(
size_t
i
=
0
;
i
<
buf
.
size
;
i
++
)
std
::
cout
<<
ptr
[
i
]
<<
std
::
endl
;
}
void
print_format_descriptors
()
{
void
print_format_descriptors
()
{
std
::
cout
<<
py
::
format_descriptor
<
Struct
>::
value
()
<<
std
::
endl
;
std
::
cout
<<
py
::
format_descriptor
<
Struct
>::
value
()
<<
std
::
endl
;
...
@@ -75,4 +94,7 @@ void init_ex20(py::module &m) {
...
@@ -75,4 +94,7 @@ void init_ex20(py::module &m) {
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
);
m
.
def
(
"print_format_descriptors"
,
&
print_format_descriptors
);
m
.
def
(
"print_format_descriptors"
,
&
print_format_descriptors
);
m
.
def
(
"print_rec_simple"
,
&
print_recarray
<
Struct
>
);
m
.
def
(
"print_rec_packed"
,
&
print_recarray
<
PackedStruct
>
);
m
.
def
(
"print_rec_nested"
,
&
print_recarray
<
NestedStruct
>
);
}
}
example/example20.py
View file @
669e1426
...
@@ -3,7 +3,8 @@ from __future__ import print_function
...
@@ -3,7 +3,8 @@ from __future__ import print_function
import
numpy
as
np
import
numpy
as
np
from
example
import
(
from
example
import
(
create_rec_simple
,
create_rec_packed
,
create_rec_nested
,
print_format_descriptors
create_rec_simple
,
create_rec_packed
,
create_rec_nested
,
print_format_descriptors
,
print_rec_simple
,
print_rec_packed
,
print_rec_nested
)
)
...
@@ -28,6 +29,12 @@ for func, dtype in [(create_rec_simple, simple_dtype), (create_rec_packed, packe
...
@@ -28,6 +29,12 @@ for func, dtype in [(create_rec_simple, simple_dtype), (create_rec_packed, packe
check_eq
(
arr
,
[(
False
,
0
,
0.0
),
(
True
,
1
,
1.5
),
(
False
,
2
,
3.0
)],
simple_dtype
)
check_eq
(
arr
,
[(
False
,
0
,
0.0
),
(
True
,
1
,
1.5
),
(
False
,
2
,
3.0
)],
simple_dtype
)
check_eq
(
arr
,
[(
False
,
0
,
0.0
),
(
True
,
1
,
1.5
),
(
False
,
2
,
3.0
)],
packed_dtype
)
check_eq
(
arr
,
[(
False
,
0
,
0.0
),
(
True
,
1
,
1.5
),
(
False
,
2
,
3.0
)],
packed_dtype
)
# uncomment lines below to cause a segfault upon exit in Py_Finalize :(
# if dtype == simple_dtype:
# print_rec_simple(arr)
# else:
# print_rec_packed(arr)
nested_dtype
=
np
.
dtype
([(
'a'
,
simple_dtype
),
(
'b'
,
packed_dtype
)])
nested_dtype
=
np
.
dtype
([(
'a'
,
simple_dtype
),
(
'b'
,
packed_dtype
)])
...
@@ -40,3 +47,4 @@ assert arr.dtype == nested_dtype
...
@@ -40,3 +47,4 @@ assert arr.dtype == nested_dtype
check_eq
(
arr
,
[((
False
,
0
,
0.0
),
(
True
,
1
,
1.5
)),
check_eq
(
arr
,
[((
False
,
0
,
0.0
),
(
True
,
1
,
1.5
)),
((
True
,
1
,
1.5
),
(
False
,
2
,
3.0
)),
((
True
,
1
,
1.5
),
(
False
,
2
,
3.0
)),
((
False
,
2
,
3.0
),
(
True
,
3
,
4.5
))],
nested_dtype
)
((
False
,
2
,
3.0
),
(
True
,
3
,
4.5
))],
nested_dtype
)
# print_rec_nested(arr)
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