Skip to content

Commit 7c93cc9

Browse files
authored
Backport PR #55008 on branch 2.1.x (CoW: Clear dead references every time we add a new one) (#55220)
CoW: Clear dead references every time we add a new one (#55008) (cherry picked from commit 7134f2c)
1 parent 21d8cdd commit 7c93cc9

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

Diff for: pandas/_libs/internals.pyx

+8-3
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,11 @@ cdef class BlockValuesRefs:
951951
else:
952952
self.referenced_blocks = []
953953

954+
def _clear_dead_references(self) -> None:
955+
self.referenced_blocks = [
956+
ref for ref in self.referenced_blocks if ref() is not None
957+
]
958+
954959
def add_reference(self, blk: SharedBlock) -> None:
955960
"""Adds a new reference to our reference collection.
956961

@@ -959,6 +964,7 @@ cdef class BlockValuesRefs:
959964
blk: SharedBlock
960965
The block that the new references should point to.
961966
"""
967+
self._clear_dead_references()
962968
self.referenced_blocks.append(weakref.ref(blk))
963969

964970
def add_index_reference(self, index: object) -> None:
@@ -969,6 +975,7 @@ cdef class BlockValuesRefs:
969975
index : Index
970976
The index that the new reference should point to.
971977
"""
978+
self._clear_dead_references()
972979
self.referenced_blocks.append(weakref.ref(index))
973980

974981
def has_reference(self) -> bool:
@@ -981,8 +988,6 @@ cdef class BlockValuesRefs:
981988
-------
982989
bool
983990
"""
984-
self.referenced_blocks = [
985-
ref for ref in self.referenced_blocks if ref() is not None
986-
]
991+
self._clear_dead_references()
987992
# Checking for more references than block pointing to itself
988993
return len(self.referenced_blocks) > 1

0 commit comments

Comments
 (0)