Skip to content

Commit 876db99

Browse files
arthurdarcetShaneHarvey
authored andcommitted
PYTHON-1867 Fix noisy AttributeError in Cursor.__del__ (#421)
1 parent 8152971 commit 876db99

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

pymongo/cursor.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,12 @@ def _clone_base(self, session):
291291
def __die(self, synchronous=False):
292292
"""Closes this cursor.
293293
"""
294-
already_killed = self.__killed
294+
try:
295+
already_killed = self.__killed
296+
except AttributeError:
297+
# __init__ did not run to completion (or at all).
298+
return
299+
295300
self.__killed = True
296301
if self.__id and not already_killed:
297302
if self.__exhaust and self.__exhaust_mgr:

test/test_cursor.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
ALL,
3636
OFF)
3737
from pymongo.collation import Collation
38-
from pymongo.cursor import CursorType
38+
from pymongo.cursor import Cursor, CursorType
3939
from pymongo.errors import (ConfigurationError,
4040
ExecutionTimeout,
4141
InvalidOperation,
@@ -1409,6 +1409,12 @@ def assertCursorKilled():
14091409
else:
14101410
self.assertEqual(0, len(results["started"]))
14111411

1412+
def test_delete_not_initialized(self):
1413+
# Creating a cursor with invalid arguments will not run __init__
1414+
# but will still call __del__, eg test.find(invalidKwarg=1).
1415+
cursor = Cursor.__new__(Cursor) # Skip calling __init__
1416+
cursor.__del__() # no error
1417+
14121418

14131419
class TestRawBatchCursor(IntegrationTest):
14141420
def test_find_raw(self):

0 commit comments

Comments
 (0)