Skip to content

Commit a72a2ab

Browse files
author
Silviu Surcica
committed
Revert "Remove context interface from Connector (PyMySQL#295)"
This reverts commit c64915b.
1 parent bf0ef58 commit a72a2ab

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

MySQLdb/connections.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,20 @@ def query(self, query):
216216
query = bytes(query)
217217
_mysql.connection.query(self, query)
218218

219+
def __enter__(self):
220+
from warnings import warn
221+
warn("context interface will be changed. Use explicit conn.commit() or conn.rollback().",
222+
DeprecationWarning, 2)
223+
if self.get_autocommit():
224+
self.query("BEGIN")
225+
return self.cursor()
226+
227+
def __exit__(self, exc, value, tb):
228+
if exc:
229+
self.rollback()
230+
else:
231+
self.commit()
232+
219233
def _bytes_literal(self, bs):
220234
assert isinstance(bs, (bytes, bytearray))
221235
x = self.string_literal(bs) # x is escaped and quoted bytes

0 commit comments

Comments
 (0)