python / python/cpython

pygettext.py crashes with UnicodeEncodeError when writing .pot files containing emoji

Đang mở
#139,873 2 bình luận 2 reaction 1 người được giao Xem trên GitHub

@StanFromIreland đang làm issue này rồi.

Từ ngày 10/10/2025.

triaged type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
36k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Bug report

Bug description:

Summary:
pygettext.py fails with a UnicodeEncodeError when generating .pot files that contain Unicode characters such as emoji (e.g. ✅). This happens on Windows systems where the default encoding is cp1252, which cannot represent many Unicode symbols.

Steps to Reproduce:

  • Add a translatable string with an emoji to a Python file:
    _("Operation complete ✅")
  • Run pygettext.py to generate the .pot file:
    python pygettext.py -d messages -o messages.pot your_script.py
  • Observe the crash:
    UnicodeEncodeError: 'charmap' codec can't encode character '\u2705' in position 1: character maps to

Cause:
The .pot file is opened using:
fp = open(output_file, 'w')

This defaults to the system encoding (cp1252 on Windows), which cannot encode emoji or other extended Unicode characters.

Suggested Fix:
Update the file-writing line in pygettext.py to explicitly use UTF-8:
fp = open(output_file, 'w', encoding='utf-8')

This ensures compatibility with all Unicode characters and aligns with modern Python practices.

Environment:

  • Python version: 3.14
  • OS: Windows 10
  • Locale: pt_BR
  • Default encoding: cp1252

Related Pull Request:
This issue appears related to PR #132244, which discusses encoding behavior in pygettext.py. However, the PR focuses on testing and does not yet address the default encoding used when writing .pot files.

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Linked PRs
  • gh-139915

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.