ldml.LocaleScanner.__find(): only Error if no matches found

The existing caller returns early on finding a match, so never ran off
the end of the iteration unless there were no matches. I'll soon be
adding a new caller that wants to iterate all matches, so will run off
the end even when there are some. So only raise the Error if we found
nothing.

Task-number: QTBUG-115158
Change-Id: I1cae4674eb5e83c433554c15ecc4441b756f20eb
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
This commit is contained in:
Edward Welbourne 2024-01-23 21:14:47 +01:00
parent 82f8afe6ba
commit c8b70f4e51

View File

@ -413,7 +413,7 @@ class LocaleScanner (object):
) # Used for month and day names ) # Used for month and day names
def __find(self, xpath): def __find(self, xpath):
retries = [ xpath.split('/') ] retries, foundNone = [ xpath.split('/') ], True
while retries: while retries:
tags, elts, roots = retries.pop(), self.nodes, (self.base.root,) tags, elts, roots = retries.pop(), self.nodes, (self.base.root,)
for selector in tags: for selector in tags:
@ -423,6 +423,8 @@ class LocaleScanner (object):
break break
else: # Found matching elements else: # Found matching elements
if elts:
foundNone = False
# Possibly filter elts to prefer the least drafty ? # Possibly filter elts to prefer the least drafty ?
for elt in elts: for elt in elts:
yield elt yield elt
@ -442,19 +444,23 @@ class LocaleScanner (object):
if not roots: if not roots:
if retries: # Let outer loop fall back on an alias path: if retries: # Let outer loop fall back on an alias path:
break break
sought = '/'.join(tags) if foundNone:
if sought != xpath: sought = '/'.join(tags)
sought += f' (for {xpath})' if sought != xpath:
raise Error(f'All lack child {selector} for {sought} in {self.name}') sought += f' (for {xpath})'
raise Error(f'All lack child {selector} for {sought} in {self.name}')
else: # Found matching elements else: # Found matching elements
if roots:
foundNone = False
for elt in roots: for elt in roots:
yield elt yield elt
sought = '/'.join(tags) if foundNone:
if sought != xpath: sought = '/'.join(tags)
sought += f' (for {xpath})' if sought != xpath:
raise Error(f'No {sought} in {self.name}') sought += f' (for {xpath})'
raise Error(f'No {sought} in {self.name}')
def __currencyDisplayName(self, stem): def __currencyDisplayName(self, stem):
try: try: