Skip to content

Commit d9399fb

Browse files
authored
Merge pull request astropy#6277 from taldcroft/table-info-weakref
Change DataInfo._parent to a weakref
2 parents 305792a + 433e080 commit d9399fb

5 files changed

Lines changed: 62 additions & 3 deletions

File tree

‎CHANGES.rst‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,15 @@ API Changes
514514
- Added optional ``axis`` parameter to ``insert`` method for ``Column`` and
515515
``MaskedColumn`` classes. [#6092]
516516

517+
- The private ``_parent`` attribute in the ``info`` attribute of table
518+
columns was changed from a direct reference to the parent column to a weak
519+
reference. This was in response to a memory leak caused by having a
520+
circular reference cycle. This change means that expressions like
521+
``col[3:5].info`` will now fail because at the point of the ``info``
522+
property being evaluated the ``col[3:5]`` weak reference is dead. Instead
523+
force a reference with ``c = col[3:5]`` followed by
524+
``c.info.indices``. [#6277]
525+
517526
- ``astropy.time``
518527

519528
- ``astropy.units``
@@ -642,6 +651,10 @@ Bug Fixes
642651

643652
- Fix QTable add/insert row for multidimensional Quantity. [#6092]
644653

654+
- Fix memory leak where updating a table column or deleting a table
655+
object was not releasing the memory due to a reference cycle
656+
in the column ``info`` attributes. [#6277]
657+
645658
- ``astropy.table``
646659

647660
- ``astropy.tests``

‎astropy/io/ascii/html.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,14 @@ def write(self, table):
412412
w.end(indent=False)
413413
col_str_iters = []
414414
new_cols_escaped = []
415+
416+
# Make a container to hold any new_col objects created
417+
# below for multicolumn elements. This is purely to
418+
# maintain a reference for these objects during
419+
# subsequent iteration to format column values. This
420+
# requires that the weakref info._parent be maintained.
421+
new_cols = []
422+
415423
for col, col_escaped in zip(cols, cols_escaped):
416424
if len(col.shape) > 1 and self.html['multicol']:
417425
span = col.shape[1]
@@ -422,6 +430,7 @@ def write(self, table):
422430
new_col_iter_str_vals = self.fill_values(col, new_col.info.iter_str_vals())
423431
col_str_iters.append(new_col_iter_str_vals)
424432
new_cols_escaped.append(col_escaped)
433+
new_cols.append(new_col)
425434
else:
426435

427436
col_iter_str_vals = self.fill_values(col, col.info.iter_str_vals())

‎astropy/table/tests/test_info.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from ... import table
1616
from ...utils.data_info import data_info_factory, dtype_info_name
1717
from ..table_helpers import simple_table
18+
import pytest
1819

1920

2021
def test_table_info_attributes(table_types):
@@ -241,3 +242,10 @@ def test_no_deprecation_warning():
241242
with warnings.catch_warnings(record=True) as warns:
242243
t.info()
243244
assert len(warns) == 0
245+
246+
247+
def test_lost_parent_error():
248+
c = table.Column([1, 2, 3], name='a')
249+
with pytest.raises(AttributeError) as err:
250+
c[:].info.name
251+
assert 'failed access "info" attribute' in str(err)

‎astropy/utils/data_info.py‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,41 @@ class DataInfo(object):
211211
_attrs_no_copy = set()
212212
_info_summary_attrs = ('dtype', 'shape', 'unit', 'format', 'description', 'class')
213213
_represent_as_dict_attrs = ()
214-
_parent = None
214+
_parent_ref = None
215215

216216
def __init__(self, bound=False):
217217
# If bound to a data object instance then create the dict of attributes
218218
# which stores the info attribute values.
219219
if bound:
220220
self._attrs = dict((attr, None) for attr in self.attr_names)
221221

222+
@property
223+
def _parent(self):
224+
if self._parent_ref is None:
225+
return None
226+
else:
227+
parent = self._parent_ref()
228+
if parent is not None:
229+
return parent
230+
231+
else:
232+
raise AttributeError("""\
233+
failed access "info" attribute on a temporary object.
234+
235+
It looks like you have done something like ``col[3:5].info``, i.e.
236+
you accessed ``info`` from a temporary slice object ``col[3:5]`` that
237+
only exists momentarily. This has failed because the reference to
238+
that temporary object is now lost. Instead force a permanent
239+
reference with ``c = col[3:5]`` followed by ``c.info``.""")
240+
241+
242+
@_parent.setter
243+
def _parent(self, value):
244+
if value is None:
245+
self._parent_ref = None
246+
else:
247+
self._parent_ref = weakref.ref(value)
248+
222249
def __get__(self, instance, owner_cls):
223250
if instance is None:
224251
# This is an unbound descriptor on the class

‎docs/table/indexing.rst‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,12 @@ The *copy_on_getitem* mode forces columns to copy and relabel their indices upon
176176
slicing. In the absence of this mode, table slices will preserve
177177
indices while column slices will not::
178178

179-
>>> t['a'][[1, 3]].info.indices
179+
>>> ca = t['a'][[1, 3]]
180+
>>> ca.info.indices
180181
[]
181182
>>> with t.index_mode('copy_on_getitem'):
182-
... print(t['a'][[1, 3]].info.indices)
183+
... ca = t['a'][[1, 3]]
184+
... print(ca.info.indices)
183185
[ a rows
184186
--- ----
185187
2 0

0 commit comments

Comments
 (0)