summaryrefslogtreecommitdiff
path: root/tests/buffers/bufaccess.pyx
diff options
context:
space:
mode:
Diffstat (limited to 'tests/buffers/bufaccess.pyx')
-rw-r--r--tests/buffers/bufaccess.pyx42
1 files changed, 41 insertions, 1 deletions
diff --git a/tests/buffers/bufaccess.pyx b/tests/buffers/bufaccess.pyx
index 6b0b4ac30..3144f613d 100644
--- a/tests/buffers/bufaccess.pyx
+++ b/tests/buffers/bufaccess.pyx
@@ -10,7 +10,7 @@
from __future__ import unicode_literals
from cpython.object cimport PyObject
-from cpython.ref cimport Py_INCREF, Py_DECREF
+from cpython.ref cimport Py_INCREF, Py_DECREF, Py_CLEAR
cimport cython
import sys
@@ -1005,6 +1005,46 @@ def assign_to_object(object[object] buf, int idx, obj):
buf[idx] = obj
@testcase
+def check_object_nulled_1d(MockBuffer[object, ndim=1] buf, int idx, obj):
+ """
+ See comments on printbuf_object above.
+
+ >>> a = object()
+ >>> rc1 = get_refcount(a)
+ >>> A = ObjectMockBuffer(None, [a, a])
+ >>> check_object_nulled_1d(A, 0, a)
+ >>> check_object_nulled_1d(A, 1, a)
+ >>> A = ObjectMockBuffer(None, [a, a, a, a], strides=(2,))
+ >>> check_object_nulled_1d(A, 0, a) # only 0 due to stride
+ >>> get_refcount(a) == rc1
+ True
+ """
+ cdef PyObject **data = <PyObject **>buf.buffer
+ Py_CLEAR(data[idx])
+ res = buf[idx] # takes None
+ buf[idx] = obj
+ return res
+
+@testcase
+def check_object_nulled_2d(MockBuffer[object, ndim=2] buf, int idx1, int idx2, obj):
+ """
+ See comments on printbuf_object above.
+
+ >>> a = object()
+ >>> rc1 = get_refcount(a)
+ >>> A = ObjectMockBuffer(None, [a, a, a, a], shape=(2, 2))
+ >>> check_object_nulled_2d(A, 0, 0, a)
+ >>> check_object_nulled_2d(A, 1, 1, a)
+ >>> get_refcount(a) == rc1
+ True
+ """
+ cdef PyObject **data = <PyObject **>buf.buffer
+ Py_CLEAR(data[idx1 + 2*idx2])
+ res = buf[idx1, idx2] # takes None
+ buf[idx1, idx2] = obj
+ return res
+
+@testcase
def assign_temporary_to_object(object[object] buf):
"""
See comments on printbuf_object above.