Skip to content

Commit c4af555

Browse files
committed
Stop using weakref for Cursor.connection
1 parent 4888ca7 commit c4af555

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

pymysql/cursors.py

+4-16
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ def __init__(self, connection):
2121
Do not create an instance of a Cursor yourself. Call
2222
connections.Connection.cursor().
2323
'''
24-
from weakref import ref
25-
self.connection = ref(connection)
24+
self.connection = connection
2625
self.description = None
2726
self.rownumber = 0
2827
self.rowcount = -1
@@ -44,10 +43,6 @@ def close(self):
4443
conn = self.connection
4544
if conn is None:
4645
return
47-
if conn() is None:
48-
self.connection = None
49-
return
50-
5146
try:
5247
while self.nextset():
5348
pass
@@ -56,11 +51,8 @@ def close(self):
5651

5752
def _get_db(self):
5853
if not self.connection:
59-
raise ProgrammingError("cursor closed")
60-
con = self.connection()
61-
if con is None:
62-
raise ProgrammingError("Connection closed")
63-
return con
54+
raise ProgrammingError("Cursor closed")
55+
return self.connection
6456

6557
def _check_executed(self):
6658
if not self._executed:
@@ -288,10 +280,6 @@ def close(self):
288280
conn = self.connection
289281
if conn is None:
290282
return
291-
conn = conn()
292-
if conn is None:
293-
self.connection = None
294-
return
295283

296284
if self._result is not None and self._result is conn._result:
297285
self._result._finish_unbuffered_query()
@@ -304,7 +292,7 @@ def close(self):
304292

305293
def _query(self, q):
306294
conn = self._get_db()
307-
self.lastexecuted = q
295+
self.lastexecuted = q
308296
conn.query(q, unbuffered=True)
309297
self._do_get_result()
310298
return self.rowcount

runtests.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
@atexit.register
1515
def report_uncollectable():
16-
if not gc.garbage: return
16+
if not gc.garbage:
17+
print("No garbages!")
18+
return
1719
print('uncollectable objects')
1820
for obj in gc.garbage:
1921
print(obj)

0 commit comments

Comments
 (0)