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
1bfc57de
Commit
1bfc57de
authored
Jun 22, 2018
by
scoder
Committed by
GitHub
Jun 22, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2396 from gabrieldemarmiesse/test_buffer_3
Added tests to "Implementing the buffer protocol" part 3
parents
b9f7a142
5e045662
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
23 deletions
+30
-23
docs/examples/userguide/buffer/view_count.pyx
docs/examples/userguide/buffer/view_count.pyx
+29
-0
docs/src/userguide/buffer.rst
docs/src/userguide/buffer.rst
+1
-23
No files found.
docs/examples/userguide/buffer/view_count.pyx
0 → 100644
View file @
1bfc57de
# distutils: language = c++
from
cpython
cimport
Py_buffer
from
libcpp.vector
cimport
vector
cdef
class
Matrix
:
cdef
int
view_count
cdef
Py_ssize_t
ncols
cdef
vector
[
float
]
v
# ...
def
__cinit__
(
self
,
Py_ssize_t
ncols
):
self
.
ncols
=
ncols
self
.
view_count
=
0
def
add_row
(
self
):
if
self
.
view_count
>
0
:
raise
ValueError
(
"can't add row while being viewed"
)
self
.
v
.
resize
(
self
.
v
.
size
()
+
self
.
ncols
)
def
__getbuffer__
(
self
,
Py_buffer
*
buffer
,
int
flags
):
# ... as before
self
.
view_count
+=
1
def
__releasebuffer__
(
self
,
Py_buffer
*
buffer
):
self
.
view_count
-=
1
\ No newline at end of file
docs/src/userguide/buffer.rst
View file @
1bfc57de
...
...
@@ -119,29 +119,7 @@ This is where ``__releasebuffer__`` comes in.
We can add a reference count to each matrix,
and lock it for mutation whenever a view exists.
::
cdef class Matrix:
# ...
cdef int view_count
def __cinit__(self, Py_ssize_t ncols):
self.ncols = ncols
self.view_count = 0
def add_row(self):
if self.view_count > 0:
raise ValueError("can't add row while being viewed")
self.v.resize(self.v.size() + self.ncols)
def __getbuffer__(self, Py_buffer *buffer, int flags):
# ... as before
self.view_count += 1
def __releasebuffer__(self, Py_buffer *buffer):
self.view_count -= 1
.. literalinclude:: ../../examples/userguide/buffer/view_count.pyx
Flags
-----
...
...
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