Skip to content

[PR #13442/f59e1ad6 backport][8.3.x] fixtures: workaround PyPy bug which sometimes causes a KeyError crash during collection #13447

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

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/13312.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a possible ``KeyError`` crash on PyPy during collection of tests involving higher-scoped parameters.
16 changes: 12 additions & 4 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,18 @@ def reorder_items_atscope(
for other_scope in HIGH_SCOPES:
other_scoped_items_by_argkey = items_by_argkey[other_scope]
for argkey in argkeys_by_item[other_scope].get(i, ()):
other_scoped_items_by_argkey[argkey][i] = None
other_scoped_items_by_argkey[argkey].move_to_end(
i, last=False
)
argkey_dict = other_scoped_items_by_argkey[argkey]
if not hasattr(sys, "pypy_version_info"):
argkey_dict[i] = None
argkey_dict.move_to_end(i, last=False)
else:
# Work around a bug in PyPy:
# https://github.com/pypy/pypy/issues/5257
# https://github.com/pytest-dev/pytest/issues/13312
bkp = argkey_dict.copy()
argkey_dict.clear()
argkey_dict[i] = None
argkey_dict.update(bkp)
break
if no_argkey_items:
reordered_no_argkey_items = reorder_items_atscope(
Expand Down
Loading