Skip to content

Commit 1acb18b

Browse files
committed
_getconftestmodules: use functools.lru_cache
Also renames `_path2confmods` to `_dirpath2confmods` for clarity (it is expected to be a dirpath in `_importconftest`).
1 parent b46d42d commit 1acb18b

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

src/_pytest/config/__init__.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
""" command line options, ini-file and conftest.py processing. """
22
from __future__ import absolute_import, division, print_function
33
import argparse
4+
import functools
45
import inspect
56
import shlex
67
import types
@@ -205,7 +206,7 @@ def __init__(self):
205206
self._conftest_plugins = set()
206207

207208
# state related to local conftest plugins
208-
self._path2confmods = {}
209+
self._dirpath2confmods = {}
209210
self._conftestpath2mod = {}
210211
self._confcutdir = None
211212
self._noconftest = False
@@ -376,31 +377,29 @@ def _try_load_conftest(self, anchor):
376377
if x.check(dir=1):
377378
self._getconftestmodules(x)
378379

380+
@functools.lru_cache(maxsize=None)
379381
def _getconftestmodules(self, path):
380382
if self._noconftest:
381383
return []
382384

383-
try:
384-
return self._path2confmods[path]
385-
except KeyError:
386-
if path.isfile():
387-
directory = path.dirpath()
388-
else:
389-
directory = path
390-
# XXX these days we may rather want to use config.rootdir
391-
# and allow users to opt into looking into the rootdir parent
392-
# directories instead of requiring to specify confcutdir
393-
clist = []
394-
for parent in directory.realpath().parts():
395-
if self._confcutdir and self._confcutdir.relto(parent):
396-
continue
397-
conftestpath = parent.join("conftest.py")
398-
if conftestpath.isfile():
399-
mod = self._importconftest(conftestpath)
400-
clist.append(mod)
401-
402-
self._path2confmods[path] = clist
403-
return clist
385+
if path.isfile():
386+
directory = path.dirpath()
387+
else:
388+
directory = path
389+
390+
# XXX these days we may rather want to use config.rootdir
391+
# and allow users to opt into looking into the rootdir parent
392+
# directories instead of requiring to specify confcutdir
393+
clist = []
394+
for parent in directory.realpath().parts():
395+
if self._confcutdir and self._confcutdir.relto(parent):
396+
continue
397+
conftestpath = parent.join("conftest.py")
398+
if conftestpath.isfile():
399+
mod = self._importconftest(conftestpath)
400+
clist.append(mod)
401+
self._dirpath2confmods[directory] = clist
402+
return clist
404403

405404
def _rget_with_confmod(self, name, path):
406405
modules = self._getconftestmodules(path)
@@ -441,8 +440,8 @@ def _importconftest(self, conftestpath):
441440
self._conftest_plugins.add(mod)
442441
self._conftestpath2mod[conftestpath] = mod
443442
dirpath = conftestpath.dirpath()
444-
if dirpath in self._path2confmods:
445-
for path, mods in self._path2confmods.items():
443+
if dirpath in self._dirpath2confmods:
444+
for path, mods in self._dirpath2confmods.items():
446445
if path and path.relto(dirpath) or path == dirpath:
447446
assert mod not in mods
448447
mods.append(mod)

0 commit comments

Comments
 (0)