babel icon indicating copy to clipboard operation
babel copied to clipboard

`pybabel extract` crashes when it encounters a byte string

Open tomasr8 opened this issue 11 months ago • 2 comments

(Happens on current master)

Sample file:

_(b'foo')

now run pybabel extract test.py -o messages.pot and you'll get this error:

> pybabel extract test.py -o messages.pot
extracting messages from test.py
Traceback (most recent call last):
  File "/home/dev/babel/venv/bin/pybabel", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/dev/babel/babel/messages/frontend.py", line 998, in main
    return CommandLineInterface().run(sys.argv)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/dev/babel/babel/messages/frontend.py", line 924, in run
    return cmdinst.run()
           ^^^^^^^^^^^^^
  File "/home/dev/babel/babel/messages/frontend.py", line 522, in run
    for filename, lineno, message, comments, context in extracted:
                                                        ^^^^^^^^^
  File "/home/dev/babel/babel/messages/extract.py", line 282, in check_and_call_extract_file
    for message_tuple in extract_from_file(
                         ^^^^^^^^^^^^^^^^^^
  File "/home/dev/babel/babel/messages/extract.py", line 324, in extract_from_file
    return list(extract(method, fileobj, keywords, comment_tags,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/dev/babel/babel/messages/extract.py", line 451, in extract
    for lineno, funcname, messages, comments in results:
                                                ^^^^^^^
  File "/home/dev/babel/babel/messages/extract.py", line 562, in extract_python
    messages.append(''.join(buf))
                    ^^^^^^^^^^^^
TypeError: sequence item 0: expected str instance, bytes found

It seems like we're trying to call .join() on a byte string. I suggest just fixing the crash but keeping byte strings disallowed - they won't work with gettext anyway because gettext uses normal strings internally.

tomasr8 avatar Feb 13 '25 15:02 tomasr8

Agreed. We should warn about that and leave it non-extracted.

akx avatar Mar 03 '25 15:03 akx

I'll put it on my todo list for this weekend

tomasr8 avatar Mar 05 '25 11:03 tomasr8