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
427e4afc
Commit
427e4afc
authored
May 28, 2017
by
Dean Moldovan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix buffer protocol inheritance
Fixes #878.
parent
6d2411f1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
2 deletions
+28
-2
include/pybind11/class_support.h
+8
-2
tests/test_buffers.cpp
+9
-0
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) {
...
@@ -447,11 +447,17 @@ inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) {
/// buffer_protocol: Fill in the view as specified by flags.
/// buffer_protocol: Fill in the view as specified by flags.
extern
"C"
inline
int
pybind11_getbuffer
(
PyObject
*
obj
,
Py_buffer
*
view
,
int
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
==
nullptr
||
obj
==
nullptr
||
!
tinfo
||
!
tinfo
->
get_buffer
)
{
if
(
view
)
if
(
view
)
view
->
obj
=
nullptr
;
view
->
obj
=
nullptr
;
PyErr_SetString
(
PyExc_BufferError
,
"
generic_type::
getbuffer(): Internal error"
);
PyErr_SetString
(
PyExc_BufferError
,
"
pybind11_
getbuffer(): Internal error"
);
return
-
1
;
return
-
1
;
}
}
memset
(
view
,
0
,
sizeof
(
Py_buffer
));
memset
(
view
,
0
,
sizeof
(
Py_buffer
));
...
...
tests/test_buffers.cpp
View file @
427e4afc
...
@@ -74,6 +74,11 @@ private:
...
@@ -74,6 +74,11 @@ private:
float
*
m_data
;
float
*
m_data
;
};
};
class
SquareMatrix
:
public
Matrix
{
public
:
SquareMatrix
(
ssize_t
n
)
:
Matrix
(
n
,
n
)
{
}
};
struct
PTMFBuffer
{
struct
PTMFBuffer
{
int32_t
value
=
0
;
int32_t
value
=
0
;
...
@@ -141,6 +146,10 @@ test_initializer buffers([](py::module &m) {
...
@@ -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
())
py
::
class_
<
PTMFBuffer
>
(
m
,
"PTMFBuffer"
,
py
::
buffer_protocol
())
.
def
(
py
::
init
<>
())
.
def
(
py
::
init
<>
())
.
def_readwrite
(
"value"
,
&
PTMFBuffer
::
value
)
.
def_readwrite
(
"value"
,
&
PTMFBuffer
::
value
)
...
...
tests/test_buffers.py
View file @
427e4afc
...
@@ -36,6 +36,7 @@ def test_from_python():
...
@@ -36,6 +36,7 @@ def test_from_python():
@pytest.unsupported_on_pypy
@pytest.unsupported_on_pypy
def
test_to_python
():
def
test_to_python
():
m
=
Matrix
(
5
,
5
)
m
=
Matrix
(
5
,
5
)
assert
memoryview
(
m
)
.
shape
==
(
5
,
5
)
assert
m
[
2
,
3
]
==
0
assert
m
[
2
,
3
]
==
0
m
[
2
,
3
]
=
4
m
[
2
,
3
]
=
4
...
@@ -64,6 +65,16 @@ def test_to_python():
...
@@ -64,6 +65,16 @@ def test_to_python():
@pytest.unsupported_on_pypy
@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
():
def
test_ptmf
():
for
cls
in
[
PTMFBuffer
,
ConstPTMFBuffer
,
DerivedPTMFBuffer
]:
for
cls
in
[
PTMFBuffer
,
ConstPTMFBuffer
,
DerivedPTMFBuffer
]:
buf
=
cls
()
buf
=
cls
()
...
...
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