Merge pull request #26 from PyMySQL/feature/cursor-context-manager

Add context manager interface to cursor
This commit is contained in:
INADA Naoki
2015-02-25 11:38:05 +09:00

View File

@ -85,14 +85,23 @@ class BaseCursor(object):
def close(self): def close(self):
"""Close the cursor. No further queries will be possible.""" """Close the cursor. No further queries will be possible."""
try:
if self.connection is None or self.connection() is None: if self.connection is None or self.connection() is None:
return return
while self.nextset(): while self.nextset():
pass pass
finally:
self.connection = None self.connection = None
self.errorhandler = None self.errorhandler = None
self._result = None self._result = None
def __enter__(self):
return self
def __exit__(self, *exc_info):
del exc_info
self.close()
def _check_executed(self): def _check_executed(self):
if not self._executed: if not self._executed:
self.errorhandler(self, ProgrammingError, "execute() first") self.errorhandler(self, ProgrammingError, "execute() first")