Skip to content

gh-130197: pygettext: Fix and test the --exclude-file option #131381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Test pygettext --exclude-file option
  • Loading branch information
tomasr8 committed Mar 17, 2025
commit b1080aaafa87495ee87e3ad6a8d048b22e7b4166
27 changes: 27 additions & 0 deletions Lib/test/test_tools/i18n_data/excluded.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2000-01-01 00:00+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: pygettext.py 1.5\n"


#: excluded.py:4
msgid "foo"
msgstr ""

#: excluded.py:12
msgid ""
"msgids containing newlines \n"
"cannot be currently excluded"
msgstr ""

13 changes: 13 additions & 0 deletions Lib/test/test_tools/i18n_data/excluded.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import gettext as _


_('foo')

_('excluded')

_('also excluded')

_('excluded with trailing whitespace ')

_('msgids containing newlines \n'
'cannot be currently excluded')
3 changes: 3 additions & 0 deletions Lib/test/test_tools/i18n_data/excluded.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
excluded
also excluded
excluded with trailing whitespace
15 changes: 14 additions & 1 deletion Lib/test/test_tools/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from textwrap import dedent
from pathlib import Path

from test.support.script_helper import assert_python_ok
from test.support.script_helper import assert_python_failure, assert_python_ok
from test.test_tools import imports_under_tool, skip_if_missing, toolsdir
from test.support.os_helper import temp_cwd, temp_dir

Expand Down Expand Up @@ -516,8 +516,19 @@ def test_parse_keyword_spec(self):
parse_spec(spec)
self.assertEqual(str(cm.exception), message)

def test_missing_exclude_file(self):
"""
Test that an error is raised if the exclude file (passed via
--exclude-file) does not exist.
"""
_, _, stderr = assert_python_failure(self.script,
'--exclude-file=foo.txt')
self.assertIn("Can't read --exclude-file: foo.txt",
stderr.decode('utf-8'))


def extract_from_snapshots():
exclude_file = DATA_DIR / 'excluded.txt'
snapshots = {
'messages.py': (),
'fileloc.py': ('--docstrings',),
Expand All @@ -526,6 +537,8 @@ def extract_from_snapshots():
'custom_keywords.py': ('--keyword=foo', '--keyword=nfoo:1,2',
'--keyword=pfoo:1c,2',
'--keyword=npfoo:1c,2,3', '--keyword=_:1,2'),
# Test excluded msgids with an exclude file
'excluded.py': (f'--exclude-file={exclude_file}',),
}

for filename, args in snapshots.items():
Expand Down
2 changes: 1 addition & 1 deletion Tools/i18n/pygettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ class Options:
if options.excludefilename:
try:
with open(options.excludefilename) as fp:
options.toexclude = fp.readlines()
options.toexclude = fp.read().splitlines()
except IOError:
print(f"Can't read --exclude-file: {options.excludefilename}",
file=sys.stderr)
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy