Skip to content

Commit

Permalink
refactor: SqliteDb uses its debug object more like other code
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed May 22, 2022
1 parent d4c09f9 commit 8991e9d
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions coverage/sqldata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ class SqliteDb(SimpleReprMixin):
"""
def __init__(self, filename, debug):
self.debug = debug if debug.should("sql") else None
self.debug = debug
self.filename = filename
self.nest = 0
self.con = None
Expand All @@ -1055,7 +1055,7 @@ def _connect(self):
# effectively causing a nested context. However, given the idempotent
# nature of the tracer operations, sharing a connection among threads
# is not a problem.
if self.debug:
if self.debug.should("sql"):
self.debug.write(f"Connecting to {self.filename!r}")
try:
self.con = sqlite3.connect(self.filename, check_same_thread=False)
Expand Down Expand Up @@ -1091,13 +1091,13 @@ def __exit__(self, exc_type, exc_value, traceback):
self.con.__exit__(exc_type, exc_value, traceback)
self.close()
except Exception as exc:
if self.debug:
if self.debug.should("sql"):
self.debug.write(f"EXCEPTION from __exit__: {exc}")
raise DataError(f"Couldn't end data file {self.filename!r}: {exc}") from exc

def execute(self, sql, parameters=()):
"""Same as :meth:`python:sqlite3.Connection.execute`."""
if self.debug:
if self.debug.should("sql"):
tail = f" with {parameters!r}" if parameters else ""
self.debug.write(f"Executing {sql!r}{tail}")
try:
Expand All @@ -1122,7 +1122,7 @@ def execute(self, sql, parameters=()):
)
except Exception: # pragma: cant happen
pass
if self.debug:
if self.debug.should("sql"):
self.debug.write(f"EXCEPTION from execute: {msg}")
raise DataError(f"Couldn't use data file {self.filename!r}: {msg}") from exc

Expand All @@ -1145,7 +1145,7 @@ def execute_one(self, sql, parameters=()):

def executemany(self, sql, data):
"""Same as :meth:`python:sqlite3.Connection.executemany`."""
if self.debug:
if self.debug.should("sql"):
data = list(data)
self.debug.write(f"Executing many {sql!r} with {len(data)} rows")
try:
Expand All @@ -1158,7 +1158,7 @@ def executemany(self, sql, data):

def executescript(self, script):
"""Same as :meth:`python:sqlite3.Connection.executescript`."""
if self.debug:
if self.debug.should("sql"):
self.debug.write("Executing script with {} chars: {}".format(
len(script), clipped_repr(script, 100),
))
Expand Down

0 comments on commit 8991e9d

Please sign in to comment.