python / python/cpython

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

オープン
#139,873 コメント 2 件 リアクション 2 件 担当者 1 名 GitHub で見る

@StanFromIreland がすでに取り組んでいます。

2025年10月10日 から。

triaged type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。