Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1c818f8

Browse filesBrowse files
committed
Cursor supports context manager interface
1 parent 864e279 commit 1c818f8
Copy full SHA for 1c818f8

File tree

Expand file treeCollapse file tree

1 file changed

+16
-7
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+16
-7
lines changed

‎MySQLdb/cursors.py

Copy file name to clipboardExpand all lines: MySQLdb/cursors.py
+16-7Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.