From da8fbb74550d88aed9aaea7cf553ce10e5f4065a Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Fri, 2 May 2025 19:57:43 +0100 Subject: [PATCH 1/4] Fix --- Lib/test/test_tools/test_msgfmt.py | 11 +---------- .../2025-05-02-19-56-00.gh-issue-53950.asdhss.rst | 1 + Tools/i18n/msgfmt.py | 3 +++ 3 files changed, 5 insertions(+), 10 deletions(-) create mode 100644 Misc/NEWS.d/next/Tools-Demos/2025-05-02-19-56-00.gh-issue-53950.asdhss.rst diff --git a/Lib/test/test_tools/test_msgfmt.py b/Lib/test/test_tools/test_msgfmt.py index 7be606bbff606a..6aec1becb7f1e9 100644 --- a/Lib/test/test_tools/test_msgfmt.py +++ b/Lib/test/test_tools/test_msgfmt.py @@ -43,6 +43,7 @@ def test_compilation(self): actual = GNUTranslations(f) self.assertDictEqual(actual._catalog, expected._catalog) + self.assertFalse(msgfmt.MESSAGES) def test_binary_header(self): with temp_cwd(): @@ -145,12 +146,6 @@ def test_generic_syntax_error(self): class POParserTest(unittest.TestCase): - @classmethod - def tearDownClass(cls): - # msgfmt uses a global variable to store messages, - # clear it after the tests. - msgfmt.MESSAGES.clear() - def test_strings(self): # Test that the PO parser correctly handles and unescape # strings in the PO file. @@ -202,8 +197,6 @@ def test_strings(self): # check the result. po = f'msgid {po_string}\nmsgstr "translation"' Path('messages.po').write_text(po) - # Reset the global MESSAGES dictionary - msgfmt.MESSAGES.clear() msgfmt.make('messages.po', 'messages.mo') with open('messages.mo', 'rb') as f: @@ -235,8 +228,6 @@ def test_strings(self): with self.subTest(string=invalid_string): po = f'msgid {invalid_string}\nmsgstr "translation"' Path('messages.po').write_text(po) - # Reset the global MESSAGES dictionary - msgfmt.MESSAGES.clear() with self.assertRaises(Exception): msgfmt.make('messages.po', 'messages.mo') diff --git a/Misc/NEWS.d/next/Tools-Demos/2025-05-02-19-56-00.gh-issue-53950.asdhss.rst b/Misc/NEWS.d/next/Tools-Demos/2025-05-02-19-56-00.gh-issue-53950.asdhss.rst new file mode 100644 index 00000000000000..39b0a972ae41c0 --- /dev/null +++ b/Misc/NEWS.d/next/Tools-Demos/2025-05-02-19-56-00.gh-issue-53950.asdhss.rst @@ -0,0 +1 @@ +Fix bug in :program:`msgfmt` where ``MESSAGES`` was not cleared. diff --git a/Tools/i18n/msgfmt.py b/Tools/i18n/msgfmt.py index cd5f1ed9f3e268..b276c4253addae 100755 --- a/Tools/i18n/msgfmt.py +++ b/Tools/i18n/msgfmt.py @@ -99,6 +99,9 @@ def generate(): def make(filename, outfile): + global MESSAGES + MESSAGES.clear() + ID = 1 STR = 2 CTXT = 3 From 2144581d9a6b98f6e71b4ce7b275a916494334b6 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> Date: Fri, 2 May 2025 20:19:53 +0100 Subject: [PATCH 2/4] Update test_msgfmt.py --- Lib/test/test_tools/test_msgfmt.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_tools/test_msgfmt.py b/Lib/test/test_tools/test_msgfmt.py index 6aec1becb7f1e9..e3f0755b96005a 100644 --- a/Lib/test/test_tools/test_msgfmt.py +++ b/Lib/test/test_tools/test_msgfmt.py @@ -43,7 +43,6 @@ def test_compilation(self): actual = GNUTranslations(f) self.assertDictEqual(actual._catalog, expected._catalog) - self.assertFalse(msgfmt.MESSAGES) def test_binary_header(self): with temp_cwd(): From 8608362e6a84deef3d822cda0c21345a001baf59 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Fri, 2 May 2025 21:13:56 +0100 Subject: [PATCH 3/4] Remove global var --- Tools/i18n/msgfmt.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/Tools/i18n/msgfmt.py b/Tools/i18n/msgfmt.py index b276c4253addae..c58dd339f0c785 100755 --- a/Tools/i18n/msgfmt.py +++ b/Tools/i18n/msgfmt.py @@ -37,9 +37,6 @@ __version__ = "1.2" -MESSAGES = {} - - def usage(code, msg=''): print(__doc__, file=sys.stderr) if msg: @@ -47,9 +44,8 @@ def usage(code, msg=''): sys.exit(code) -def add(ctxt, id, str, fuzzy): +def add(MESSAGES, ctxt, id, str, fuzzy): "Add a non-fuzzy translation to the dictionary." - global MESSAGES if not fuzzy and str: if ctxt is None: MESSAGES[id] = str @@ -57,9 +53,8 @@ def add(ctxt, id, str, fuzzy): MESSAGES[b"%b\x04%b" % (ctxt, id)] = str -def generate(): +def generate(MESSAGES): "Return the generated output." - global MESSAGES # the keys are sorted in the .mo file keys = sorted(MESSAGES.keys()) offsets = [] @@ -99,9 +94,7 @@ def generate(): def make(filename, outfile): - global MESSAGES - MESSAGES.clear() - + MESSAGES = {} ID = 1 STR = 2 CTXT = 3 @@ -143,7 +136,7 @@ def make(filename, outfile): lno += 1 # If we get a comment line after a msgstr, this is a new entry if l[0] == '#' and section == STR: - add(msgctxt, msgid, msgstr, fuzzy) + add(MESSAGES, msgctxt, msgid, msgstr, fuzzy) section = msgctxt = None fuzzy = 0 # Record a fuzzy mark @@ -155,7 +148,7 @@ def make(filename, outfile): # Now we are in a msgid or msgctxt section, output previous section if l.startswith('msgctxt'): if section == STR: - add(msgctxt, msgid, msgstr, fuzzy) + add(MESSAGES, msgctxt, msgid, msgstr, fuzzy) section = CTXT l = l[7:] msgctxt = b'' @@ -172,7 +165,7 @@ def make(filename, outfile): charset = p.parsestr(msgstr.decode(encoding)).get_content_charset() if charset: encoding = charset - add(msgctxt, msgid, msgstr, fuzzy) + add(MESSAGES, msgctxt, msgid, msgstr, fuzzy) msgctxt = None section = ID l = l[5:] @@ -222,10 +215,10 @@ def make(filename, outfile): sys.exit(1) # Add last entry if section == STR: - add(msgctxt, msgid, msgstr, fuzzy) + add(MESSAGES, msgctxt, msgid, msgstr, fuzzy) # Compute output - output = generate() + output = generate(MESSAGES) try: with open(outfile,"wb") as f: From 0582facfd2426f0b734420c919d720197c869f31 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Fri, 2 May 2025 21:40:08 +0100 Subject: [PATCH 4/4] Lowercase --- Tools/i18n/msgfmt.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Tools/i18n/msgfmt.py b/Tools/i18n/msgfmt.py index c58dd339f0c785..225f49f1bf55a4 100755 --- a/Tools/i18n/msgfmt.py +++ b/Tools/i18n/msgfmt.py @@ -44,27 +44,27 @@ def usage(code, msg=''): sys.exit(code) -def add(MESSAGES, ctxt, id, str, fuzzy): +def add(messages, ctxt, id, str, fuzzy): "Add a non-fuzzy translation to the dictionary." if not fuzzy and str: if ctxt is None: - MESSAGES[id] = str + messages[id] = str else: - MESSAGES[b"%b\x04%b" % (ctxt, id)] = str + messages[b"%b\x04%b" % (ctxt, id)] = str -def generate(MESSAGES): +def generate(messages): "Return the generated output." # the keys are sorted in the .mo file - keys = sorted(MESSAGES.keys()) + keys = sorted(messages.keys()) offsets = [] ids = strs = b'' for id in keys: # For each string, we need size and file offset. Each string is NUL # terminated; the NUL does not count into the size. - offsets.append((len(ids), len(id), len(strs), len(MESSAGES[id]))) + offsets.append((len(ids), len(id), len(strs), len(messages[id]))) ids += id + b'\0' - strs += MESSAGES[id] + b'\0' + strs += messages[id] + b'\0' output = '' # The header is 7 32-bit unsigned integers. We don't use hash tables, so # the keys start right after the index tables. @@ -94,7 +94,7 @@ def generate(MESSAGES): def make(filename, outfile): - MESSAGES = {} + messages = {} ID = 1 STR = 2 CTXT = 3 @@ -136,7 +136,7 @@ def make(filename, outfile): lno += 1 # If we get a comment line after a msgstr, this is a new entry if l[0] == '#' and section == STR: - add(MESSAGES, msgctxt, msgid, msgstr, fuzzy) + add(messages, msgctxt, msgid, msgstr, fuzzy) section = msgctxt = None fuzzy = 0 # Record a fuzzy mark @@ -148,7 +148,7 @@ def make(filename, outfile): # Now we are in a msgid or msgctxt section, output previous section if l.startswith('msgctxt'): if section == STR: - add(MESSAGES, msgctxt, msgid, msgstr, fuzzy) + add(messages, msgctxt, msgid, msgstr, fuzzy) section = CTXT l = l[7:] msgctxt = b'' @@ -165,7 +165,7 @@ def make(filename, outfile): charset = p.parsestr(msgstr.decode(encoding)).get_content_charset() if charset: encoding = charset - add(MESSAGES, msgctxt, msgid, msgstr, fuzzy) + add(messages, msgctxt, msgid, msgstr, fuzzy) msgctxt = None section = ID l = l[5:] @@ -215,10 +215,10 @@ def make(filename, outfile): sys.exit(1) # Add last entry if section == STR: - add(MESSAGES, msgctxt, msgid, msgstr, fuzzy) + add(messages, msgctxt, msgid, msgstr, fuzzy) # Compute output - output = generate(MESSAGES) + output = generate(messages) try: with open(outfile,"wb") as f: