jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/874023 )

Change subject: [bugfix] Use a default result for allusers() if next() leads to 
StopIteration
......................................................................

[bugfix] Use a default result for allusers() if next() leads to StopIteration

- use a default result for allusers() if next() leads to StopIteration
- catch the NoUsernameError exception in login.py script and only
  show the exception message instead of raising it
- fix error messages handling

Bug: T326063
Change-Id: Ia41bc9d80fcfd2e9e88542201236ceab87c6508c
---
M pywikibot/login.py
M pywikibot/scripts/login.py
2 files changed, 28 insertions(+), 9 deletions(-)

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




diff --git a/pywikibot/login.py b/pywikibot/login.py
index 1b1b661..e284b98 100644
--- a/pywikibot/login.py
+++ b/pywikibot/login.py
@@ -1,6 +1,6 @@
 """Library to log the bot in to a wiki account."""
 #
-# (C) Pywikibot team, 2003-2022
+# (C) Pywikibot team, 2003-2023
 #
 # Distributed under the terms of the MIT license.
 #
@@ -133,7 +133,7 @@

         try:
             data = self.site.allusers(start=main_username, total=1)
-            user = next(data)
+            user = next(data, {'name': None})
         except APIError as e:
             if e.code == 'readapidenied':
                 pywikibot.warning("Could not check user '{}' exists on {}"
@@ -294,15 +294,16 @@
             self.login_to_site()
         except APIError as e:
             error_code = e.code
-            pywikibot.error(f'Login failed ({error_code}).')
+
+            # TODO: investigate other unhandled API codes
             if error_code in self._api_error:
-                error_msg = 'Username "{}" {} on {}'.format(
+                error_msg = 'Username {!r} {} on {}'.format(
                     self.login_name, self._api_error[error_code], self.site)
                 if error_code in ('Failed', 'FAIL'):
-                    error_msg += f'\n.{e.info}'
+                    error_msg += f'.\n{e.info}'
                 raise NoUsernameError(error_msg)

-            # TODO: investigate other unhandled API codes (bug T75539)
+            pywikibot.error(f'Login failed ({error_code}).')
             if retry:
                 self.password = None
                 return self.login(retry=False)
diff --git a/pywikibot/scripts/login.py b/pywikibot/scripts/login.py
index f47bc20..cbdd9a2 100755
--- a/pywikibot/scripts/login.py
+++ b/pywikibot/scripts/login.py
@@ -47,7 +47,7 @@
    moved to :mod:`pywikibot.scripts` folder
 """
 #
-# (C) Pywikibot team, 2003-2022
+# (C) Pywikibot team, 2003-2023
 #
 # Distributed under the terms of the MIT license.
 #
@@ -58,7 +58,7 @@
 import pywikibot
 from pywikibot import config
 from pywikibot.backports import Tuple, nullcontext
-from pywikibot.exceptions import SiteDefinitionError
+from pywikibot.exceptions import NoUsernameError, SiteDefinitionError
 from pywikibot.login import OauthLoginManager


@@ -109,7 +109,10 @@
     if logout:
         site.logout()
     else:
-        site.login(autocreate=autocreate)
+        try:
+            site.login(autocreate=autocreate)
+        except NoUsernameError as e:
+            pywikibot.error(e)

     user = site.user()
     if user:

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

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ia41bc9d80fcfd2e9e88542201236ceab87c6508c
Gerrit-Change-Number: 874023
Gerrit-PatchSet: 4
Gerrit-Owner: Xqt <i...@gno.de>
Gerrit-Reviewer: Xqt <i...@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
_______________________________________________
Pywikibot-commits mailing list -- pywikibot-commits@lists.wikimedia.org
To unsubscribe send an email to pywikibot-commits-le...@lists.wikimedia.org

Reply via email to