Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Unreleased

Thanks to Abhishek Verma in `PR #755 <https://github.com/pytest-dev/pytest-randomly/pull/755>`__.

* Fix crash when test node IDs contain surrogate characters from undecodable filenames.

Thanks to Huanyi Xie in `PR #751 <https://github.com/pytest-dev/pytest-randomly/pull/751>`__.

5.0.0 (2026-09-01)
------------------

Expand Down
2 changes: 1 addition & 1 deletion src/pytest_randomly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def reduce_list_of_lists(lists: list[list[T]]) -> list[T]:

@lru_cache
def _crc32(string: str) -> int:
return crc32(string.encode())
return crc32(string.encode(errors="surrogatepass"))


if have_faker: # pragma: no branch
Expand Down
26 changes: 26 additions & 0 deletions tests/test_pytest_randomly.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import shutil
from unittest import mock
from zlib import crc32

import pytest

Expand Down Expand Up @@ -91,6 +92,23 @@ def test_a():
out.assert_outcomes(passed=1, failed=0)


def test_undecodable_filename(ourtester):
path = ourtester.path / "test_\udcff.py"
try:
path.write_text("def test_one():\n assert True\n", encoding="utf-8")
except OSError: # pragma: no cover
pytest.skip("Filesystem does not allow undecodable filenames")

result = ourtester.runpytest(
"--randomly-seed=1",
# Upstream issue: the cache provider cannot persist surrogate-containing node IDs
"-p",
"no:cacheprovider",
)

result.assert_outcomes(passed=1)


def test_using_last_seed(ourtester):
ourtester.makepyfile(
test_one="""
Expand Down Expand Up @@ -913,3 +931,11 @@ def test_a(pytestconfig):
seed = header[0].removeprefix("Using --randomly-seed=")
worker_seeds = {p.read_text() for p in ourtester.path.glob("seed-*.txt")}
assert worker_seeds == {seed}


def test_crc32_surrogates():
"""
Lone surrogates, as found in node IDs from undecodable filenames, hash as
their UTF-8 surrogatepass bytes instead of raising UnicodeEncodeError.
"""
assert pytest_randomly._crc32("a\udcffb") == crc32(b"a\xed\xb3\xbfb")