Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
f3f8b6fe
Commit
f3f8b6fe
authored
Jun 22, 2018
by
scoder
Committed by
GitHub
Jun 22, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2395 from gabrieldemarmiesse/test_buffer_2
Added tests to "Implementing the buffer protocol" part 2
parents
1bfc57de
9c665e36
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
45 deletions
+46
-45
docs/examples/userguide/buffer/matrix_with_buffer.pyx
docs/examples/userguide/buffer/matrix_with_buffer.pyx
+45
-0
docs/src/userguide/buffer.rst
docs/src/userguide/buffer.rst
+1
-45
No files found.
docs/examples/userguide/buffer/matrix_with_buffer.pyx
0 → 100644
View file @
f3f8b6fe
# distutils: language = c++
from
cpython
cimport
Py_buffer
from
libcpp.vector
cimport
vector
cdef
class
Matrix
:
cdef
Py_ssize_t
ncols
cdef
Py_ssize_t
shape
[
2
]
cdef
Py_ssize_t
strides
[
2
]
cdef
vector
[
float
]
v
def
__cinit__
(
self
,
Py_ssize_t
ncols
):
self
.
ncols
=
ncols
def
add_row
(
self
):
"""Adds a row, initially zero-filled."""
self
.
v
.
resize
(
self
.
v
.
size
()
+
self
.
ncols
)
def
__getbuffer__
(
self
,
Py_buffer
*
buffer
,
int
flags
):
cdef
Py_ssize_t
itemsize
=
sizeof
(
self
.
v
[
0
])
self
.
shape
[
0
]
=
self
.
v
.
size
()
/
self
.
ncols
self
.
shape
[
1
]
=
self
.
ncols
# Stride 1 is the distance, in bytes, between two items in a row;
# this is the distance between two adjacent items in the vector.
# Stride 0 is the distance between the first elements of adjacent rows.
self
.
strides
[
1
]
=
<
Py_ssize_t
>
(
<
char
*>&
(
self
.
v
[
1
])
-
<
char
*>&
(
self
.
v
[
0
]))
self
.
strides
[
0
]
=
self
.
ncols
*
self
.
strides
[
1
]
buffer
.
buf
=
<
char
*>&
(
self
.
v
[
0
])
buffer
.
format
=
'f'
# float
buffer
.
internal
=
NULL
# see References
buffer
.
itemsize
=
itemsize
buffer
.
len
=
self
.
v
.
size
()
*
itemsize
# product(shape) * itemsize
buffer
.
ndim
=
2
buffer
.
obj
=
self
buffer
.
readonly
=
0
buffer
.
shape
=
self
.
shape
buffer
.
strides
=
self
.
strides
buffer
.
suboffsets
=
NULL
# for pointer arrays only
def
__releasebuffer__
(
self
,
Py_buffer
*
buffer
):
pass
docs/src/userguide/buffer.rst
View file @
f3f8b6fe
...
@@ -27,51 +27,7 @@ Implementing the buffer protocol requires adding two methods,
...
@@ -27,51 +27,7 @@ Implementing the buffer protocol requires adding two methods,
``__getbuffer__`` and ``__releasebuffer__``,
``__getbuffer__`` and ``__releasebuffer__``,
which Cython handles specially.
which Cython handles specially.
::
.. literalinclude:: ../../examples/userguide/buffer/matrix_with_buffer.pyx
from cpython cimport Py_buffer
from libcpp.vector cimport vector
cdef class Matrix:
cdef Py_ssize_t ncols
cdef Py_ssize_t shape[2]
cdef Py_ssize_t strides[2]
cdef vector[float] v
def __cinit__(self, Py_ssize_t ncols):
self.ncols = ncols
def add_row(self):
"""Adds a row, initially zero-filled."""
self.v.resize(self.v.size() + self.ncols)
def __getbuffer__(self, Py_buffer *buffer, int flags):
cdef Py_ssize_t itemsize = sizeof(self.v[0])
self.shape[0] = self.v.size() / self.ncols
self.shape[1] = self.ncols
# Stride 1 is the distance, in bytes, between two items in a row;
# this is the distance between two adjacent items in the vector.
# Stride 0 is the distance between the first elements of adjacent rows.
self.strides[1] = <Py_ssize_t>( <char *>&(self.v[1])
- <char *>&(self.v[0]))
self.strides[0] = self.ncols * self.strides[1]
buffer.buf = <char *>&(self.v[0])
buffer.format = 'f' # float
buffer.internal = NULL # see References
buffer.itemsize = itemsize
buffer.len = self.v.size() * itemsize # product(shape) * itemsize
buffer.ndim = 2
buffer.obj = self
buffer.readonly = 0
buffer.shape = self.shape
buffer.strides = self.strides
buffer.suboffsets = NULL # for pointer arrays only
def __releasebuffer__(self, Py_buffer *buffer):
pass
The method ``Matrix.__getbuffer__`` fills a descriptor structure,
The method ``Matrix.__getbuffer__`` fills a descriptor structure,
called a ``Py_buffer``, that is defined by the Python C-API.
called a ``Py_buffer``, that is defined by the Python C-API.
...
...
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