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

Change subject: [Bugfix] Fix i18n.translate calls
......................................................................

[Bugfix] Fix i18n.translate calls

i18n.translate() does not raise a KeyError if the key does not exists
but just returns None. Therefore use the following variants:

- suppress TypeError if the result is to be unpacked in more than one
  variable
- check whether the result is None to continue
- add additional checks to solve_disambiguation.py

Bug: T219094
Change-Id: Iae2ac3af3229be160940062f3f2f14d6050ec435
---
M pywikibot/cosmetic_changes.py
M scripts/category.py
M scripts/commonscat.py
M scripts/imagetransfer.py
M scripts/interwiki.py
M scripts/solve_disambiguation.py
6 files changed, 47 insertions(+), 55 deletions(-)

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




diff --git a/pywikibot/cosmetic_changes.py b/pywikibot/cosmetic_changes.py
index ffc66cd..c397430 100644
--- a/pywikibot/cosmetic_changes.py
+++ b/pywikibot/cosmetic_changes.py
@@ -50,7 +50,7 @@
                                      'your_script_name_2']
 """
 #
-# (C) Pywikibot team, 2006-2024
+# (C) Pywikibot team, 2006-2025
 #
 # Distributed under the terms of the MIT license.
 #
@@ -373,11 +373,9 @@
         if not self.talkpage:
             subpage = False
             if self.template:
-                try:
-                    tmpl, loc = i18n.translate(self.site.code, moved_links)
-                    del tmpl
-                except KeyError:
-                    loc = None
+                loc = None
+                with suppress(TypeError):
+                    _tmpl, loc = i18n.translate(self.site.code, moved_links)
                 if loc is not None and loc in self.title:
                     subpage = True

diff --git a/scripts/category.py b/scripts/category.py
index 7cd95e1..100b4ef 100755
--- a/scripts/category.py
+++ b/scripts/category.py
@@ -153,7 +153,7 @@
    :mod:`pagegenerators` are supported with "move" and "remove" action.
 """
 #
-# (C) Pywikibot team, 2004-2024
+# (C) Pywikibot team, 2004-2025
 #
 # Distributed under the terms of the MIT license.
 #
@@ -301,7 +301,7 @@
             return page

         tmpl: Sequence = []
-        with suppress(KeyError):
+        with suppress(TypeError):
             tmpl, _loc = i18n.translate(page.site.code, moved_links)

         if not isinstance(tmpl, list):
diff --git a/scripts/commonscat.py b/scripts/commonscat.py
index 1c7c4e0..298a8ba 100755
--- a/scripts/commonscat.py
+++ b/scripts/commonscat.py
@@ -40,7 +40,7 @@
 # *Found one template. Add this template
 # *Found more templates. Ask the user <- still have to implement this
 #
-# (C) Pywikibot team, 2008-2024
+# (C) Pywikibot team, 2008-2025
 #
 # Distributed under the terms of the MIT license.
 #
@@ -260,10 +260,8 @@
     @staticmethod
     def skipPage(page) -> bool:
         """Determine if the page should be skipped."""
-        try:
-            templates_to_ignore = i18n.translate(page.site.code,
-                                                 ignoreTemplates)
-        except KeyError:
+        templates_to_ignore = i18n.translate(page.site.code, ignoreTemplates)
+        if not templates_to_ignore:
             return False

         for template in templates_to_ignore:
diff --git a/scripts/imagetransfer.py b/scripts/imagetransfer.py
index a1820a7..89855f9 100755
--- a/scripts/imagetransfer.py
+++ b/scripts/imagetransfer.py
@@ -43,7 +43,7 @@
 &params;
 """
 #
-# (C) Pywikibot team, 2004-2024
+# (C) Pywikibot team, 2004-2025
 #
 # Distributed under the terms of the MIT license.
 #
@@ -206,7 +206,7 @@

         :return: the filename which was used to upload the image
         """
-        def delete_source(old_filename, target_filename):
+        def delete_source(_old_filename: str, target_filename: str) -> None:
             """Delete source image or tag nowCommons template to it.

             This function is called when upload to Commons was
@@ -223,16 +223,14 @@
                and sourceImagePage.delete(reason):
                 return

-            if sourceSite.lang in nowCommonsTemplate \
-               and sourceSite.family.name in config.usernames \
-               and sourceSite.lang in config.usernames[sourceSite.family.name]:
+            tmpl = i18n.translate(sourceSite.code, nowCommonsTemplate)
+            if tmpl and sourceSite.family.name in config.usernames \
+               and sourceSite.code in config.usernames[sourceSite.family.name]:
                 # add the nowCommons template.
                 pywikibot.info('Adding nowCommons template to '
                                + sourceImagePage.title())
                 sourceImagePage.put(sourceImagePage.get() + '\n\n'
-                                    + i18n.translate(sourceSite.code,
-                                                     nowCommonsTemplate)
-                                    % target_filename,
+                                    + tmpl % target_filename,
                                     summary=reason)

         sourceSite = sourceImagePage.site
diff --git a/scripts/interwiki.py b/scripts/interwiki.py
index e357276..de7913c 100755
--- a/scripts/interwiki.py
+++ b/scripts/interwiki.py
@@ -343,7 +343,7 @@
 ``-continue`` next time.
 """
 #
-# (C) Pywikibot team, 2003-2024
+# (C) Pywikibot team, 2003-2025
 #
 # Distributed under the terms of the MIT license.
 #
