From 8e7ea78226220524b0c79a87ad70e28e53f3cba5 Mon Sep 17 00:00:00 2001 From: BChass <34097574+Bchass@users.noreply.github.com> Date: Mon, 3 Jun 2024 14:38:58 -0400 Subject: [PATCH 1/8] Take a users input; not writeable if set to False --- tinynumpy/tinynumpy.py | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/tinynumpy/tinynumpy.py b/tinynumpy/tinynumpy.py index 9709dcf..bf31dac 100644 --- a/tinynumpy/tinynumpy.py +++ b/tinynumpy/tinynumpy.py @@ -526,10 +526,11 @@ class ndarray(object): """ __slots__ = ['_dtype', '_shape', '_strides', '_itemsize', - '_offset', '_base', '_data'] + '_offset', '_base', '_data', '_flags_bool'] def __init__(self, shape, dtype='float64', buffer=None, offset=0, strides=None, order=None): + # Check order if order is not None: raise RuntimeError('ndarray order parameter is not supported') @@ -589,6 +590,8 @@ def __init__(self, shape, dtype='float64', buffer=None, offset=0, self._data = BufferClass.from_address(ctypes.addressof(buffer)) else: self._data = BufferClass.from_buffer(buffer) + + self._flags_bool = True @property def __array_interface__(self): @@ -639,11 +642,15 @@ def __setitem__(self, key, value): # Get info for view offset, shape, strides = self._index_helper(key) - - # Is this easy? - if not shape: - self._data[offset] = value - return + + # Check if flag is True or False + if not self._flags_bool: + raise ValueError("Array is not writeable") + else: + # Is this easy? + if not shape: + self._data[offset] = value + return # Create view to set data to view = ndarray(shape, self.dtype, @@ -1146,14 +1153,18 @@ def T(self): @property def flags(self): - c_cont = _get_step(self) == 1 - return dict(C_CONTIGUOUS=c_cont, - F_CONTIGUOUS=(c_cont and self.ndim < 2), - OWNDATA=(self._base is None), - WRITEABLE=True, # todo: fix this - ALIGNED=c_cont, # todo: different from contiguous? - ) + return {'C_CONTIGUOUS': c_cont, + 'F_CONTIGUOUS': (c_cont and self.ndim < 2), + 'OWNDATA': (self._base is None), + 'WRITEABLE': self._flags_bool, + 'ALIGNED': c_cont, + 'WRITEBACKIFCOPY': False} + + @flags.setter + def flags(self, value): + if isinstance(value, dict): + self._flags_bool = value['WRITEABLE'] ## Methods - managemenet From 663d70688c5710e11898b22bb388516c47cbba26 Mon Sep 17 00:00:00 2001 From: BChass <34097574+Bchass@users.noreply.github.com> Date: Mon, 3 Jun 2024 15:10:04 -0400 Subject: [PATCH 2/8] WRITEBACKIFCOPY can't be set to True --- tinynumpy/tinynumpy.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tinynumpy/tinynumpy.py b/tinynumpy/tinynumpy.py index bf31dac..8f25310 100644 --- a/tinynumpy/tinynumpy.py +++ b/tinynumpy/tinynumpy.py @@ -1164,7 +1164,10 @@ def flags(self): @flags.setter def flags(self, value): if isinstance(value, dict): - self._flags_bool = value['WRITEABLE'] + if 'WRITABLE' in value: + self._flags_bool = value['WRITEABLE'] + if 'WRITEBACKIFCOPY' in value and value['WRITEBACKIFCOPY'] == True: + raise ValueError("can't set WRITEBACKIFCOPY to True") ## Methods - managemenet From 23bb54367c29791fa7ecf2cc7f10ae096ef953fe Mon Sep 17 00:00:00 2001 From: BChass <34097574+Bchass@users.noreply.github.com> Date: Mon, 3 Jun 2024 16:10:25 -0400 Subject: [PATCH 3/8] typo --- tinynumpy/tinynumpy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tinynumpy/tinynumpy.py b/tinynumpy/tinynumpy.py index 8f25310..573d5f3 100644 --- a/tinynumpy/tinynumpy.py +++ b/tinynumpy/tinynumpy.py @@ -1164,7 +1164,7 @@ def flags(self): @flags.setter def flags(self, value): if isinstance(value, dict): - if 'WRITABLE' in value: + if 'WRITEABLE' in value: self._flags_bool = value['WRITEABLE'] if 'WRITEBACKIFCOPY' in value and value['WRITEBACKIFCOPY'] == True: raise ValueError("can't set WRITEBACKIFCOPY to True") From 22f31e2cc4de69b6139d703dd803b788b3d6db4d Mon Sep 17 00:00:00 2001 From: BChass <34097574+Bchass@users.noreply.github.com> Date: Mon, 3 Jun 2024 16:10:29 -0400 Subject: [PATCH 4/8] test case --- tinynumpy/tests/test_tinynumpy.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tinynumpy/tests/test_tinynumpy.py b/tinynumpy/tests/test_tinynumpy.py index 49743db..c1b0480 100644 --- a/tinynumpy/tests/test_tinynumpy.py +++ b/tinynumpy/tests/test_tinynumpy.py @@ -542,6 +542,18 @@ def test_getitem(): a = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) b = tnp.array([[1, 2, 3, 4], [5, 6, 7, 8]]) +def test_setitem_writeable(): + + a = tnp.array([1, 2, 3]) + a[0] = 4 + expected_result = tnp.array([4, 2, 3, 4, 5], dtype='int64') + assert all(a == expected_result) + + with pytest.raises(ValueError): + a = tnp.array([1, 2, 3]) + a.flags = {'WRITEABLE': False} + a[0] = 4 + def test_transpose(): """test the transpose function for tinynumpy""" From 7ccfd5a2d15d9d237ecc39f74f8976acd4f6b5df Mon Sep 17 00:00:00 2001 From: BChass <34097574+Bchass@users.noreply.github.com> Date: Mon, 3 Jun 2024 20:32:27 -0400 Subject: [PATCH 5/8] cover WRITEBACKIFCOPY --- tinynumpy/tests/test_tinynumpy.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tinynumpy/tests/test_tinynumpy.py b/tinynumpy/tests/test_tinynumpy.py index c1b0480..845b4c7 100644 --- a/tinynumpy/tests/test_tinynumpy.py +++ b/tinynumpy/tests/test_tinynumpy.py @@ -553,6 +553,10 @@ def test_setitem_writeable(): a = tnp.array([1, 2, 3]) a.flags = {'WRITEABLE': False} a[0] = 4 + + with pytest.raises(ValueError): + a = tnp.array([1, 2, 3]) + a.flags = {'WRITEBACKIFCOPY': True} def test_transpose(): From ec882b08687c3c03ec89ab7dfd4ce1bfda031a59 Mon Sep 17 00:00:00 2001 From: BChass <34097574+Bchass@users.noreply.github.com> Date: Mon, 3 Jun 2024 20:46:44 -0400 Subject: [PATCH 6/8] should be RuntimeError --- tinynumpy/tests/test_tinynumpy.py | 2 +- tinynumpy/tinynumpy.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tinynumpy/tests/test_tinynumpy.py b/tinynumpy/tests/test_tinynumpy.py index 845b4c7..c981832 100644 --- a/tinynumpy/tests/test_tinynumpy.py +++ b/tinynumpy/tests/test_tinynumpy.py @@ -549,7 +549,7 @@ def test_setitem_writeable(): expected_result = tnp.array([4, 2, 3, 4, 5], dtype='int64') assert all(a == expected_result) - with pytest.raises(ValueError): + with pytest.raises(RuntimeError): a = tnp.array([1, 2, 3]) a.flags = {'WRITEABLE': False} a[0] = 4 diff --git a/tinynumpy/tinynumpy.py b/tinynumpy/tinynumpy.py index 573d5f3..c4014ec 100644 --- a/tinynumpy/tinynumpy.py +++ b/tinynumpy/tinynumpy.py @@ -645,7 +645,7 @@ def __setitem__(self, key, value): # Check if flag is True or False if not self._flags_bool: - raise ValueError("Array is not writeable") + raise RuntimeError ("Array is not writeable") else: # Is this easy? if not shape: From db7f508030edd47b2b2ce7e3cdccb72ccfe3c713 Mon Sep 17 00:00:00 2001 From: BChass <34097574+Bchass@users.noreply.github.com> Date: Mon, 3 Jun 2024 21:46:14 -0400 Subject: [PATCH 7/8] Enusre WRITEABLE is True when creating a view --- tinynumpy/tinynumpy.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tinynumpy/tinynumpy.py b/tinynumpy/tinynumpy.py index c4014ec..bd4734f 100644 --- a/tinynumpy/tinynumpy.py +++ b/tinynumpy/tinynumpy.py @@ -558,6 +558,8 @@ def __init__(self, shape, dtype='float64', buffer=None, offset=0, self._offset = 0 assert strides is None self._strides = _strides_for_shape(self._shape, self.itemsize) + # Set flag to true by default + self._flags_bool = True else: # Existing array @@ -565,6 +567,8 @@ def __init__(self, shape, dtype='float64', buffer=None, offset=0, buffer = buffer.base # Keep a reference to avoid memory cleanup self._base = buffer + # WRITEABLE should be True when creating a view + self._flags_bool = True # for ndarray we use the data property if isinstance(buffer, ndarray): buffer = buffer.data @@ -590,8 +594,6 @@ def __init__(self, shape, dtype='float64', buffer=None, offset=0, self._data = BufferClass.from_address(ctypes.addressof(buffer)) else: self._data = BufferClass.from_buffer(buffer) - - self._flags_bool = True @property def __array_interface__(self): @@ -1154,6 +1156,7 @@ def T(self): @property def flags(self): c_cont = _get_step(self) == 1 + writeable = (self._base is None) and self._flags_bool return {'C_CONTIGUOUS': c_cont, 'F_CONTIGUOUS': (c_cont and self.ndim < 2), 'OWNDATA': (self._base is None), From a553228a5619d84442112e98cfb7c87ab3968164 Mon Sep 17 00:00:00 2001 From: BChass <34097574+Bchass@users.noreply.github.com> Date: Mon, 3 Jun 2024 21:47:42 -0400 Subject: [PATCH 8/8] Don't need this line --- tinynumpy/tinynumpy.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tinynumpy/tinynumpy.py b/tinynumpy/tinynumpy.py index bd4734f..965e7c4 100644 --- a/tinynumpy/tinynumpy.py +++ b/tinynumpy/tinynumpy.py @@ -1156,7 +1156,6 @@ def T(self): @property def flags(self): c_cont = _get_step(self) == 1 - writeable = (self._base is None) and self._flags_bool return {'C_CONTIGUOUS': c_cont, 'F_CONTIGUOUS': (c_cont and self.ndim < 2), 'OWNDATA': (self._base is None),