Skip to content

Skip byte strings in extract_python instead of crashing - #1259

Merged
akx merged 2 commits into
python-babel:masterfrom
bysiber:fix-extract-byte-string-crash
Oct 30, 2026
Merged

akx merged 2 commits into
python-babel:masterfrom
bysiber:fix-extract-byte-string-crash

Conversation

@bysiber

@bysiber bysiber commented Feb 22, 2026

Copy link
Copy Markdown
Contributor

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

@akx 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.

LGTM, tiny addition request!

Comment thread babel/messages/extract.py Resolved
bysiber and others added 2 commits October 30, 2026 12:36
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.
@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.55%. Comparing base (0d2b263) to head (9e804d5).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1259   +/-   ##
=======================================
  Coverage   93.55%   93.55%           
=======================================
  Files          27       27           
  Lines        4931     4933    +2     
=======================================
+ Hits         4613     4615    +2     
  Misses        318      318           
Flag Coverage Δ
macos-14-3.10 92.64% <100.00%> (+<0.01%) ⬆️
macos-14-3.11 92.58% <100.00%> (+<0.01%) ⬆️
macos-14-3.12 92.78% <100.00%> (+<0.01%) ⬆️
macos-14-3.13 92.78% <100.00%> (+<0.01%) ⬆️
macos-14-3.14 92.76% <100.00%> (+<0.01%) ⬆️
macos-14-3.15 92.76% <100.00%> (+<0.01%) ⬆️
macos-14-3.8 92.51% <100.00%> (+<0.01%) ⬆️
macos-14-3.9 92.57% <100.00%> (+<0.01%) ⬆️
macos-14-pypy3.10 92.64% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.10 92.66% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.11 92.60% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.12 92.80% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.13 92.80% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.14 92.78% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.15 92.78% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.8 92.53% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.9 92.59% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-pypy3.10 92.66% <100.00%> (+<0.01%) ⬆️
windows-2022-3.10 92.65% <100.00%> (+<0.01%) ⬆️
windows-2022-3.11 92.59% <100.00%> (+<0.01%) ⬆️
windows-2022-3.12 92.79% <100.00%> (+<0.01%) ⬆️
windows-2022-3.13 92.79% <100.00%> (+<0.01%) ⬆️
windows-2022-3.14 92.77% <100.00%> (+<0.01%) ⬆️
windows-2022-3.15 92.77% <100.00%> (+<0.01%) ⬆️
windows-2022-3.8 92.62% <100.00%> (+<0.01%) ⬆️
windows-2022-3.9 92.58% <100.00%> (+<0.01%) ⬆️
windows-2022-pypy3.10 92.65% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@codspeed

codspeed Bot commented Oct 30, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 88 untouched benchmarks


Comparing bysiber:fix-extract-byte-string-crash (9e804d5) with master (0d2b263)

Open in CodSpeed

@akx
akx merged commit 7b00bf9 into python-babel:master Oct 30, 2026
36 checks passed
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