Add Context Manager Interface to Connection. (#413)

Fixes #400.
This commit is contained in:
Inada Naoki
2019-11-29 21:15:17 +09:00
committed by GitHub
parent 8fd628ef0f
commit 643220972e
2 changed files with 11 additions and 0 deletions

View File

@ -207,6 +207,12 @@ class Connection(_mysql.connection):
self.autocommit(autocommit)
self.messages = []
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.close()
def autocommit(self, on):
on = bool(on)
if self.get_autocommit() != on: