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
fac7c094
Commit
fac7c094
authored
Oct 13, 2016
by
Wenzel Jakob
Browse files
NumPy "base" feature: integrated feedback by @aldanor
parent
c49d6e50
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
1 deletion
+45
-1
include/pybind11/numpy.h
include/pybind11/numpy.h
+1
-1
tests/test_numpy_array.cpp
tests/test_numpy_array.cpp
+15
-0
tests/test_numpy_array.py
tests/test_numpy_array.py
+29
-0
No files found.
include/pybind11/numpy.h
View file @
fac7c094
...
...
@@ -157,7 +157,7 @@ NAMESPACE_END(detail)
#define PyArrayDescr_GET_(ptr, attr) \
(reinterpret_cast<::pybind11::detail::PyArrayDescr_Proxy*>(ptr)->attr)
#define PyArray_FLAGS_(ptr) \
(reinterpret_cast<::pybind11::detail::
PyArray_
Proxy*>
(ptr
)->
flags)
PyArray_
GET_
(ptr
,
flags)
#define PyArray_CHKFLAGS_(ptr, flag) \
(flag == (PyArray_FLAGS_(ptr) & flag))
...
...
tests/test_numpy_array.cpp
View file @
fac7c094
...
...
@@ -109,4 +109,19 @@ test_initializer numpy_array([](py::module &m) {
a
);
});
struct
ArrayClass
{
int
data
[
2
]
=
{
1
,
2
};
ArrayClass
()
{
py
::
print
(
"ArrayClass()"
);
}
~
ArrayClass
()
{
py
::
print
(
"~ArrayClass()"
);
}
};
py
::
class_
<
ArrayClass
>
(
sm
,
"ArrayClass"
)
.
def
(
py
::
init
<>
())
.
def
(
"numpy_view"
,
[](
py
::
object
&
obj
)
{
py
::
print
(
"ArrayClass::numpy_view()"
);
ArrayClass
&
a
=
obj
.
cast
<
ArrayClass
&>
();
return
py
::
array_t
<
int
>
({
2
},
{
4
},
a
.
data
,
obj
);
}
);
});
tests/test_numpy_array.py
View file @
fac7c094
import
pytest
import
gc
with
pytest
.
suppress
(
ImportError
):
import
numpy
as
np
...
...
@@ -209,3 +210,31 @@ def test_wrap():
A1
=
A1
.
diagonal
()
A2
=
wrap
(
A1
)
assert_references
(
A1
,
A2
)
@
pytest
.
requires_numpy
def
test_numpy_view
(
capture
):
from
pybind11_tests.array
import
ArrayClass
with
capture
:
ac
=
ArrayClass
()
ac_view_1
=
ac
.
numpy_view
()
ac_view_2
=
ac
.
numpy_view
()
assert
np
.
all
(
ac_view_1
==
np
.
array
([
1
,
2
],
dtype
=
np
.
int32
))
del
ac
gc
.
collect
()
assert
capture
==
"""
ArrayClass()
ArrayClass::numpy_view()
ArrayClass::numpy_view()
"""
ac_view_1
[
0
]
=
4
ac_view_1
[
1
]
=
3
assert
ac_view_2
[
0
]
==
4
assert
ac_view_2
[
1
]
==
3
with
capture
:
del
ac_view_1
del
ac_view_2
gc
.
collect
()
assert
capture
==
"""
~ArrayClass()
"""
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