Skip to content

Commit b6313e0

Browse files
authored
Merge pull request #13447 from pytest-dev/patchback/backports/8.3.x/f59e1ad661832f18bce28a9522be2c6d37485d09/pr-13442
[PR #13442/f59e1ad6 backport][8.3.x] fixtures: workaround PyPy bug which sometimes causes a `KeyError` crash during collection
2 parents 84c7c7e + c104690 commit b6313e0

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
@@ -278,10 +278,18 @@ def reorder_items_atscope(
278278
for other_scope in HIGH_SCOPES:
279279
other_scoped_items_by_argkey = items_by_argkey[other_scope]
280280
for argkey in argkeys_by_item[other_scope].get(i, ()):
281-
other_scoped_items_by_argkey[argkey][i] = None
282-
other_scoped_items_by_argkey[argkey].move_to_end(
283-
i, last=False
284-
)
281+
argkey_dict = other_scoped_items_by_argkey[argkey]
282+
if not hasattr(sys, "pypy_version_info"):
283+
argkey_dict[i] = None
284+
argkey_dict.move_to_end(i, last=False)
285+
else:
286+
# Work around a bug in PyPy:
287+
# https://github.com/pypy/pypy/issues/5257
288+
# https://github.com/pytest-dev/pytest/issues/13312
289+
bkp = argkey_dict.copy()
290+
argkey_dict.clear()
291+
argkey_dict[i] = None
292+
argkey_dict.update(bkp)
285293
break
286294
if no_argkey_items:
287295
reordered_no_argkey_items = reorder_items_atscope(

0 commit comments

Comments
 (0)