[Python-checkins] [3.12] gh-107715: Escape class name in regular expression (GH-107716) (#107726)

Yhg1s webhook-mailer at python.org
Wed Aug 16 08:29:50 EDT 2023


https://github.com/python/cpython/commit/ea6865242c184c8afc816e0211652f0d04d9b8f8
commit: ea6865242c184c8afc816e0211652f0d04d9b8f8
branch: 3.12
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Yhg1s <thomas at python.org>
date: 2023年08月16日T14:29:42+02:00
summary:
[3.12] gh-107715: Escape class name in regular expression (GH-107716) (#107726)
* gh-107715: Escape class name in regular expression (GH-107716)
This patch escapes the class name before embedding it in the regular expression
for `pat` in `doctest.DocTestFinder._find_lineno`. While class names do not
ordinarily contain special characters, it is possible to encounter these when a
class is created dynamically. Escaping the name will correctly return `None` in
this scenario, rather than potentially matching a different class or raising
`re.error` depending on the symbols used.
(cherry picked from commit 85793278793708ad6b7132a54ac9fb4b2c5bcac1)
Co-authored-by: Gertjan van Zwieten <git at gjvz.nl>
* Update 2023年08月07日-14-12-07.gh-issue-107715.238r2f.rst
---------
Co-authored-by: Gertjan van Zwieten <git at gjvz.nl>
Co-authored-by: Serhiy Storchaka <storchaka at gmail.com>
files:
A Misc/NEWS.d/next/Library/2023-08-07-14-12-07.gh-issue-107715.238r2f.rst
M Lib/doctest.py
diff --git a/Lib/doctest.py b/Lib/doctest.py
index 2776d74bf9b58..a63df46a112e6 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -1110,7 +1110,7 @@ def _find_lineno(self, obj, source_lines):
 if source_lines is None:
 return None
 pat = re.compile(r'^\s*class\s*%s\b' %
- getattr(obj, '__name__', '-'))
+ re.escape(getattr(obj, '__name__', '-')))
 for i, line in enumerate(source_lines):
 if pat.match(line):
 lineno = i
diff --git a/Misc/NEWS.d/next/Library/2023-08-07-14-12-07.gh-issue-107715.238r2f.rst b/Misc/NEWS.d/next/Library/2023-08-07-14-12-07.gh-issue-107715.238r2f.rst
new file mode 100644
index 0000000000000..4bf08c071df2f
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-08-07-14-12-07.gh-issue-107715.238r2f.rst
@@ -0,0 +1 @@
+Fix :meth:`doctest.DocTestFinder.find` in presence of class names with special characters. Patch by Gertjan van Zwieten.


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /