Add context manager to cursor.

This commit is contained in:
INADA Naoki
2015-03-01 03:37:20 +09:00
parent 30f1fbe82a
commit bd29e7ef96
2 changed files with 8 additions and 4 deletions

View File

@@ -53,6 +53,13 @@ class Cursor(object):
finally:
self.connection = None
def __enter__(self):
return self
def __exit__(self, *exc_info):
del exc_info
self.close()
def _get_db(self):
if not self.connection:
raise err.ProgrammingError("Cursor closed")

View File

@@ -93,13 +93,10 @@ class TestConversion(base.PyMySQLTestCase):
self.safe_create_table(
conn, "test_blob", "create table test_blob (b blob)")
c = conn.cursor()
try:
with conn.cursor() as c:
c.execute("insert into test_blob (b) values (%s)", (data,))
c.execute("select b from test_blob")
self.assertEqual(data, c.fetchone()[0])
finally:
c.close()
def test_untyped(self):
""" test conversion of null, empty string """