From 95a17e585b177786be73601fe954c5ab79f0464c Mon Sep 17 00:00:00 2001 From: Kadir Can Ozden <101993364+bysiber@users.noreply.github.com> Date: Sun, 22 Feb 2026 03:47:43 +0300 Subject: [PATCH] Skip byte strings in extract_python instead of crashing When a byte string like _(b'foo') is encountered during message extraction, _parse_python_string would return the bytes object which then caused a TypeError in ''.join(buf). Byte strings are not valid translation strings anyway, so return None to skip them. Added a test to confirm byte strings are silently skipped while normal strings are still extracted correctly. --- babel/messages/extract.py | 2 ++ tests/messages/test_extract.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/babel/messages/extract.py b/babel/messages/extract.py index 6fad84304..6ebaad8ee 100644 --- a/babel/messages/extract.py +++ b/babel/messages/extract.py @@ -710,6 +710,8 @@ def _parse_python_string(value: str, encoding: str, future_flags: int) -> str | if isinstance(code, ast.Expression): body = code.body if isinstance(body, ast.Constant): + if isinstance(body.value, bytes): + return None # byte strings are not translatable return body.value if isinstance(body, ast.JoinedStr): # f-string if all(isinstance(node, ast.Constant) for node in body.values): diff --git a/tests/messages/test_extract.py b/tests/messages/test_extract.py index 41eda8903..92b6efaed 100644 --- a/tests/messages/test_extract.py +++ b/tests/messages/test_extract.py @@ -180,3 +180,17 @@ def test_issue_1195_2(): 'NOTE: This should still be considered, even if', 'the text is far away', ] + + +def test_extract_byte_string_does_not_crash(): + """Byte strings like _(b'foo') should be skipped without crashing.""" + buf = BytesIO(b"""\ +_(b'foo') +_('hello') +_(b"bar") +_('world') +""") + messages = list(extract.extract('python', buf, {'_': None}, [], {})) + assert len(messages) == 2 + assert messages[0][1] == 'hello' + assert messages[1][1] == 'world'