Skip to content

Commit

Permalink
[IMPR] ignore ValueError durig upcast of FilePage due to invalid file…
Browse files Browse the repository at this point in the history
… extension

Also remove retry loop in
TestFilePageDataItem.test_data_item_when_file_exist_but_without_item
of file_tests which doesn't make any sense because the generator was
empty after the exception. Now let the test fail if an exception might
occur but skip the test if no related FilePage was found.

Bug: T367777
Change-Id: Ife4b1a7e648f770df98d6141902a28e0d7cb62f5
  • Loading branch information
xqt committed Jun 18, 2024
1 parent acd370d commit d65d6d2
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 d65d6d2

Please sign in to comment.