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
Boxiang Sun
cython
Commits
5e045662
Commit
5e045662
authored
Jun 22, 2018
by
gabrieldemarmiesse
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved an example from buffer.rst to the examples directory.
parent
b9f7a142
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 @
5e045662
# 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 @
5e045662
...
@@ -119,29 +119,7 @@ This is where ``__releasebuffer__`` comes in.
...
@@ -119,29 +119,7 @@ This is where ``__releasebuffer__`` comes in.
We can add a reference count to each matrix,
We can add a reference count to each matrix,
and lock it for mutation whenever a view exists.
and lock it for mutation whenever a view exists.
::
.. literalinclude:: ../../examples/userguide/buffer/view_count.pyx
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
Flags
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