From 36e585bdbfd50ed0c686ef5b3183db1597cfa8c1 Mon Sep 17 00:00:00 2001 From: Shane Harvey Date: Wed, 29 Nov 2017 12:34:04 -0800 Subject: [PATCH] PYTHON-1427 Always initialize Cursor attributes used in __del__. Prevents attribute errors during garbage collection. --- pymongo/cursor.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pymongo/cursor.py b/pymongo/cursor.py index 2c50e3d8b..b995050f6 100644 --- a/pymongo/cursor.py +++ b/pymongo/cursor.py @@ -124,9 +124,12 @@ class Cursor(object): .. mongodoc:: cursors """ + # Initialize all attributes used in __del__ before possibly raising + # an error to avoid attribute errors during garbage collection. self.__id = None self.__exhaust = False self.__exhaust_mgr = None + self.__killed = False if session: self.__session = session @@ -206,7 +209,6 @@ class Cursor(object): self.__data = deque() self.__address = None self.__retrieved = 0 - self.__killed = False self.__codec_options = collection.codec_options self.__read_preference = collection.read_preference