jenkins-bot has submitted this change and it was merged.
Change subject: page.py: Create a new function to clear cached page attributes ......................................................................
page.py: Create a new function to clear cached page attributes
- Add _cache_attrs as a new attribute to BasePage. It contains a list of possibly cached attributes. - Create a new function called clear_cache inside BasePage that tries to remove all _cache_attrs of the page. (T114624) - Call the above function inside site.deletepage, page.latest_revision_id.deleter, and page.purge methods to make sure that any outdated cached data is cleared. (T126949 and T114625) - Update the related test function in page_tests.py.
Bug: T114625 Bug: T114624 Bug: T126949 Change-Id: Id1a9e9bab704108df2e01ffdc388c0c2924dcdc5 --- M pywikibot/page.py M pywikibot/site.py M tests/page_tests.py 3 files changed, 24 insertions(+), 2 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py index b1a731c..c0c8294 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -89,6 +89,13 @@ Will be subclassed by Page, WikibasePage, and FlowPage. """
+ _cache_attrs = ( + '_text', '_pageid', '_catinfo', '_templates', '_protection', + '_contentmodel', '_langlinks', '_isredir', '_coords', + '_preloadedtext', '_timestamp', '_applicable_protections', + '_flowinfo', '_quality', '_pageprops', '_revid', '_quality_text' + ) + def __init__(self, source, title=u"", ns=0): """ Instantiate a Page object. @@ -1270,12 +1277,22 @@ """ return self.site.watch(self, unwatch)
+ def clear_cache(self): + """Clear the cached attributes of the page.""" + self._revisions = {} + for attr in self._cache_attrs: + try: + delattr(self, attr) + except AttributeError: + pass + def purge(self, **kwargs): """ Purge the server's cache for this page.
@rtype: bool """ + self.clear_cache() return self.site.purgepages([self], **kwargs)
def touch(self, callback=None, botflag=False, **kwargs): @@ -3579,7 +3596,7 @@
@latest_revision_id.deleter def latest_revision_id(self, value): - del self._revid + self.clear_cache()
@staticmethod def _normalizeLanguages(data): diff --git a/pywikibot/site.py b/pywikibot/site.py index 3b16319..4f37f8c 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -5127,6 +5127,8 @@ % err.code, _logger) raise + else: + page.clear_cache() finally: self.unlock_page(page)
diff --git a/tests/page_tests.py b/tests/page_tests.py index 48829f9..9eb7d83 100644 --- a/tests/page_tests.py +++ b/tests/page_tests.py @@ -874,9 +874,12 @@ p = pywikibot.Page(site, u'User:Unicodesnowman/DeleteTest') # Ensure the page exists p.text = 'pywikibot unit test page' - p.save('unit test', botflag=True) + p.save('#redirect[[unit test]]', botflag=True) + self.assertEqual(p.isRedirectPage(), True) # Test deletion p.delete(reason='pywikibot unit test', prompt=False, mark=False) + self.assertEqual(p._pageid, 0) + self.assertEqual(p.isRedirectPage(), False) self.assertRaises(pywikibot.NoPage, p.get, force=True) # Test undeleting last two revisions del_revs = list(p.loadDeletedRevisions())
pywikibot-commits@lists.wikimedia.org