python / python/cpython

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

未關閉
#139,873 2 則留言 2 個 reaction 已指派 1 人 在 GitHub 檢視

@StanFromIreland 已經在處理了。

開始於 2025年10月10日。

triaged type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。