Skip to content

Commit 023f051

Browse files
committed
main: move collection cache attributes to local variables in collect()
They are only used for the duration of this function.
1 parent c867452 commit 023f051

File tree

1 file changed

+30
-40
lines changed

1 file changed

+30
-40
lines changed

src/_pytest/main.py

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@
4545
from typing import Type
4646
from typing_extensions import Literal
4747

48-
from _pytest.python import Package
49-
5048

5149
def pytest_addoption(parser: Parser) -> None:
5250
parser.addini(
@@ -443,23 +441,6 @@ def __init__(self, config: Config) -> None:
443441
self.startdir = config.invocation_dir
444442
self._initialpaths = frozenset() # type: FrozenSet[py.path.local]
445443

446-
# Keep track of any collected nodes in here, so we don't duplicate fixtures.
447-
self._collection_node_cache1 = (
448-
{}
449-
) # type: Dict[py.path.local, Sequence[nodes.Collector]]
450-
self._collection_node_cache2 = (
451-
{}
452-
) # type: Dict[Tuple[Type[nodes.Collector], py.path.local], nodes.Collector]
453-
454-
# Keep track of any collected collectors in matchnodes paths, so they
455-
# are not collected more than once.
456-
self._collection_matchnodes_cache = (
457-
{}
458-
) # type: Dict[Tuple[Type[nodes.Collector], str], CollectReport]
459-
460-
# Dirnames of pkgs with dunder-init files.
461-
self._collection_pkg_roots = {} # type: Dict[str, Package]
462-
463444
self._bestrelpathcache = _bestrelpath_cache(
464445
config.rootdir
465446
) # type: Dict[py.path.local, str]
@@ -639,6 +620,21 @@ def perform_collect( # noqa: F811
639620
def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
640621
from _pytest.python import Package
641622

623+
# Keep track of any collected nodes in here, so we don't duplicate fixtures.
624+
node_cache1 = {} # type: Dict[py.path.local, Sequence[nodes.Collector]]
625+
node_cache2 = (
626+
{}
627+
) # type: Dict[Tuple[Type[nodes.Collector], py.path.local], nodes.Collector]
628+
629+
# Keep track of any collected collectors in matchnodes paths, so they
630+
# are not collected more than once.
631+
matchnodes_cache = (
632+
{}
633+
) # type: Dict[Tuple[Type[nodes.Collector], str], CollectReport]
634+
635+
# Dirnames of pkgs with dunder-init files.
636+
pkg_roots = {} # type: Dict[str, Package]
637+
642638
for argpath, names in self._initial_parts:
643639
self.trace("processing argument", (argpath, names))
644640
self.trace.root.indent += 1
@@ -655,14 +651,12 @@ def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
655651
if parent.isdir():
656652
pkginit = parent.join("__init__.py")
657653
if pkginit.isfile():
658-
if pkginit not in self._collection_node_cache1:
654+
if pkginit not in node_cache1:
659655
col = self._collectfile(pkginit, handle_dupes=False)
660656
if col:
661657
if isinstance(col[0], Package):
662-
self._collection_pkg_roots[str(parent)] = col[0]
663-
self._collection_node_cache1[col[0].fspath] = [
664-
col[0]
665-
]
658+
pkg_roots[str(parent)] = col[0]
659+
node_cache1[col[0].fspath] = [col[0]]
666660

667661
# If it's a directory argument, recurse and look for any Subpackages.
668662
# Let the Package collector deal with subnodes, don't collect here.
@@ -685,28 +679,28 @@ def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
685679
for x in self._collectfile(pkginit):
686680
yield x
687681
if isinstance(x, Package):
688-
self._collection_pkg_roots[str(dirpath)] = x
689-
if str(dirpath) in self._collection_pkg_roots:
682+
pkg_roots[str(dirpath)] = x
683+
if str(dirpath) in pkg_roots:
690684
# Do not collect packages here.
691685
continue
692686

693687
for x in self._collectfile(path):
694688
key = (type(x), x.fspath)
695-
if key in self._collection_node_cache2:
696-
yield self._collection_node_cache2[key]
689+
if key in node_cache2:
690+
yield node_cache2[key]
697691
else:
698-
self._collection_node_cache2[key] = x
692+
node_cache2[key] = x
699693
yield x
700694
else:
701695
assert argpath.check(file=1)
702696

703-
if argpath in self._collection_node_cache1:
704-
col = self._collection_node_cache1[argpath]
697+
if argpath in node_cache1:
698+
col = node_cache1[argpath]
705699
else:
706-
collect_root = self._collection_pkg_roots.get(argpath.dirname, self)
700+
collect_root = pkg_roots.get(argpath.dirname, self)
707701
col = collect_root._collectfile(argpath, handle_dupes=False)
708702
if col:
709-
self._collection_node_cache1[argpath] = col
703+
node_cache1[argpath] = col
710704

711705
matching = []
712706
work = [
@@ -724,11 +718,11 @@ def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
724718
if not isinstance(node, nodes.Collector):
725719
continue
726720
key = (type(node), node.nodeid)
727-
if key in self._collection_matchnodes_cache:
728-
rep = self._collection_matchnodes_cache[key]
721+
if key in matchnodes_cache:
722+
rep = matchnodes_cache[key]
729723
else:
730724
rep = collect_one_node(node)
731-
self._collection_matchnodes_cache[key] = rep
725+
matchnodes_cache[key] = rep
732726
if rep.passed:
733727
submatchnodes = []
734728
for r in rep.result:
@@ -776,10 +770,6 @@ def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
776770
yield from matching
777771

778772
self.trace.root.indent -= 1
779-
self._collection_node_cache1.clear()
780-
self._collection_node_cache2.clear()
781-
self._collection_matchnodes_cache.clear()
782-
self._collection_pkg_roots.clear()
783773

784774
def genitems(
785775
self, node: Union[nodes.Item, nodes.Collector]

0 commit comments

Comments
 (0)