Skip to content

Commit 233c2a2

Browse files
authored
Merge pull request #4250 from blueyed/ignore-pyc
collection: _recurse: skip __pycache__
2 parents cc531a1 + 40228fc commit 233c2a2

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

src/_pytest/main.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,19 @@ def _collect(self, arg):
517517
# Let the Package collector deal with subnodes, don't collect here.
518518
if argpath.check(dir=1):
519519
assert not names, "invalid arg %r" % (arg,)
520+
521+
if six.PY2:
522+
523+
def filter_(f):
524+
return f.check(file=1) and not f.strpath.endswith("*.pyc")
525+
526+
else:
527+
528+
def filter_(f):
529+
return f.check(file=1)
530+
520531
for path in argpath.visit(
521-
fil=lambda x: x.check(file=1), rec=self._recurse, bf=True, sort=True
532+
fil=filter_, rec=self._recurse, bf=True, sort=True
522533
):
523534
pkginit = path.dirpath().join("__init__.py")
524535
if pkginit.exists() and not any(x in pkginit.parts() for x in paths):
@@ -562,15 +573,17 @@ def _collectfile(self, path):
562573

563574
return ihook.pytest_collect_file(path=path, parent=self)
564575

565-
def _recurse(self, path):
566-
ihook = self.gethookproxy(path.dirpath())
567-
if ihook.pytest_ignore_collect(path=path, config=self.config):
568-
return
576+
def _recurse(self, dirpath):
577+
if dirpath.basename == "__pycache__":
578+
return False
579+
ihook = self.gethookproxy(dirpath.dirpath())
580+
if ihook.pytest_ignore_collect(path=dirpath, config=self.config):
581+
return False
569582
for pat in self._norecursepatterns:
570-
if path.check(fnmatch=pat):
583+
if dirpath.check(fnmatch=pat):
571584
return False
572-
ihook = self.gethookproxy(path)
573-
ihook.pytest_collect_directory(path=path, parent=self)
585+
ihook = self.gethookproxy(dirpath)
586+
ihook.pytest_collect_directory(path=dirpath, parent=self)
574587
return True
575588

576589
def _tryconvertpyarg(self, x):

src/_pytest/python.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -516,15 +516,17 @@ def __init__(self, fspath, parent=None, config=None, session=None, nodeid=None):
516516
self._norecursepatterns = session._norecursepatterns
517517
self.fspath = fspath
518518

519-
def _recurse(self, path):
520-
ihook = self.gethookproxy(path.dirpath())
521-
if ihook.pytest_ignore_collect(path=path, config=self.config):
519+
def _recurse(self, dirpath):
520+
if dirpath.basename == "__pycache__":
522521
return False
522+
ihook = self.gethookproxy(dirpath.dirpath())
523+
if ihook.pytest_ignore_collect(path=dirpath, config=self.config):
524+
return
523525
for pat in self._norecursepatterns:
524-
if path.check(fnmatch=pat):
526+
if dirpath.check(fnmatch=pat):
525527
return False
526-
ihook = self.gethookproxy(path)
527-
ihook.pytest_collect_directory(path=path, parent=self)
528+
ihook = self.gethookproxy(dirpath)
529+
ihook.pytest_collect_directory(path=dirpath, parent=self)
528530
return True
529531

530532
def gethookproxy(self, fspath):

0 commit comments

Comments
 (0)