10000 gh-104400: Add more tests to pygettext by tomasr8 · Pull Request #108173 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-104400: Add more tests to pygettext #108173

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

Merged
merged 21 commits into from
Nov 3, 2024
Merged
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
Prev Previous commit
Next Next commit
Add a CLI command to regenerate snapshots
  • Loading branch information
tomasr8 committed Oct 28, 2024
commit 5fba1bbabf5097bc9a8f74a77a8d8383d38c403b
21 changes: 21 additions & 0 deletions Lib/test/test_tools/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,24 @@ def test_files_list(self):
self.assertIn(f'msgid "{text1}"', data)
self.assertIn(f'msgid "{text2}"', data)
self.assertNotIn(text3, data)


def update_POT_snapshots():
for input_file in DATA_DIR.glob('*.py'):
output_file = input_file.with_suffix('.pot')
contents = input_file.read_text(encoding='utf-8')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have some files with non-UTF-8 encoding.

Since contents is only used to copy a file, you can read/write the binary content.

with temp_cwd(None):
Path(input_file.name).write_text(contents)
assert_python_ok(Test_pygettext.script, '--docstrings', input_file.name)
output = Path('messages.pot').read_text()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you read text, always specify the encoding.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This causes problems on Windows, where the encoding is cp1252 so reading it back as utf8 fails. I don't know how else to get around this besides forcing pygettext to always output utf8 (or adding a configurable parameter). Do you have any suggestions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use -Xutf8 or PYTHONIOENCODING=utf-8 to run pygettext, because the text can be non-encodable with the locale encoding.


output = normalize_POT_file(output)
output_file.write_text(output, encoding='utf-8')


if __name__ == '__main__':
# To regenerate POT files
if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update':
update_POT_snapshots()
sys.exit(0)
unittest.main()
0