Skip to content

Commit

Permalink
Put the filename calc back in _connect. Fixes #916
Browse files Browse the repository at this point in the history
It was moved to __init__ to avoid recalculating, but the directory could
have changed, so we need to wait to do the work.  Instead, only do the
relpath on systems that need it (Windows Py 2).
  • Loading branch information
nedbat committed Jan 8, 2020
1 parent d402b46 commit e4b8389
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
6 changes: 5 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ want to know what's different in 5.0 since 4.5.x, see :ref:`whatsnew5x`.
Unreleased
----------

Nothing yet.
- A performance improvement in 5.0.2 didn't work for test suites that changed
directory before combining data, causing "Couldn't use data file: no such
table: meta" errors (`issue 916`_). This is now fixed.

.. _issue 916: https://github.com/nedbat/coveragepy/issues/916


.. _changes_502:
Expand Down
28 changes: 16 additions & 12 deletions coverage/sqldata.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import sys
import zlib

from coverage import env
from coverage.backward import get_thread_id, iitems, to_bytes, to_string
from coverage.debug import NoDebugging, SimpleReprMixin, clipped_repr
from coverage.files import PathAliases
Expand Down Expand Up @@ -971,31 +972,34 @@ def __init__(self, filename, debug):
self.filename = filename
self.nest = 0
self.con = None
# SQLite on Windows on py2 won't open a file if the filename argument
# has non-ascii characters in it. Opening a relative file name avoids
# a problem if the current directory has non-ascii.
try:
self.connect_filename = os.path.relpath(self.filename)
except ValueError:
# ValueError can be raised under Windows when os.getcwd() returns a
# folder from a different drive than the drive of self.filename in
# which case we keep the original value of self.filename unchanged,
# hoping that we won't face the non-ascii directory problem.
self.connect_filename = self.filename

def _connect(self):
"""Connect to the db and do universal initialization."""
if self.con is not None:
return

# SQLite on Windows on py2 won't open a file if the filename argument
# has non-ascii characters in it. Opening a relative file name avoids
# a problem if the current directory has non-ascii.
filename = self.filename
if env.WINDOWS and env.PY2:
try:
filename = os.path.relpath(self.filename)
except ValueError:
# ValueError can be raised under Windows when os.getcwd() returns a
# folder from a different drive than the drive of self.filename in
# which case we keep the original value of self.filename unchanged,
# hoping that we won't face the non-ascii directory problem.
pass

# It can happen that Python switches threads while the tracer writes
# data. The second thread will also try to write to the data,
# 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:
self.debug.write("Connecting to {!r}".format(self.filename))
self.con = sqlite3.connect(self.connect_filename, check_same_thread=False)
self.con = sqlite3.connect(filename, check_same_thread=False)
self.con.create_function('REGEXP', 2, _regexp)

# This pragma makes writing faster. It disables rollbacks, but we never need them.
Expand Down

0 comments on commit e4b8389

Please sign in to comment.