launchpad-reviewers team mailing list archive
-
launchpad-reviewers team
-
Mailing list archive
-
Message #29114
[Merge] ~cjwatson/launchpad:list-len-cache-workaround into launchpad:master
Colin Watson has proposed merging ~cjwatson/launchpad:list-len-cache-workaround into launchpad:master.
Commit message:
Cache return value of POFilesByPOTemplates.__len__
Requested reviews:
Launchpad code reviewers (launchpad-reviewers)
For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/429250
This works around https://github.com/python/cpython/issues/84010, which otherwise causes extra SQL statements and hence test failures on Python 3.8-3.10.
--
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:list-len-cache-workaround into launchpad:master.
diff --git a/lib/lp/translations/model/translatedlanguage.py b/lib/lp/translations/model/translatedlanguage.py
index 8299279..3749a16 100644
--- a/lib/lp/translations/model/translatedlanguage.py
+++ b/lib/lp/translations/model/translatedlanguage.py
@@ -25,6 +25,7 @@ class POFilesByPOTemplates:
def __init__(self, templates_collection, language):
self.templates_collection = templates_collection
self.language = language
+ self._count_templates = None
def _getPlaceholderOrPOFile(self, potemplate, pofile):
if pofile is None:
@@ -66,8 +67,14 @@ class POFilesByPOTemplates:
resultset = self._getPOTemplatesAndPOFilesResultSet()
yield from self._getPOFilesForResultSet(resultset)
+ # Cached because `list()` calls `__len__` twice on Python 3.8-3.10. See
+ # https://github.com/python/cpython/issues/84010.
def __len__(self):
- return self.templates_collection.select(POTemplate).count()
+ if self._count_templates is None:
+ self._count_templates = self.templates_collection.select(
+ POTemplate
+ ).count()
+ return self._count_templates
def __bool__(self):
return bool(self.templates_collection.select(POTemplate).any())