Commit 5e045662 authored by gabrieldemarmiesse's avatar gabrieldemarmiesse

Moved an example from buffer.rst to the examples directory.

parent b9f7a142
# 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
...@@ -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
----- -----
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment