Add context manager to cursor.
This commit is contained in:
@@ -53,6 +53,13 @@ class Cursor(object):
|
|||||||
finally:
|
finally:
|
||||||
self.connection = None
|
self.connection = None
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *exc_info):
|
||||||
|
del exc_info
|
||||||
|
self.close()
|
||||||
|
|
||||||
def _get_db(self):
|
def _get_db(self):
|
||||||
if not self.connection:
|
if not self.connection:
|
||||||
raise err.ProgrammingError("Cursor closed")
|
raise err.ProgrammingError("Cursor closed")
|
||||||
|
|||||||
@@ -93,13 +93,10 @@ class TestConversion(base.PyMySQLTestCase):
|
|||||||
self.safe_create_table(
|
self.safe_create_table(
|
||||||
conn, "test_blob", "create table test_blob (b blob)")
|
conn, "test_blob", "create table test_blob (b blob)")
|
||||||
|
|
||||||
c = conn.cursor()
|
with conn.cursor() as c:
|
||||||
try:
|
|
||||||
c.execute("insert into test_blob (b) values (%s)", (data,))
|
c.execute("insert into test_blob (b) values (%s)", (data,))
|
||||||
c.execute("select b from test_blob")
|
c.execute("select b from test_blob")
|
||||||
self.assertEqual(data, c.fetchone()[0])
|
self.assertEqual(data, c.fetchone()[0])
|
||||||
finally:
|
|
||||||
c.close()
|
|
||||||
|
|
||||||
def test_untyped(self):
|
def test_untyped(self):
|
||||||
""" test conversion of null, empty string """
|
""" test conversion of null, empty string """
|
||||||
|
|||||||
Reference in New Issue
Block a user