Skip to content

Commit

Permalink
Merge "[IMPR] ignore ValueError durig upcast of FilePage due to inval…
Browse files Browse the repository at this point in the history
…id file extension"
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Jun 18, 2024
2 parents f5b95b4 + d65d6d2 commit cd24693
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
20 changes: 15 additions & 5 deletions pywikibot/pagegenerators/_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ def NewpagesPageGenerator(site: BaseSite | None = None,
namespaces: NamespaceArgType = (0, ),
total: int | None = None
) -> Generator[pywikibot.page.Page, None, None]:
"""
Iterate Page objects for all new titles in a single namespace.
"""Iterate Page objects for all new titles in a single namespace.
:param site: Site for generator results.
:param namespace: namespace to retrieve pages from
Expand Down Expand Up @@ -185,8 +184,14 @@ def RecentChangesPageGenerator(
.. versionchanged:: 8.2
The YieldType depends on namespace. It can be
:class:`pywikibot.Page`, :class:`pywikibot.User`,
:class:`pywikibot.FilePage` or :class:`pywikibot.Category`.
:class:`pywikibot.Page<pywikibot.page.Page>`,
:class:`pywikibot.User<pywikibot.page.User>`,
:class:`pywikibot.FilePage<pywikibot.page.FilePage>` or
:class:`pywikibot.Category<pywikibot.page.Category>`.
.. versionchanged:: 9.2
Ignore :class:`pywikibot.FilePage<pywikibot.page.FilePage>` if it
raises a :exc:`ValueError` during upcast e.g. due to an invaild
file extension.
:param site: Site for generator results.
"""
Expand All @@ -206,7 +211,12 @@ def upcast(gen):
pageclass = pywikibot.Category
else:
pageclass = pywikibot.Page
yield pageclass(site, rc['title'])
try:
yield pageclass(site, rc['title'])
except ValueError:
if pageclass == pywikibot.FilePage:
pywikibot.exception()
raise

if site is None:
site = pywikibot.Site()
Expand Down
14 changes: 5 additions & 9 deletions tests/file_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,16 +415,12 @@ def test_data_item_when_file_exist_but_without_item(self):
)

# Seek to first page without mediainfo.
# Retry loop is for excepting incorrect files
for retry in range(5):
try:
for page in gen:
slots = page.latest_revision.slots
if 'mediainfo' not in slots:
break
for page in gen:
slots = page.latest_revision.slots
if 'mediainfo' not in slots:
break
except ValueError:
pass
else:
self.skipTest('No page found without mediainfo')

item = page.data_item()
self.assertIsInstance(item, pywikibot.MediaInfo)
Expand Down

0 comments on commit cd24693

Please sign in to comment.