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

Change subject: IMPR: support Queue.shutdown() in Python 3.13+ with fallback 
for 3.9–3.12
......................................................................

IMPR: support Queue.shutdown() in Python 3.13+ with fallback for 3.9–3.12

- Use queue.shutdown() when available (Python 3.13+) in _flush()
  to stop the async_manager queue cleanly.
- Provide a dummy ShutDown exception for Python 3.9–3.12 so that
  async_manager can use the same exception handling path.
- Make the 'block' parameter of async_manager keyword-only.

Change-Id: I12a6dad12889b90d9330137ea2ae529aee8ec2b2
---
M pywikibot/__init__.py
1 file changed, 31 insertions(+), 9 deletions(-)

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




diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index abdd4a1..cfcf215 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -1,6 +1,6 @@
 """The initialization file for the Pywikibot framework."""
 #
-# (C) Pywikibot team, 2008-2025
+# (C) Pywikibot team, 2008-2026
 #
 # Distributed under the terms of the MIT license.
 #
@@ -61,12 +61,23 @@
 )
 from pywikibot.site import BaseSite as _BaseSite
 from pywikibot.time import Timestamp
-from pywikibot.tools import normalize_username
+from pywikibot.tools import (
+    PYTHON_VERSION,
+    deprecated_signature,
+    normalize_username,
+)


 if TYPE_CHECKING:
     from pywikibot.site import APISite

+if PYTHON_VERSION >= (3, 13):
+    from queue import ShutDown
+else:
+    class ShutDown(Exception):
+
+        """Dummy exception for Python 3.9-3.12."""
+

 __all__ = (
     '__copyright__', '__url__', '__version__',
@@ -334,18 +345,22 @@
     debug('_flush() called')

     def remaining() -> tuple[int, datetime.timedelta]:
+        """Calculate remaining pages and seconds."""
         remaining_pages = page_put_queue.qsize()
-        if stop:
+        if stop and PYTHON_VERSION < (3, 13):
             # -1 because we added a None element to stop the queue
             remaining_pages -= 1

         remaining_seconds = datetime.timedelta(
-            seconds=round(remaining_pages * _config.put_throttle))
+            seconds=round(remaining_pages * max(_config.put_throttle, 1)))
         return (remaining_pages, remaining_seconds)

     if stop:
-        # None task element leaves async_manager
-        page_put_queue.put((None, [], {}))
+        if PYTHON_VERSION >= (3, 13):
+            page_put_queue.shutdown()
+        else:
+            # None task element leaves async_manager
+            page_put_queue.put((None, [], {}))

     num, sec = remaining()
     if num > 0 and sec.total_seconds() > _config.noisysleep:
@@ -384,9 +399,13 @@


 # Create a separate thread for asynchronous page saves (and other requests)
-def async_manager(block=True) -> None:
+@deprecated_signature(since='11.0.0')
+def async_manager(*, block=True) -> None:
     """Daemon to take requests from the queue and execute them in background.

+    .. versionchanged:: 11.0
+       *block* must be given as keyword argument.
+
     :param block: If true, block :attr:`page_put_queue` if necessary
         until a request is available to process. Otherwise process a
         request if one is immediately available, else leave the function.
@@ -395,10 +414,13 @@
         if not block and page_put_queue.empty():
             break

-        (request, args, kwargs) = page_put_queue.get(block)
+        try:
+            request, args, kwargs = page_put_queue.get(block)
+        except ShutDown:
+            break

         with _page_put_queue_busy:
-            if request is None:
+            if request is None:  # Python < 3.13 not handled by ShutDown
                 break
             request(*args, **kwargs)
             page_put_queue.task_done()

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1221585?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: I12a6dad12889b90d9330137ea2ae529aee8ec2b2
Gerrit-Change-Number: 1221585
Gerrit-PatchSet: 7
Gerrit-Owner: Xqt <[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