From ddc8a7b88e141e44629a4d81dd07b73d65b43b49 Mon Sep 17 00:00:00 2001 From: Huanyi Xie <74809554+xiehuanyi@users.noreply.github.com> Date: Sun, 11 Oct 2026 21:54:28 +0300 Subject: [PATCH 1/4] Hash node IDs containing filesystem surrogate characters --- CHANGELOG.rst | 3 +++ src/pytest_randomly/__init__.py | 2 +- tests/test_pytest_randomly.py | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a340a2e..bce2a47 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -5,6 +5,9 @@ Changelog Unreleased ---------- +* Avoid crashing when test node IDs contain surrogate characters from undecodable filesystem names. + In that situation, pytest’s independent cache serialization limitation still requires ``-p no:cacheprovider``. + * Drop Python 3.10 support. * Fix ``--randomly-seed=last`` with pytest-xdist, where workers could each pick a different seed after ``--cache-clear``, failing with “Different tests were collected”. diff --git a/src/pytest_randomly/__init__.py b/src/pytest_randomly/__init__.py index 85e9eee..8878f84 100644 --- a/src/pytest_randomly/__init__.py +++ b/src/pytest_randomly/__init__.py @@ -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 diff --git a/tests/test_pytest_randomly.py b/tests/test_pytest_randomly.py index a71d68c..d5e99cc 100644 --- a/tests/test_pytest_randomly.py +++ b/tests/test_pytest_randomly.py @@ -1,6 +1,7 @@ from __future__ import annotations import shutil +import sys from unittest import mock import pytest @@ -91,6 +92,23 @@ def test_a(): out.assert_outcomes(passed=1, failed=0) +@pytest.mark.skipif( + sys.platform == "win32", reason="Requires byte-based filesystem names" +) +def test_undecodable_filename(ourtester): + path = ourtester.path / "test_\udcff.py" + path.write_text("def test_one():\n assert True\n", encoding="utf-8") + + 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=""" From 68fab26582dcd6ba8f88c4a6d18df024e0f41803 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Sun, 8 Nov 2026 23:05:58 +0000 Subject: [PATCH 2/4] changelog --- CHANGELOG.rst | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index bce2a47..537ff7f 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -5,9 +5,6 @@ Changelog Unreleased ---------- -* Avoid crashing when test node IDs contain surrogate characters from undecodable filesystem names. - In that situation, pytest’s independent cache serialization limitation still requires ``-p no:cacheprovider``. - * Drop Python 3.10 support. * Fix ``--randomly-seed=last`` with pytest-xdist, where workers could each pick a different seed after ``--cache-clear``, failing with “Different tests were collected”. @@ -15,6 +12,11 @@ Unreleased Thanks to Abhishek Verma in `PR #755 `__. +* Avoid crashing when test node IDs contain surrogate characters from undecodable filesystem names. + In that situation, pytest’s independent cache serialization limitation still requires ``-p no:cacheprovider``. + + Thanks to Huanyi Xie in `PR #751 `__. + 5.0.0 (2026-09-01) ------------------ From e34d569694294bee51a17d6f1a0dae39e49fdf54 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Sun, 8 Nov 2026 23:13:19 +0000 Subject: [PATCH 3/4] Make test pass on macOs, test crc32 directly, improve chaneglog --- CHANGELOG.rst | 3 +-- tests/test_pytest_randomly.py | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 537ff7f..2758042 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -12,8 +12,7 @@ Unreleased Thanks to Abhishek Verma in `PR #755 `__. -* Avoid crashing when test node IDs contain surrogate characters from undecodable filesystem names. - In that situation, pytest’s independent cache serialization limitation still requires ``-p no:cacheprovider``. +* Fix crash when test node IDs contain surrogate characters from undecodable filenames. Thanks to Huanyi Xie in `PR #751 `__. diff --git a/tests/test_pytest_randomly.py b/tests/test_pytest_randomly.py index d5e99cc..e04c2a2 100644 --- a/tests/test_pytest_randomly.py +++ b/tests/test_pytest_randomly.py @@ -1,8 +1,8 @@ from __future__ import annotations import shutil -import sys from unittest import mock +from zlib import crc32 import pytest @@ -92,12 +92,12 @@ def test_a(): out.assert_outcomes(passed=1, failed=0) -@pytest.mark.skipif( - sys.platform == "win32", reason="Requires byte-based filesystem names" -) def test_undecodable_filename(ourtester): path = ourtester.path / "test_\udcff.py" - path.write_text("def test_one():\n assert True\n", encoding="utf-8") + try: + path.write_text("def test_one():\n assert True\n", encoding="utf-8") + except OSError: + pytest.skip("Filesystem does not allow undecodable filenames") result = ourtester.runpytest( "--randomly-seed=1", @@ -931,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") From 6e99e200b3af1e1bfa5783931c93a2ed8914ff1f Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Sun, 8 Nov 2026 23:16:10 +0000 Subject: [PATCH 4/4] nocover --- tests/test_pytest_randomly.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_pytest_randomly.py b/tests/test_pytest_randomly.py index e04c2a2..c3214b4 100644 --- a/tests/test_pytest_randomly.py +++ b/tests/test_pytest_randomly.py @@ -96,7 +96,7 @@ 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: + except OSError: # pragma: no cover pytest.skip("Filesystem does not allow undecodable filenames") result = ourtester.runpytest(