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

Change subject: [bugfix] upcast to FilePage for a proper extension only
......................................................................

[bugfix] upcast to FilePage for a proper extension only

- also raise ValueError if no file extension is given
- update pagegenerators_tests TestWantedFactoryGenerator accordingly
  and take into account that generator yields a Page instead of a
  FilePage.

Bug: T346889
Change-Id: I4f6b52b456655a64bed54780b973d552b7a9eeda
---
M pywikibot/page/_filepage.py
M tests/pagegenerators_tests.py
M pywikibot/data/api/_generators.py
3 files changed, 27 insertions(+), 5 deletions(-)

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




diff --git a/pywikibot/data/api/_generators.py 
b/pywikibot/data/api/_generators.py
index 3090ef3..e483ef3 100644
--- a/pywikibot/data/api/_generators.py
+++ b/pywikibot/data/api/_generators.py
@@ -11,6 +11,7 @@
 # Distributed under the terms of the MIT license.
 #
 from abc import ABC, abstractmethod
+from contextlib import suppress
 from typing import Union
 from warnings import warn

@@ -719,7 +720,8 @@
         if ns == 2:
             p = pywikibot.User(p)
         elif ns == 6:
-            p = pywikibot.FilePage(p)
+            with suppress(ValueError):
+                p = pywikibot.FilePage(p)
         elif ns == 14:
             p = pywikibot.Category(p)
         update_page(p, pagedata, self.props)
diff --git a/pywikibot/page/_filepage.py b/pywikibot/page/_filepage.py
index 4cb91fe..f9347ad 100644
--- a/pywikibot/page/_filepage.py
+++ b/pywikibot/page/_filepage.py
@@ -55,9 +55,10 @@
         super().__init__(source, title, 6)
         if self.namespace() != 6:
             raise ValueError(f"'{self.title()}' is not in the file namespace!")
-        title = self.title(with_section=False)
-        extension = title.rpartition('.')[2].lower()
-        if extension not in self.site.file_extensions:
+
+        title = self.title(with_ns=False, with_section=False)
+        _, sep, extension = title.rpartition('.')
+        if not sep or extension.lower() not in self.site.file_extensions:
             raise ValueError(
                 f'{title!r} does not have a valid extension '
                 f'({", ".join(self.site.file_extensions)}).'
diff --git a/tests/pagegenerators_tests.py b/tests/pagegenerators_tests.py
index 607617b..f6b90b6 100755
--- a/tests/pagegenerators_tests.py
+++ b/tests/pagegenerators_tests.py
@@ -1435,7 +1435,11 @@
         """Test wantedfiles generator."""
         self.gf.handle_arg('-wantedfiles:5')
         for page in self._generator_with_tests():
-            self.assertIsInstance(page, pywikibot.FilePage)
+            self.assertIsInstance(page, pywikibot.Page)
+            if not isinstance(page, pywikibot.FilePage):
+                with self.assertRaisesRegex(ValueError,
+                                            'does not have a valid extension'):
+                    pywikibot.FilePage(page)

     def test_wanted_templates(self):
         """Test wantedtemplates generator."""

--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/959331
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: I4f6b52b456655a64bed54780b973d552b7a9eeda
Gerrit-Change-Number: 959331
Gerrit-PatchSet: 5
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: JJMC89 <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to