FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Skip byte strings in extract_python instead of crashing by bysiber · Pull Request #1259 · python-babel/babel · GitHub

Repository navigation

Skip byte strings in extract_python instead of crashing - #1259

Open
bysiber wants to merge 1 commit into
python-babel:masterfrom
bysiber:fix-extract-byte-string-crash
Open

bysiber wants to merge 1 commit into
python-babel:masterfrom
bysiber:fix-extract-byte-string-crash

Conversation

bysiber commented Feb 22, 2026

Copy link
Copy Markdown

When pybabel extract encounters a byte string like _(b'foo'), _parse_python_string returns a bytes object which then causes ''.join(buf) to crash with TypeError: sequence item 0: expected str instance, bytes found.

Since byte strings are not valid translation strings (gettext uses normal strings internally), this just skips them by returning None from _parse_python_string when it encounters bytes.

Added a test that confirms byte strings are silently skipped while normal strings in the same file are still extracted correctly.

Fixes #1190

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.

akx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM, tiny addition request!

Comment thread babel/messages/extract.py
Comment on lines +713 to +714
if isinstance(body.value, bytes):
return None # byte strings are not translatable

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Like for the f-string case below, let's add a note for our future selves that we could warn in this case.

Suggested change
if isinstance(body.value, bytes):
return None # byte strings are not translatable
if isinstance(body.value, bytes):
# TODO: we could raise an error or warning when this occurs
return None # byte strings are not translatable

This branch has not been deployed

No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pybabel extract crashes when it encounters a byte string

2 participants


Back | FazBrowse Home | New Git URL