Skip to content

Commit ac9960e

Browse files
committed
Cache return value of POFilesByPOTemplates.__len__
This works around python/cpython#84010, which otherwise causes extra SQL statements and hence test failures on Python 3.8-3.10.
1 parent 0c72108 commit ac9960e

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

‎lib/lp/translations/model/translatedlanguage.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class POFilesByPOTemplates:
2525
def __init__(self, templates_collection, language):
2626
self.templates_collection = templates_collection
2727
self.language = language
28+
self._count_templates = None
2829

2930
def _getPlaceholderOrPOFile(self, potemplate, pofile):
3031
if pofile is None:
@@ -66,8 +67,14 @@ def __iter__(self):
6667
resultset = self._getPOTemplatesAndPOFilesResultSet()
6768
yield from self._getPOFilesForResultSet(resultset)
6869

70+
# Cached because `list()` calls `__len__` twice on Python 3.8-3.10. See
71+
# https://github.com/python/cpython/issues/84010.
6972
def __len__(self):
70-
return self.templates_collection.select(POTemplate).count()
73+
if self._count_templates is None:
74+
self._count_templates = self.templates_collection.select(
75+
POTemplate
76+
).count()
77+
return self._count_templates
7178

7279
def __bool__(self):
7380
return bool(self.templates_collection.select(POTemplate).any())

0 commit comments

Comments
 (0)