Skip to content

Commit 8579327

Browse files
authoredAug 7, 2023
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.
1 parent ed64204 commit 8579327

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed
 

‎Lib/doctest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ def _find_lineno(self, obj, source_lines):
11101110
if source_lines is None:
11111111
return None
11121112
pat = re.compile(r'^\s*class\s*%s\b' %
1113-
getattr(obj, '__name__', '-'))
1113+
re.escape(getattr(obj, '__name__', '-')))
11141114
for i, line in enumerate(source_lines):
11151115
if pat.match(line):
11161116
lineno = i
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix `doctest.DocTestFinder.find` in presence of class names with special characters. Patch by Gertjan van Zwieten.

0 commit comments

Comments
 (0)
Please sign in to comment.