jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1319540?usp=email )
Change subject: [refactor] Replace unnecessary [\s\S] patterns with DOTALL
where appropriate
......................................................................
[refactor] Replace unnecessary [\s\S] patterns with DOTALL where appropriate
Bug: T432951
Change-Id: I5bbd0c224978127898fdf870b6627d2990132f3b
---
M pywikibot/textlib.py
M tests/textlib_tests.py
M tests/tools_threading_tests.py
3 files changed, 23 insertions(+), 11 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index fc89027..f55eac5 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -241,14 +241,14 @@
"""Return a tag pattern for the given tag name."""
return (
rf'<{ignore_case(tag_name)}(?:>|\s+[^>]*(?<!/)>)' # start tag
- r'[\s\S]*?' # contents
+ r'.*?' # contents
rf'</{ignore_case(tag_name)}\s*>' # end tag
)
def _tag_regex(tag_name: str):
"""Return a compiled tag regex for the given tag name."""
- return re.compile(_tag_pattern(tag_name))
+ return re.compile(_tag_pattern(tag_name), re.DOTALL)
def _create_default_regexes() -> None:
@@ -265,14 +265,14 @@
# categories
'category': (r'\[\[ *(?:%s)\s*:.*?\]\]',
lambda site: '|'.join(site.namespaces[14])),
- 'comment': re.compile(r'<!--[\s\S]*?-->'),
+ 'comment': re.compile(r'<!--.*?-->', re.DOTALL),
# files
'file': (FILE_LINK_REGEX, lambda site: '|'.join(site.namespaces[6])),
# section headers
'header': re.compile(
- r'(?:(?<=\n)|\A)(?:<!--[\s\S]*?-->)*'
- r'(=(?:[^\n]|<!--[\s\S]*?-->)+=)'
- r' *(?:<!--[\s\S]*?--> *)*(?=\n|\Z)'),
+ r'(?:(?<=\n)|\A)(?s:<!--.*?-->)*'
+ r'(=(?:[^\n]|(?s:<!--.*?-->))+=)'
+ r' *(?s:<!--.*?--> *)*(?=\n|\Z)'),
# external links
'hyperlink': compileLinkR(),
# also finds links to foreign sites with preleading ":"
@@ -283,15 +283,15 @@
+ list(site.family.obsolete.keys()))),
# Module invocations (currently only Lua)
'invoke': (
- r'\{\{\s*\#(?:%s):[\s\S]*?\}\}',
+ r'\{\{\s*\#(?:%s):(?s:.*?)\}\}',
lambda site: '|'.join(
ignore_case(mw) for mw in site.getmagicwords('invoke'))),
# this matches internal wikilinks, but also interwiki, categories, and
# images.
'link': re.compile(r'\[\[[^\]|]*(\|[^\]]*)?\]\]'),
# pagelist tag (used in Proofread extension).
- 'pagelist': re.compile(r'<{}[\s\S]*?/>'
- .format(ignore_case('pagelist'))),
+ 'pagelist': re.compile(r'<{}.*?/>'
+ .format(ignore_case('pagelist')), re.DOTALL),
# Wikibase property inclusions
'property': (
r'\{\{\s*\#(?:%s):\s*[Pp]\d+.*?\}\}',
@@ -306,7 +306,8 @@
# source code readability.
# TODO: handle nested tables.
'table': re.compile(
- r'(?:(?<=\n)|\A){\|[\S\s]*?\n\|}|%s' % _tag_pattern('table')),
+ r'(?:(?<=\n)|\A){\|.*?\n\|}|%s' % _tag_pattern('table'),
+ re.DOTALL),
'template': NESTED_TEMPLATE_REGEX,
})
diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py
index 6f1a9a0..e62544e 100755
--- a/tests/textlib_tests.py
+++ b/tests/textlib_tests.py
@@ -1183,6 +1183,17 @@
'\n-->\n', 'x', 'y',
['header'], site=self.site),
'\n<!--\ncomment-->==x==<!--comment\n-->\n')
+ self.assertEqual(textlib.replaceExcept('\n==x<!--\n'
+ 'comment-->==\n', 'x', 'y',
+ ['header'], site=self.site),
+ '\n==x<!--\n'
+ 'comment-->==\n')
+ self.assertEqual(textlib.replaceExcept('\n{|\n x \n|}\n', 'x', 'y',
+ ['table'], site=self.site),
+ '\n{|\n x \n|}\n')
+ self.assertEqual(textlib.replaceExcept('{{#invoke:foo\n|x}}', 'x', 'y',
+ ['invoke'], site=self.site),
+ '{{#invoke:foo\n|x}}')
self.assertEqual(textlib.replaceExcept('<pre>x</pre>', 'x', 'y',
['pre'], site=self.site),
'<pre>x</pre>')
diff --git a/tests/tools_threading_tests.py b/tests/tools_threading_tests.py
index a279cf9..3175063 100755
--- a/tests/tools_threading_tests.py
+++ b/tests/tools_threading_tests.py
@@ -238,7 +238,7 @@
BoundedPoolExecutor(Executor)
with self.assertRaisesRegex(
TypeError,
- r'(duplicate base class |Cannot create a consistent method[\s\S]*)'
+ r'(duplicate base class |Cannot create a consistent method(?s:.*))'
"'?BoundedPoolExecutor'?"
):
BoundedPoolExecutor(BoundedPoolExecutor)
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1319540?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I5bbd0c224978127898fdf870b6627d2990132f3b
Gerrit-Change-Number: 1319540
Gerrit-PatchSet: 3
Gerrit-Owner: Prakhar0804 <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]