Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix infinite recursion collection bug with pytest_ignore_collect hook #3771

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/3771.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix infinite recursion during collection if a ``pytest_ignore_collect`` returns ``False`` instead of ``None``.
9 changes: 6 additions & 3 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ def __init__(self, fspath, parent=None, config=None, session=None, nodeid=None):
def _recurse(self, path):
ihook = self.gethookproxy(path.dirpath())
if ihook.pytest_ignore_collect(path=path, config=self.config):
return
return False
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't have anything to do with the fix, just thought I would keep the return value consistent.

for pat in self._norecursepatterns:
if path.check(fnmatch=pat):
return False
Expand Down Expand Up @@ -594,9 +594,12 @@ def isinitpath(self, path):
return path in self.session._initialpaths

def collect(self):
path = self.fspath.dirpath()
this_path = self.fspath.dirpath()
pkg_prefix = None
for path in path.visit(fil=lambda x: 1, rec=self._recurse, bf=True, sort=True):
for path in this_path.visit(rec=self._recurse, bf=True, sort=True):
# we will visit our own __init__.py file, in which case we skip it
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the actual fix. I did not understand what this had to do with pytest_ignore_collect though.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

visit() calls back the self._recurse(path) which in turn calls the pytest_ignore_collect

if path.basename == "__init__.py" and path.dirpath() == this_path:
continue
if pkg_prefix and pkg_prefix in path.parts():
continue
for x in self._collectfile(path):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def pytest_ignore_collect(path):
return False
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def test():
pass
6 changes: 6 additions & 0 deletions testing/python/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1577,3 +1577,9 @@ def test_real():
)
result = testdir.runpytest("--keep-duplicates", a.strpath, a.strpath)
result.stdout.fnmatch_lines(["*collected 2 item*"])


def test_package_collection_infinite_recursion(testdir):
testdir.copy_example("collect/package_infinite_recursion")
result = testdir.runpytest()
result.stdout.fnmatch_lines("*1 passed*")