]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-107715: Escape class name in regular expression (GH-107716)
authorGertjan van Zwieten <git@gjvz.nl>
Mon, 7 Aug 2023 15:24:02 +0000 (18:24 +0300)
committerGitHub <noreply@github.com>
Mon, 7 Aug 2023 15:24:02 +0000 (18:24 +0300)
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.

Lib/doctest.py
Misc/NEWS.d/next/Library/2023-08-07-14-12-07.gh-issue-107715.238r2f.rst [new file with mode: 0644]

index 2776d74bf9b586d8c4b6d11f19d1d768eb96414b..a63df46a112e641b756e4c12f2f2e863d349515c 100644 (file)
@@ -1110,7 +1110,7 @@ class DocTestFinder:
             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 (file)
index 0000000..cd2a5d0
--- /dev/null
@@ -0,0 +1 @@
+Fix `doctest.DocTestFinder.find` in presence of class names with special characters. Patch by Gertjan van Zwieten.