Skip to content

Commit f54d81f

Browse files
authored
test: make GC check resilient to broken weakrefs (#1099)
* fix: Make gc check resilient to broken weakrefs In some python deployments (I believe macOS is one), weakref objects aren't guaranteed to exist across gc collections. The proposed check pattern is also used in tensorflow [0] which ran into similar issues a few years ago. [0]: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/framework/errors_test.py#L33 * fix: add pragma for coverage * Fix whitespace
1 parent 51aced2 commit f54d81f

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

tests/unit/test_dbapi_connection.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,14 @@ def test_does_not_keep_cursor_instances_alive(self):
219219
# Connections should not hold strong references to the Cursor instances
220220
# they created, unnecessarily keeping them alive.
221221
gc.collect()
222-
cursors = [obj for obj in gc.get_objects() if isinstance(obj, Cursor)]
223-
self.assertEqual(len(cursors), 2)
222+
cursor_count = 0
223+
for obj in gc.get_objects():
224+
try:
225+
if isinstance(obj, Cursor):
226+
cursor_count += 1
227+
except ReferenceError: # pragma: NO COVER
228+
pass
229+
self.assertEqual(cursor_count, 2)
224230

225231
def test_commit(self):
226232
connection = self._make_one(client=self._mock_client())

0 commit comments

Comments
 (0)