gh-130647: Add `--omit-header` option to pygettext (#130650)

StanFromIreland <[email protected]>
Newsgroups gmane.comp.python.cvs
Message-ID <[email protected]>
https://github.com/python/cpython/commit/e8158d1a02d6e5e974d4dbc0633b6a3ad960d7a3
commit: e8158d1a02d6e5e974d4dbc0633b6a3ad960d7a3
branch: main
author: Stan Ulbrych <[email protected]>
committer: StanFromIreland <[email protected]>
date: 2026-08-20T10:59:54+01:00
summary:

gh-130647: Add `--omit-header` option to pygettext (#130650)

Co-authored-by: Serhiy Storchaka <[email protected]>

files:
A Lib/test/test_tools/i18n_data/noheader.pot
A Lib/test/test_tools/i18n_data/noheader.py
A Misc/NEWS.d/next/Tools-Demos/2025-02-27-19-00-00.gh-issue-130647.uwda2h.rst
M Lib/test/test_tools/test_i18n.py
M Tools/i18n/pygettext.py

diff --git a/Lib/test/test_tools/i18n_data/noheader.pot b/Lib/test/test_tools/i18n_data/noheader.pot
new file mode 100644
index 000000000000000..834006e42732e56
--- /dev/null
+++ b/Lib/test/test_tools/i18n_data/noheader.pot
@@ -0,0 +1,8 @@
+#: noheader.py:3
+msgid "Foo"
+msgstr ""
+
+#: noheader.py:5
+msgid "Bar"
+msgstr ""
+
diff --git a/Lib/test/test_tools/i18n_data/noheader.py b/Lib/test/test_tools/i18n_data/noheader.py
new file mode 100644
index 000000000000000..23971799af23c67
--- /dev/null
+++ b/Lib/test/test_tools/i18n_data/noheader.py
@@ -0,0 +1,5 @@
+from gettext import gettext as _
+
+_('Foo')
+
+_('Bar')
diff --git a/Lib/test/test_tools/test_i18n.py b/Lib/test/test_tools/test_i18n.py
index 7583b61480e3709..2dafdd3d059afaf 100644
--- a/Lib/test/test_tools/test_i18n.py
+++ b/Lib/test/test_tools/test_i18n.py
@@ -606,6 +606,7 @@ 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'),
+        'noheader.py': ('--omit-header',),
         'multiple_keywords.py': ('--keyword=foo:1c,2,3', '--keyword=foo:1c,2',
                                  '--keyword=foo:1,2',
                                  # repeat a keyword to make sure it is extracted only once
diff --git a/Misc/NEWS.d/next/Tools-Demos/2025-02-27-19-00-00.gh-issue-130647.uwda2h.rst b/Misc/NEWS.d/next/Tools-Demos/2025-02-27-19-00-00.gh-issue-130647.uwda2h.rst
new file mode 100644
index 000000000000000..4e8c1e3f883fb56
--- /dev/null
+++ b/Misc/NEWS.d/next/Tools-Demos/2025-02-27-19-00-00.gh-issue-130647.uwda2h.rst
@@ -0,0 +1 @@
+Add ``--omit-header`` option to :program:`pygettext`.
diff --git a/Tools/i18n/pygettext.py b/Tools/i18n/pygettext.py
index ddf4474d2bce55f..9d0fc184b84eca9 100755
--- a/Tools/i18n/pygettext.py
+++ b/Tools/i18n/pygettext.py
@@ -124,6 +124,15 @@
     --width=columns
         Set width of output to columns.
 
+    --omit-header
+        Do not write header to file.
+
+        This is useful for testing purposes because it eliminates a source of
+        variance for generated .mo files.
+
+        Note: Using this option may lead to an error during compilation or other
+        manipulation if the resulting file is not entirely in ASCII.
+
     -x filename
     --exclude-file=filename
         Specify a file that contains a list of strings that are not be
@@ -629,9 +638,11 @@ def _is_string_const(self, node):
 def write_pot_file(messages, options, fp):
     timestamp = time.strftime('%Y-%m-%d %H:%M%z')
     encoding = fp.encoding if fp.encoding else 'UTF-8'
-    print(pot_header % {'time': timestamp, 'version': __version__,
-                        'charset': encoding,
-                        'encoding': '8bit'}, file=fp)
+
+    if not options.omit_header:
+        print(pot_header % {'time': timestamp, 'version': __version__,
+                            'charset': encoding,
+                            'encoding': '8bit'}, file=fp)
 
     # Sort locations within each message by filename and lineno
     sorted_keys = [
@@ -691,7 +702,7 @@ def main():
             ['extract-all', 'add-comments=?', 'default-domain=', 'escape',
              'help', 'keyword=', 'no-default-keywords',
              'add-location', 'no-location', 'output=', 'output-dir=',
-             'style=', 'verbose', 'version', 'width=', 'exclude-file=',
+             'style=', 'verbose', 'version', 'width=', 'omit-header', 'exclude-file=',
              'docstrings', 'no-docstrings',
              ])
     except getopt.error as msg:
@@ -712,6 +723,7 @@ class Options:
         locationstyle = GNU
         verbose = 0
         width = 78
+        omit_header = False
         excludefilename = ''
         docstrings = 0
         nodocstrings = {}
@@ -764,6 +776,8 @@ class Options:
                 options.width = int(arg)
             except ValueError:
                 usage(1, f'--width argument must be an integer: {arg}')
+        elif opt in ('--omit-header',):
+            options.omit_header = True
         elif opt in ('-x', '--exclude-file'):
             options.excludefilename = arg
         elif opt in ('-X', '--no-docstrings'):

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.