Skip to content

Commit f59e1ad

Browse files
authored
Merge pull request #13442 from bluetech/reorder-items-pypy
fixtures: workaround PyPy bug which sometimes causes a `KeyError` crash during collection
2 parents c7aabcc + 1fdcf68 commit f59e1ad

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

changelog/13312.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed a possible ``KeyError`` crash on PyPy during collection of tests involving higher-scoped parameters.

src/_pytest/fixtures.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,18 @@ def reorder_items_atscope(
286286
for other_scope in HIGH_SCOPES:
287287
other_scoped_items_by_argkey = items_by_argkey[other_scope]
288288
for argkey in argkeys_by_item[other_scope].get(i, ()):
289-
other_scoped_items_by_argkey[argkey][i] = None
290-
other_scoped_items_by_argkey[argkey].move_to_end(
291-
i, last=False
292-
)
289+
argkey_dict = other_scoped_items_by_argkey[argkey]
290+
if not hasattr(sys, "pypy_version_info"):
291+
argkey_dict[i] = None
292+
argkey_dict.move_to_end(i, last=False)
293+
else:
294+
# Work around a bug in PyPy:
295+
# https://github.com/pypy/pypy/issues/5257
296+
# https://github.com/pytest-dev/pytest/issues/13312
297+
bkp = argkey_dict.copy()
298+
argkey_dict.clear()
299+
argkey_dict[i] = None
300+
argkey_dict.update(bkp)
293301
break
294302
if no_argkey_items:
295303
reordered_no_argkey_items = reorder_items_atscope(

0 commit comments

Comments
 (0)