move the _bestrelpathcache to session

This commit is contained in:
Ronny Pfannschmidt 2018-11-05 21:07:21 +01:00
parent 5eb4016110
commit f521f5e012
2 changed files with 17 additions and 7 deletions

View File

@ -9,6 +9,7 @@ import os
import pkgutil import pkgutil
import sys import sys
import attr
import py import py
import six import six
@ -369,6 +370,16 @@ class Failed(Exception):
""" signals a stop as failed test run. """ """ signals a stop as failed test run. """
@attr.s
class _bestrelpath_cache(dict):
path = attr.ib()
def __missing__(self, path):
r = self.path.bestrelpath(path)
self[path] = r
return r
class Session(nodes.FSCollector): class Session(nodes.FSCollector):
Interrupted = Interrupted Interrupted = Interrupted
Failed = Failed Failed = Failed
@ -387,9 +398,14 @@ class Session(nodes.FSCollector):
self._initialpaths = frozenset() self._initialpaths = frozenset()
# Keep track of any collected nodes in here, so we don't duplicate fixtures # Keep track of any collected nodes in here, so we don't duplicate fixtures
self._node_cache = {} self._node_cache = {}
self._bestrelpathcache = _bestrelpath_cache(config.rootdir)
self.config.pluginmanager.register(self, name="session") self.config.pluginmanager.register(self, name="session")
def _node_location_to_relpath(self, node_path):
# bestrelpath is a quite slow function
return self._bestrelpathcache[node_path]
@hookimpl(tryfirst=True) @hookimpl(tryfirst=True)
def pytest_collectstart(self): def pytest_collectstart(self):
if self.shouldfail: if self.shouldfail:

View File

@ -523,13 +523,7 @@ class Item(Node):
return self._location return self._location
except AttributeError: except AttributeError:
location = self.reportinfo() location = self.reportinfo()
# bestrelpath is a quite slow function fspath = self.session._node_location_to_relpath(location[0])
cache = self.config.__dict__.setdefault("_bestrelpathcache", {})
try:
fspath = cache[location[0]]
except KeyError:
fspath = self.session.fspath.bestrelpath(location[0])
cache[location[0]] = fspath
location = (fspath, location[1], str(location[2])) location = (fspath, location[1], str(location[2]))
self._location = location self._location = location
return location return location