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

Change subject: tests: Skip ApiTimeoutError for logentries tests on lobbypedia
......................................................................

tests: Skip ApiTimeoutError for logentries tests on lobbypedia

- add site and uri attributes to ApiTimeoutError
- catch site attribute of ApiTimeoutError in
  TestLogentriesBase.setUpClass and skip the test for Lobbypedia
- raise ApiTimeoutError with site and uri parameter in WikiBlameMixin
- add **kwargs parameter to WaitingMixin.wait which is passed to the
  ApiTimeoutError if the maximum number of retries is reached
- call WaitingMixin.wait with uri parameter in SparqlQuery
- call WaitingMixin.wait with site and uri parameters in Requests

Bug: T434974
Change-Id: I6bd078aabd31bb43878b9ce94469e9915394ac7c
---
M pywikibot/data/__init__.py
M pywikibot/data/api/_requests.py
M pywikibot/data/sparql.py
M pywikibot/exceptions.py
M pywikibot/page/_toolforge.py
M tests/logentries_tests.py
6 files changed, 61 insertions(+), 7 deletions(-)

Approvals:
  Mahveotm: Looks good to me, but someone else must approve
  jenkins-bot: Verified
  Xqt: Looks good to me, approved




diff --git a/pywikibot/data/__init__.py b/pywikibot/data/__init__.py
index f0ae5b9..02a21ea 100644
--- a/pywikibot/data/__init__.py
+++ b/pywikibot/data/__init__.py
@@ -25,12 +25,18 @@
         request. Starting with 1 if attribute is missing.
     """

-    def wait(self, delay: int | float | None = None) -> None:
+    def wait(self, delay: int | float | None = None, **kwargs) -> None:
         """Determine how long to wait after a failed request.

+        .. version-changed:: 11.7
+           The *kwargs* parameter was added.
+
         :param delay: Minimum time in seconds to wait. Overwrites
             ``retry_wait`` variable if given. The delay doubles each
             retry until ``retry_max`` seconds is reached.
+        :param kwargs: Additional keyword arguments passed to
+            :exc:`exceptions.ApiTimeoutError` if the maximum number of
+            retries is reached.
         """
         if not hasattr(self, 'max_retries'):
             self.max_retries = pywikibot.config.max_retries
@@ -45,7 +51,7 @@

         if self.current_retries > self.max_retries:
             raise pywikibot.exceptions.ApiTimeoutError(
-                'Maximum retries attempted without success.')
+                'Maximum retries attempted without success.', **kwargs)

         # double the next wait, but do not exceed config.retry_max seconds
         delay = delay or self.retry_wait
diff --git a/pywikibot/data/api/_requests.py b/pywikibot/data/api/_requests.py
index b11b235..928f68e 100644
--- a/pywikibot/data/api/_requests.py
+++ b/pywikibot/data/api/_requests.py
@@ -772,7 +772,7 @@
         else:
             return response, use_get

-        self.wait()
+        self.wait(site=self.site, uri=uri)
         return None, use_get

     def _json_loads(self, response: requests.Response) -> dict | None:
diff --git a/pywikibot/data/sparql.py b/pywikibot/data/sparql.py
index ec5b98d..2dc88ea 100644
--- a/pywikibot/data/sparql.py
+++ b/pywikibot/data/sparql.py
@@ -157,7 +157,7 @@
                     raise
             else:
                 break
-            self.wait()
+            self.wait(uri=url)

         try:
             return self.last_response.json()
diff --git a/pywikibot/exceptions.py b/pywikibot/exceptions.py
index 30af4ad..861f2b4 100644
--- a/pywikibot/exceptions.py
+++ b/pywikibot/exceptions.py
@@ -749,7 +749,40 @@

 class ApiTimeoutError(Error):

-    """Request failed with a timeout error."""
+    """Request failed with a timeout error.
+
+    .. version-changed:: 11.5
+       :exc:`TimeoutError` was renamed to :exc:`ApiTimeoutError`
+    .. version-changed:: 11.7
+       The *site* and *uri* attributes and parameters were added.
+
+    :param args: Arguments passed to the base exception.
+    :param site: Site associated with the failed request, if available.
+    :param uri: URI associated with the failed request, if available.
+    """
+
+    def __init__(
+        self,
+        *args: Exception | str,
+        site: pywikibot.site.BaseSite | None = None,
+        uri: str | None = None
+    ) -> None:
+        """Initializer."""
+        super().__init__(*args)
+
+        #: Site associated with the failed request.
+        self.site = site
+        #: URI associated with the failed request.
+        self.uri = uri
+
+    def __repr__(self) -> str:
+        """Return a detailed representation of the exception."""
+        args = ', '.join(repr(arg) for arg in self.args)
+        if self.site is not None:
+            args += f', site={self.site!r}'
+        if self.uri is not None:
+            args += f', uri={self.uri!r}'
+        return f'{type(self).__name__}({args})'


 class MaxlagTimeoutError(ApiTimeoutError):
diff --git a/pywikibot/page/_toolforge.py b/pywikibot/page/_toolforge.py
index bbbeddc..4994d01 100644
--- a/pywikibot/page/_toolforge.py
+++ b/pywikibot/page/_toolforge.py
@@ -190,7 +190,8 @@

             pywikibot.sleep(pywikibot.config.retry_wait)
         else:
-            raise pywikibot.exceptions.ApiTimeoutError('WikiHistory Timeout')
+            raise pywikibot.exceptions.ApiTimeoutError(
+                'WikiHistory Timeout', site=self.site, uri=url)

         length = len(self.text)
         result: list[list[str]] = []
diff --git a/tests/logentries_tests.py b/tests/logentries_tests.py
index 4f5cf15..1443421 100755
--- a/tests/logentries_tests.py
+++ b/tests/logentries_tests.py
@@ -12,7 +12,11 @@
 from contextlib import suppress

 import pywikibot
-from pywikibot.exceptions import HiddenKeyError, NoMoveTargetError
+from pywikibot.exceptions import (
+    ApiTimeoutError,
+    HiddenKeyError,
+    NoMoveTargetError,
+)
 from pywikibot.family import AutoFamily
 from pywikibot.logentries import (
     LogEntryFactory,
@@ -58,6 +62,16 @@
         }
     }

+    @classmethod
+    def setUpClass(cls) -> None:
+        """Skip tests when the Lobbypedia API times out during setup."""
+        try:
+            super().setUpClass()
+        except ApiTimeoutError as e:
+            if e.site and e.site.family.name == 'lobbypedia':
+                raise unittest.SkipTest(f'API timeout on Lobbypedia: {e}')
+            raise
+
     def _get_logentry(self, logtype):
         """Retrieve a single log entry."""
         if self.site_key == 'old':

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1326057?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: I6bd078aabd31bb43878b9ce94469e9915394ac7c
Gerrit-Change-Number: 1326057
Gerrit-PatchSet: 5
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: 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