Add context manager to cursor.
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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 """
|
||||
|
||||
Reference in New Issue
Block a user