Skip to content

Commit a42fe11

Browse files
committed
Merge pull request PyMySQL#26 from PyMySQL/feature/cursor-context-manager
Add context manager interface to cursor
2 parents 864e279 + 1c818f8 commit a42fe11

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

MySQLdb/cursors.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,22 @@ def __init__(self, connection):
8585

8686
def close(self):
8787
"""Close the cursor. No further queries will be possible."""
88-
if self.connection is None or self.connection() is None:
89-
return
90-
while self.nextset():
91-
pass
92-
self.connection = None
93-
self.errorhandler = None
94-
self._result = None
88+
try:
89+
if self.connection is None or self.connection() is None:
90+
return
91+
while self.nextset():
92+
pass
93+
finally:
94+
self.connection = None
95+
self.errorhandler = None
96+
self._result = None
97+
98+
def __enter__(self):
99+
return self
100+
101+
def __exit__(self, *exc_info):
102+
del exc_info
103+
self.close()
95104

96105
def _check_executed(self):
97106
if not self._executed:

0 commit comments

Comments
 (0)