jenkins-bot submitted this change.

View Change

Approvals: jenkins-bot: Verified Xqt: Looks good to me, approved
[IMPR] Avoid empty blocks in textlib

Instead iterating over all elements to find the last match (and cnt),
create a list and extract the needed values from there.

Change-Id: Ia8fd6432771f7e721523212baa8e7672b3f450d3
---
M pywikibot/textlib.py
1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index 788df3a..b54d4f5 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -1671,8 +1671,7 @@

if under_categories:
category = get_regexes('category', site)[0]
- for last_category in category.finditer(newtext):
- pass
+ last_category = list(category.finditer(newtext))[-1]
for reg in under_categories:
special = reg.search(newtext)
if special and not isDisabled(newtext, special.start()):
@@ -2112,29 +2111,31 @@
"""
return to_latin_digits(line)

- def _last_match_and_replace(self, txt: str, pat):
+ def _last_match_and_replace(self,
+ txt: str,
+ pat) -> tuple[str, Match[str] | None]:
"""Take the rightmost match and replace with marker.

It does so to prevent spurious earlier matches.
"""
- m = None
- cnt = 0
- for cnt, m in enumerate(pat.finditer(txt), start=1):
- pass
+ all_matches = list(pat.finditer(txt))
+ cnt = len(all_matches)

- def marker(m):
+ if not cnt:
+ return (txt, None)
+
+ m = all_matches[-1]
+
+ def marker(m: Match[str]):
"""
Replace exactly the same number of matched characters.

- Same number of chars shall be replaced, in order to be able to
- compare pos for matches reliably (absolute pos of a match
+ Same number of chars shall be replaced, in order to be able
+ to compare pos for matches reliably (absolute pos of a match
is not altered by replacement).
"""
return '@' * (m.end() - m.start())

- if not m:
- return (txt, None)
-
# month and day format might be identical (e.g. see bug T71315),
# avoid to wipe out day, after month is matched. Replace all matches
# but the last two (i.e. allow to search for dd. mm.)

To view, visit change 1025406. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ia8fd6432771f7e721523212baa8e7672b3f450d3
Gerrit-Change-Number: 1025406
Gerrit-PatchSet: 6
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged