8000
Skip to content

gh-133049: make pygettext match xgettext for empty input/no messages - #156322

Draft
aryansk wants to merge 1 commit into
python:mainfrom
aryansk:fix-pygettext-no-input-133049
Draft

gh-133049: make pygettext match xgettext for empty input/no messages#156322
aryansk wants to merge 1 commit into
python:mainfrom
aryansk:fix-pygettext-no-input-133049

Conversation

@aryansk
@aryansk aryansk commented Aug 24, 2026
Copy link
Copy Markdown

Fixes #133049

Problem

Tools/i18n/pygettext.py diverges from GNU xgettext in two empty-input cases:

  1. No input file givenxgettext prints xgettext: no input file given and Try 'xgettext --help' … with exit 1. pygettext silently generated an empty messages.pot with only a header.

  2. No messages to extractxgettext produces no output when no strings are found. pygettext still wrote a header-only messages.pot.

Issue #133049 reports both; tomasr8 confirmed the first is clear and should be fixed (input file should be required), while the second is debated but the issue requests xgettext-compatible behavior.

Change

  • In Tools/i18n/pygettext.py:main() — after expanding args via getFilesForName, check if not args:usage(1, "pygettext: no input file given\nTry 'pygettext --help' for more information.")

  • After visiting files, check if not visitor.messages: → verbose message to stderr and sys.exit(0) without creating/writing the output file (covers both -o FILE and - stdout cases). This matches xgettext's "no output is generated if there are no messages".

Keep unrelated cleanup out of this PR.

Why this approach

Minimal, xgettext-compatible, and respects the maintainer's lean: the first fix is uncontroversial and matches the exact xgettext error wording; the second implements the requested "no empty output" while remaining silent unless -v is given, so existing workflows that expect an empty POT can be detected via the exit and verbose hint.

Testing

command: python3.14 Tools/i18n/pygettext.py (no args)
result: prints __doc__ + "pygettext: no input file given\nTry 'pygettext --help' …" to stderr, exit 1 — matches xgettext

command: echo 'print("hello")' > /tmp/test.py && python3.14 Tools/i18n/pygettext.py -o /tmp/out.pot /tmp/test.py; ls /tmp/out.pot
result: no 
8000
out.pot created, exit 0

command: echo '_("hello")' > /tmp/test2.py && python3.14 Tools/i18n/pygettext.py -o /tmp/out2.pot /tmp/test2.py; cat /tmp/out2.pot
result: header + msgid "hello" written correctly

command: python3.14 -m py_compile Tools/i18n/pygettext.py
result: ok (3.13/3.14; 3.9 fails on unrelated PEP 701 f-string syntax already on main)

command: git diff --check
result: clean

make check for pygettext is covered by test.test_tools.test_i18n (skipped in this env due to missing build dir, but git diff --check and manual checks pass).

Documentation and release impact

  • User-facing behavior updated (CLI error / empty-output)
  • Changelog/release note needed: bug fix, no docs
  • Migration or compatibility note needed
  • No documentation impact

Review notes

  • Known limitations: verbose-only hint for empty-messages case
  • Follow-up issue, if any: none
  • Security/licensing considerations: none

…sages

Fixes python#133049

pygettext now matches GNU xgettext behavior:

* No input file given → error "pygettext: no input file given" and
  "Try 'pygettext --help' ..." with exit 1, instead of silently
  generating an empty messages.pot.

* No strings to extract → no output file generated (exit 0, verbose
  message to stderr), instead of writing a header-only messages.pot.
  This matches xgettext which produces no output when there are no
  messages.

Validation: manual check with python3.14 Tools/i18n/pygettext.py (no
args → exit 1 with error, empty file → no out.pot, file with _() →
out.pot with header+msgid); py_compile passes on 3.13/3.14.
@python-cla-bot
python-cla-bot Bot commented Aug 24, 2026
Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
bedevere-app Bot commented Aug 24, 2026
Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pygettext generates output even when there should be none

1 participant

0