jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/816853 )
Change subject: [bugfix] Fix content=True in page.template() ......................................................................
[bugfix] Fix content=True in page.template()
Discard cache and reload it if cache was loaded without content and content is required in subsequent function calls.
Fix also page.itertemplates().
Bug: T313736 Change-Id: Id399b56e208cd1f480ff8256c2919f014c99919c --- M pywikibot/page/_pages.py 1 file changed, 17 insertions(+), 1 deletion(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page/_pages.py b/pywikibot/page/_pages.py index d130a89..9537cd8 100644 --- a/pywikibot/page/_pages.py +++ b/pywikibot/page/_pages.py @@ -19,6 +19,7 @@ # # Distributed under the terms of the MIT license. # +import itertools import re from collections import Counter, defaultdict from contextlib import suppress @@ -401,6 +402,14 @@
return self.latest_revision.text
+ def has_content(self) -> bool: + """ + Page has been loaded. + + Not existing pages are considered loaded. + """ + return not self.exists() or self._latest_cached_revision() is not None + def _latest_cached_revision(self): """Get the latest revision if cached and has text, otherwise None.""" if (hasattr(self, '_revid') and self._revid in self._revisions @@ -1528,6 +1537,12 @@ :param content: bool """ # Data might have been preloaded + # Delete cache if content is needed and elements have no content + if (hasattr(self, '_templates') + and content + and not all(t.has_content() for t in self._templates)): + del self._templates + if not hasattr(self, '_templates'): self._templates = list(self.itertemplates(content=content))
@@ -1549,7 +1564,8 @@ :param content: bool """ if hasattr(self, '_templates'): - return iter(self._templates) + return itertools.islice(self.templates(content=content), total) + return self.site.pagetemplates(self, total=total, content=content)
def imagelinks(self, total: Optional[int] = None, content: bool = False):