Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
427e4afc
Commit
427e4afc
authored
May 28, 2017
by
Dean Moldovan
Browse files
Fix buffer protocol inheritance
Fixes #878.
parent
6d2411f1
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
2 deletions
+28
-2
include/pybind11/class_support.h
include/pybind11/class_support.h
+8
-2
tests/test_buffers.cpp
tests/test_buffers.cpp
+9
-0
tests/test_buffers.py
tests/test_buffers.py
+11
-0
No files found.
include/pybind11/class_support.h
View file @
427e4afc
...
...
@@ -447,11 +447,17 @@ inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) {
/// buffer_protocol: Fill in the view as specified by flags.
extern
"C"
inline
int
pybind11_getbuffer
(
PyObject
*
obj
,
Py_buffer
*
view
,
int
flags
)
{
auto
tinfo
=
get_type_info
(
Py_TYPE
(
obj
));
// Look for a `get_buffer` implementation in this type's info or any bases (following MRO).
type_info
*
tinfo
=
nullptr
;
for
(
auto
type
:
reinterpret_borrow
<
tuple
>
(
Py_TYPE
(
obj
)
->
tp_mro
))
{
tinfo
=
get_type_info
((
PyTypeObject
*
)
type
.
ptr
());
if
(
tinfo
&&
tinfo
->
get_buffer
)
break
;
}
if
(
view
==
nullptr
||
obj
==
nullptr
||
!
tinfo
||
!
tinfo
->
get_buffer
)
{
if
(
view
)
view
->
obj
=
nullptr
;
PyErr_SetString
(
PyExc_BufferError
,
"
generic_type::
getbuffer(): Internal error"
);
PyErr_SetString
(
PyExc_BufferError
,
"
pybind11_
getbuffer(): Internal error"
);
return
-
1
;
}
memset
(
view
,
0
,
sizeof
(
Py_buffer
));
...
...
tests/test_buffers.cpp
View file @
427e4afc
...
...
@@ -74,6 +74,11 @@ private:
float
*
m_data
;
};
class
SquareMatrix
:
public
Matrix
{
public:
SquareMatrix
(
ssize_t
n
)
:
Matrix
(
n
,
n
)
{
}
};
struct
PTMFBuffer
{
int32_t
value
=
0
;
...
...
@@ -141,6 +146,10 @@ test_initializer buffers([](py::module &m) {
})
;
// Derived classes inherit the buffer protocol and the buffer access function
py
::
class_
<
SquareMatrix
,
Matrix
>
(
m
,
"SquareMatrix"
)
.
def
(
py
::
init
<
ssize_t
>
());
py
::
class_
<
PTMFBuffer
>
(
m
,
"PTMFBuffer"
,
py
::
buffer_protocol
())
.
def
(
py
::
init
<>
())
.
def_readwrite
(
"value"
,
&
PTMFBuffer
::
value
)
...
...
tests/test_buffers.py
View file @
427e4afc
...
...
@@ -36,6 +36,7 @@ def test_from_python():
@
pytest
.
unsupported_on_pypy
def
test_to_python
():
m
=
Matrix
(
5
,
5
)
assert
memoryview
(
m
).
shape
==
(
5
,
5
)
assert
m
[
2
,
3
]
==
0
m
[
2
,
3
]
=
4
...
...
@@ -63,6 +64,16 @@ def test_to_python():
assert
cstats
.
move_assignments
==
0
@
pytest
.
unsupported_on_pypy
def
test_inherited_protocol
():
"""SquareMatrix is derived from Matrix and inherits the buffer protocol"""
from
pybind11_tests
import
SquareMatrix
matrix
=
SquareMatrix
(
5
)
assert
memoryview
(
matrix
).
shape
==
(
5
,
5
)
assert
np
.
asarray
(
matrix
).
shape
==
(
5
,
5
)
@
pytest
.
unsupported_on_pypy
def
test_ptmf
():
for
cls
in
[
PTMFBuffer
,
ConstPTMFBuffer
,
DerivedPTMFBuffer
]:
...
...
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