@@ -1864,9 +1864,8 @@
                     continue
                 if page.namespace() == 10:
                     loc = None
-                    with suppress(KeyError):
-                        tmpl, loc = i18n.translate(page.site.code, moved_links)
-                        del tmpl
+                    with suppress(TypeError):
+                        _tpl, loc = i18n.translate(page.site.code, moved_links)
                     if loc is not None and loc in page.title():
                         pywikibot.info(
                             f'Skipping: {page.title()} is a templates subpage')
@@ -2084,19 +2083,18 @@
 def botMayEdit(page) -> bool:
     """Test for allowed edits."""
     tmpl = []
-    with suppress(KeyError):
+    with suppress(TypeError):
         tmpl, _ = i18n.translate(page.site.code, moved_links)

     if not isinstance(tmpl, list):
         tmpl = [tmpl]

-    with suppress(KeyError):
-        tmpl += i18n.translate(page.site.code, ignoreTemplates,
-                               fallback=i18n.DEFAULT_FALLBACK)
+    with suppress(TypeError):
+        tmpl += i18n.translate(page.site.code, ignoreTemplates)

-    tmpl += i18n.translate('_default', ignoreTemplates,
-                           fallback=i18n.DEFAULT_FALLBACK)
-    if tmpl != []:
+    tmpl += i18n.translate('_default', ignoreTemplates)
+
+    if tmpl:
         templates = page.templatesWithParams()
         for template in templates:
             if template[0].title(with_ns=False).lower() in tmpl:
diff --git a/scripts/solve_disambiguation.py b/scripts/solve_disambiguation.py
index ef114b1..fd530e8 100755
--- a/scripts/solve_disambiguation.py
+++ b/scripts/solve_disambiguation.py
@@ -74,7 +74,7 @@

 """
 #
-# (C) Pywikibot team, 2003-2024
+# (C) Pywikibot team, 2003-2025
 #
 # Distributed under the terms of the MIT license.
 #
@@ -1025,24 +1025,25 @@
         :return: True if everything goes fine, False otherwise
         """
         if page.isRedirectPage() and not self.opt.primary:
+            topic = None
             primary = i18n.translate(page.site,
                                      self.primary_redir_template)
             if primary:
-                primary_page = pywikibot.Page(page.site,
-                                              'Template:' + primary)
-            if primary and primary_page in page.itertemplates(
+                primary_page = pywikibot.Page(page.site, 'Template:' + primary)
+                topic = i18n.translate(self.site.code, primary_topic_format)
+
+            if topic and primary_page in page.itertemplates(
                     namespaces=Namespace.TEMPLATE):
                 baseTerm = page.title()
                 for template, params in page.templatesWithParams():
                     if params and template == primary_page:
                         baseTerm = params[1]
                         break
-                disambTitle = i18n.translate(
-                    self.site.lang,
-                    primary_topic_format) % baseTerm
+
+                disamb_title = topic % baseTerm
                 try:
                     page2 = pywikibot.Page(
-                        pywikibot.Link(disambTitle, self.site))
+                        pywikibot.Link(disamb_title, self.site))
                     links = page2.linkedPages()
                     if self.opt.first:
                         links = self.firstize(page2, links)
@@ -1050,7 +1051,7 @@
                              for link in links]
                 except NoPageError:
                     pywikibot.info(
-                        f'No page at {disambTitle}, using redirect target.')
+                        f'No page at {disamb_title}, using redirect target.')
                     links = page.linkedPages()[:1]
                     links = [correctcap(link,
                                         page.get(get_redirect=True))
@@ -1065,34 +1066,30 @@
                     user_input = pywikibot.input("""\
 Please enter the name of the page where the redirect should have pointed at,
 or press enter to quit:""")
-                    if user_input == '':
-                        self.quit()
-                    else:
-                        self.opt.pos.append(user_input)
+                    if not user_input:
+                        self.quit()  # raises QuitKeyboardInterrupt
+
+                    self.opt.pos.append(user_input)
                 except IsNotRedirectPageError:
                     pywikibot.info(
                         'The specified page is not a redirect. Skipping.')
                     return False
+
         elif self.opt.just:
             # not page.isRedirectPage() or self.opt.primary
             try:
-                if self.opt.primary:
+                topic = i18n.translate(self.site.lang, primary_topic_format)
+                if topic and self.opt.primary:
                     try:
-                        page2 = pywikibot.Page(
-                            pywikibot.Link(
-                                i18n.translate(self.site.lang,
-                                               primary_topic_format)
-                                % page.title(),
-                                self.site))
+                        page2 = pywikibot.Page(self.site, topic % page.title())
                         links = page2.linkedPages()
                         if self.opt.first:
                             links = self.firstize(page2, links)
                         links = [correctcap(link, page2.get())
                                  for link in links]
                     except NoPageError:
-                        pywikibot.info(
-                            'Page does not exist; using first '
-                            f'link in page {page.title()}.')
+                        pywikibot.info('Page does not exist; using first '
+                                       f'link in page {page.title()}.')
                         links = page.linkedPages()[:1]
                         links = [correctcap(link, page.get())
                                  for link in links]
@@ -1106,10 +1103,13 @@
                     except NoPageError:
                         pywikibot.info('Page does not exist, skipping.')
                         return False
+
             except IsRedirectPageError:
                 pywikibot.info('Page is a redirect, skipping.')
                 return False
+
             self.opt.pos += links
+
         return True

     def setSummaryMessage(

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