diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2018-10-14 20:39:46 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2018-10-14 20:59:57 -0400 |
commit | ae9f040dc4189d73fdd708aa4ba61bf50d7b2a25 (patch) | |
tree | ee36612ab56483fbbcae6fc60c4f2812ef9014a5 /coverage/sqldata.py | |
parent | 4a404ce724280bf9c5f4bcf1e6a5ba76d7499d89 (diff) | |
download | python-coveragepy-git-ae9f040dc4189d73fdd708aa4ba61bf50d7b2a25.tar.gz |
Defer using the database when calling set_context #716
The collector calls set_context() before any code is run. If we touch
the database there, it will get created *very* early. This causes
problems with pytest-cov, which will delete those early-created files
when erasing data.
By deferring the database access until add_lines is called, the data
file stays off the disk until the collection is done (or until the
context switches), which avoids the problem.
Diffstat (limited to 'coverage/sqldata.py')
-rw-r--r-- | coverage/sqldata.py | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/coverage/sqldata.py b/coverage/sqldata.py index 6c12d681..561878fa 100644 --- a/coverage/sqldata.py +++ b/coverage/sqldata.py @@ -93,6 +93,7 @@ class CoverageSqliteData(SimpleReprMixin): self._has_lines = False self._has_arcs = False + self._current_context = None self._current_context_id = None def _choose_filename(self): @@ -205,13 +206,17 @@ class CoverageSqliteData(SimpleReprMixin): """Set the current context for future `add_lines` etc.""" if self._debug.should('dataop'): self._debug.write("Setting context: %r" % (context,)) - self._start_using() - context = context or "" - with self._connect() as con: - row = con.execute("select id from context where context = ?", (context,)).fetchone() - if row is not None: - self._current_context_id = row[0] - else: + self._current_context = context + self._current_context_id = None + + def _set_context_id(self): + """Use the _current_context to set _current_context_id.""" + context = self._current_context or "" + context_id = self._context_id(context) + if context_id is not None: + self._current_context_id = context_id + else: + with self._connect() as con: cur = con.execute("insert into context (context) values (?)", (context,)) self._current_context_id = cur.lastrowid @@ -229,8 +234,7 @@ class CoverageSqliteData(SimpleReprMixin): )) self._start_using() self._choose_lines_or_arcs(lines=True) - if self._current_context_id is None: - self.set_context("") + self._set_context_id() with self._connect() as con: for filename, linenos in iitems(line_data): file_id = self._file_id(filename, add=True) @@ -254,8 +258,7 @@ class CoverageSqliteData(SimpleReprMixin): )) self._start_using() self._choose_lines_or_arcs(arcs=True) - if self._current_context_id is None: - self.set_context("") + self._set_context_id() with self._connect() as con: for filename, arcs in iitems(arc_data): file_id = self._file_id(filename, add=True) |