Skip to content

Commit

Permalink
_getconftestmodules: use functools.lru_cache
Browse files Browse the repository at this point in the history
Also renames `_path2confmods` to `_dirpath2confmods` for clarity (it is
expected to be a dirpath in `_importconftest`).

Uses an explicit maxsize, since it appears to be only relevant for a
short period [1].

1: pytest-dev#4237 (comment)
  • Loading branch information
blueyed committed Oct 26, 2018
1 parent 1f50de2 commit 371ed5b
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def __init__(self):
self._conftest_plugins = set()

# state related to local conftest plugins
self._path2confmods = {}
self._dirpath2confmods = {}
self._conftestpath2mod = {}
self._confcutdir = None
self._noconftest = False
Expand Down Expand Up @@ -384,31 +384,29 @@ def _try_load_conftest(self, anchor):
if x.check(dir=1):
self._getconftestmodules(x)

@lru_cache(maxsize=128)
def _getconftestmodules(self, path):
if self._noconftest:
return []

try:
return self._path2confmods[path]
except KeyError:
if path.isfile():
directory = path.dirpath()
else:
directory = path
# XXX these days we may rather want to use config.rootdir
# and allow users to opt into looking into the rootdir parent
# directories instead of requiring to specify confcutdir
clist = []
for parent in directory.realpath().parts():
if self._confcutdir and self._confcutdir.relto(parent):
continue
conftestpath = parent.join("conftest.py")
if conftestpath.isfile():
mod = self._importconftest(conftestpath)
clist.append(mod)

self._path2confmods[path] = clist
return clist
if path.isfile():
directory = path.dirpath()
else:
directory = path

# XXX these days we may rather want to use config.rootdir
# and allow users to opt into looking into the rootdir parent
# directories instead of requiring to specify confcutdir
clist = []
for parent in directory.realpath().parts():
if self._confcutdir and self._confcutdir.relto(parent):
continue
conftestpath = parent.join("conftest.py")
if conftestpath.isfile():
mod = self._importconftest(conftestpath)
clist.append(mod)
self._dirpath2confmods[directory] = clist
return clist

def _rget_with_confmod(self, name, path):
modules = self._getconftestmodules(path)
Expand Down Expand Up @@ -449,8 +447,8 @@ def _importconftest(self, conftestpath):
self._conftest_plugins.add(mod)
self._conftestpath2mod[conftestpath] = mod
dirpath = conftestpath.dirpath()
if dirpath in self._path2confmods:
for path, mods in self._path2confmods.items():
if dirpath in self._dirpath2confmods:
for path, mods in self._dirpath2confmods.items():
if path and path.relto(dirpath) or path == dirpath:
assert mod not in mods
mods.append(mod)
Expand Down

0 comments on commit 371ed5b

Please sign in to comment.