Skip to content

Commit 00fa1d2

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 4dba778 commit 00fa1d2

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]
@@ -625,6 +606,21 @@ def perform_collect( # noqa: F811
625606
def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
626607
from _pytest.python import Package
627608

609+
# Keep track of any collected nodes in here, so we don't duplicate fixtures.
610+
node_cache1 = {} # type: Dict[py.path.local, Sequence[nodes.Collector]]
611+
node_cache2 = (
612+
{}
613+
) # type: Dict[Tuple[Type[nodes.Collector], py.path.local], nodes.Collector]
614+
615+
# Keep track of any collected collectors in matchnodes paths, so they
616+
# are not collected more than once.
617+
matchnodes_cache = (
618+
{}
619+
) # type: Dict[Tuple[Type[nodes.Collector], str], CollectReport]
620+
621+
# Dirnames of pkgs with dunder-init files.
622+
pkg_roots = {} # type: Dict[str, Package]
623+
628624
for argpath, names in self._initial_parts:
629625
self.trace("processing argument", (argpath, names))
630626
self.trace.root.indent += 1
@@ -641,14 +637,12 @@ def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
641637
if parent.isdir():
642638
pkginit = parent.join("__init__.py")
643639
if pkginit.isfile():
644-
if pkginit not in self._collection_node_cache1:
640+
if pkginit not in node_cache1:
645641
col = self._collectfile(pkginit, handle_dupes=False)
646642
if col:
647643
if isinstance(col[0], Package):
648-
self._collection_pkg_roots[str(parent)] = col[0]
649-
self._collection_node_cache1[col[0].fspath] = [
650-
col[0]
651-
]
644+
pkg_roots[str(parent)] = col[0]
645+
node_cache1[col[0].fspath] = [col[0]]
652646

653647
# If it's a directory argument, recurse and look for any Subpackages.
654648
# Let the Package collector deal with subnodes, don't collect here.
@@ -671,28 +665,28 @@ def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
671665
for x in self._collectfile(pkginit):
672666
yield x
673667
if isinstance(x, Package):
674-
self._collection_pkg_roots[str(dirpath)] = x
675-
if str(dirpath) in self._collection_pkg_roots:
668+
pkg_roots[str(dirpath)] = x
669+
if str(dirpath) in pkg_roots:
676670
# Do not collect packages here.
677671
continue
678672

679673
for x in self._collectfile(path):
680674
key = (type(x), x.fspath)
681-
if key in self._collection_node_cache2:
682-
yield self._collection_node_cache2[key]
675+
if key in node_cache2:
676+
yield node_cache2[key]
683677
else:
684-
self._collection_node_cache2[key] = x
678+
node_cache2[key] = x
685679
yield x
686680
else:
687681
assert argpath.check(file=1)
688682

689-
if argpath in self._collection_node_cache1:
690-
col = self._collection_node_cache1[argpath]
683+
if argpath in node_cache1:
684+
col = node_cache1[argpath]
691685
else:
692-
collect_root = self._collection_pkg_roots.get(argpath.dirname, self)
686+
collect_root = pkg_roots.get(argpath.dirname, self)
693687
col = collect_root._collectfile(argpath, handle_dupes=False)
694688
if col:
695-
self._collection_node_cache1[argpath] = col
689+
node_cache1[argpath] = col
696690

697691
matching = []
698692
work = [
@@ -710,11 +704,11 @@ def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
710704
if not isinstance(node, nodes.Collector):
711705
continue
712706
key = (type(node), node.nodeid)
713-
if key in self._collection_matchnodes_cache:
714-
rep = self._collection_matchnodes_cache[key]
707+
if key in matchnodes_cache:
708+
rep = matchnodes_cache[key]
715709
else:
716710
rep = collect_one_node(node)
717-
self._collection_matchnodes_cache[key] = rep
711+
matchnodes_cache[key] = rep
718712
if rep.passed:
719713
submatchnodes = []
720714
for r in rep.result:
@@ -762,10 +756,6 @@ def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
762756
yield from matching
763757

764758
self.trace.root.indent -= 1
765-
self._collection_node_cache1.clear()
766-
self._collection_node_cache2.clear()
767-
self._collection_matchnodes_cache.clear()
768-
self._collection_pkg_roots.clear()
769759

770760
def genitems(
771761
self, node: Union[nodes.Item, nodes.Collector]

0 commit comments

Comments
 (0)