jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1325852?usp=email )

Change subject: throttle: Centralize control file serialization
......................................................................

throttle: Centralize control file serialization

Let _write_file own process ordering and write formatted entries directly.
Avoid sorting the same entries twice or mutating caller-owned lists.

Change-Id: If61ed89012a60f6d2562356fbeef1b426fb7088a
---
M pywikibot/throttle.py
M tests/__init__.py
A tests/throttle_tests.py
3 files changed, 42 insertions(+), 6 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified




diff --git a/pywikibot/throttle.py b/pywikibot/throttle.py
index 34fcae9..b9013a1 100644
--- a/pywikibot/throttle.py
+++ b/pywikibot/throttle.py
@@ -167,13 +167,11 @@

     def _write_file(self, processes) -> None:
         """Write process entries to file."""
-        if not isinstance(processes, list):
-            processes = list(processes)
-        processes.sort(key=lambda p: (p.pid, p.site))
+        processes = sorted(processes, key=lambda p: (p.pid, p.site))

         with suppress(IOError), open(self.ctrlfilename, 'w') as f:
-            for p in processes:
-                f.write(FORMAT_LINE.format_map(p._asdict()))
+            f.writelines(FORMAT_LINE.format_map(p._asdict())
+                         for p in processes)

     def checkMultiplicity(self) -> None:
         """Count running processes for site and set process_multiplicity.
@@ -215,7 +213,7 @@
             if not mysite:
                 del processes[-1]

-            self._write_file(sorted(processes, key=lambda p: p.pid))
+            self._write_file(processes)

             self.process_multiplicity = count
             pywikibot.log(f'Found {count} {mysite} processes running,'
diff --git a/tests/__init__.py b/tests/__init__.py
index e36e031..b3fe091 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -146,6 +146,7 @@
     'tests',
     'textlib',
     'thanks',
+    'throttle',
     'time',
     'timestripper',
     'titletranslate',
diff --git a/tests/throttle_tests.py b/tests/throttle_tests.py
new file mode 100755
index 0000000..46646c7
--- /dev/null
+++ b/tests/throttle_tests.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+#
+# (C) Pywikibot team, 2026
+#
+# Distributed under the terms of the MIT license.
+#
+"""Test cases for the :mod:`throttle` module."""
+from __future__ import annotations
+
+import tempfile
+from pathlib import Path
+
+from pywikibot.throttle import ProcEntry, Throttle
+from tests.aspects import TestCase
+
+
+class ThrottleTestCase(TestCase):
+
+    """Test throttle process file handling."""
+
+    net = False
+
+    def test_process_file_roundtrip(self) -> None:
+        """Test that process entries are written in canonical order."""
+        processes = [
+            ProcEntry('c', 2, 30, 'site-b'),
+            ProcEntry('b', 1, 20, 'site-b'),
+            ProcEntry('a', 1, 10, 'site-a'),
+        ]
+        expected = sorted(processes, key=lambda p: (p.pid, p.site))
+
+        with tempfile.TemporaryDirectory() as directory:
+            throttle = object.__new__(Throttle)
+            throttle.ctrlfilename = Path(directory) / 'throttle.ctrl'
+            throttle._write_file(iter(processes))
+
+            self.assertEqual(list(throttle._read_file()), expected)

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1325852?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: If61ed89012a60f6d2562356fbeef1b426fb7088a
Gerrit-Change-Number: 1325852
Gerrit-PatchSet: 2
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to