From 16d968c433733e83d0e8963411d13e60aa17544e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Delfino?= Date: Mon, 9 Nov 2026 23:37:19 -0300 Subject: [PATCH] gh-161730: Fix the mode in the compression.zstd.open() invalid mode error compression.zstd.open() removed 't' from the mode before checking for the invalid 't' and 'b' combination, so the error message named the modified mode (e.g. "Invalid mode: 'rb'" for 'rtb'), which is a valid mode the caller didn't pass. Check before removing 't', matching gzip.open(), bz2.open() and lzma.open(). Co-Authored-By: Claude Opus 5.5 --- Lib/compression/zstd/_zstdfile.py | 2 +- .../next/Library/2026-11-09-23-37-08.gh-issue-161730.hh7MVs.rst | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-09-23-37-08.gh-issue-161730.hh7MVs.rst diff --git a/Lib/compression/zstd/_zstdfile.py b/Lib/compression/zstd/_zstdfile.py index 26d7c9006375fb..f504c92d1fa143 100644 --- a/Lib/compression/zstd/_zstdfile.py +++ b/Lib/compression/zstd/_zstdfile.py @@ -325,11 +325,11 @@ def open(file, /, mode='rb', *, level=None, options=None, zstd_dict=None, """ text_mode = 't' in mode - mode = mode.replace('t', '') if text_mode: if 'b' in mode: raise ValueError(f'Invalid mode: {mode!r}') + mode = mode.replace('t', '') else: if encoding is not None: raise ValueError('Argument "encoding" not supported in binary mode') diff --git a/Misc/NEWS.d/next/Library/2026-11-09-23-37-08.gh-issue-161730.hh7MVs.rst b/Misc/NEWS.d/next/Library/2026-11-09-23-37-08.gh-issue-161730.hh7MVs.rst new file mode 100644 index 00000000000000..db673f4e95ea14 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-09-23-37-08.gh-issue-161730.hh7MVs.rst @@ -0,0 +1,2 @@ +Fix :func:`compression.zstd.open` naming the wrong mode in the error message +for an invalid mode that contains both ``'t'`` and ``'b'``.