Skip to content

Commit c511aad

Browse files
Array: use .copy() to ensure data ownership
for `Array(..., copy=True)`, see astropy/astropy#7244
1 parent f5e0ffe commit c511aad

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

‎gwpy/tests/test_array.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ def test_new(self):
101101
# test with some data
102102
array = self.create()
103103
utils.assert_array_equal(array.value, self.data)
104+
105+
# test that copies ensures owndata
106+
a = self.TEST_CLASS([1, 2, 3, 4], copy=True)
107+
assert a.flags.owndata
108+
assert self.TEST_CLASS(a, copy=True).flags.owndata
109+
110+
# return array for subclasses to use
104111
return array
105112

106113
def test_unit(self, array):

‎gwpy/types/array.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class Array(Quantity):
105105

106106
def __new__(cls, value, unit=None, # Quantity attrs
107107
name=None, epoch=None, channel=None, # new attrs
108-
dtype=None, copy=False, subok=True, # ndarray attrs
108+
dtype=None, copy=True, subok=True, # ndarray attrs
109109
order=None, ndmin=0):
110110
"""Create a new `Array`
111111
"""
@@ -119,9 +119,14 @@ def __new__(cls, value, unit=None, # Quantity attrs
119119

120120
# create new array
121121
new = super(Array, cls).__new__(cls, value, unit=unit, dtype=dtype,
122-
copy=copy, order=order, subok=subok,
122+
copy=False, order=order, subok=subok,
123123
ndmin=ndmin)
124124

125+
# explicitly copy here to get ownership of the data,
126+
# see (astropy/astropy#7244)
127+
if copy:
128+
new = new.copy()
129+
125130
# set new attributes
126131
if name is not None:
127132
new.name = name

0 commit comments

Comments
 (0)