Skip to content

Commit 5f36e5c

Browse files
[3.11] gh-107715: Escape class name in regular expression (GH-107716) (GH-107727)
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 8579327) Co-authored-by: Gertjan van Zwieten <[email protected]>
1 parent ec254c5 commit 5f36e5c

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

Lib/doctest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ def _find_lineno(self, obj, source_lines):
11041104
if source_lines is None:
11051105
return None
11061106
pat = re.compile(r'^\s*class\s*%s\b' %
1107-
getattr(obj, '__name__', '-'))
1107+
re.escape(getattr(obj, '__name__', '-')))
11081108
for i, line in enumerate(source_lines):
11091109
if pat.match(line):
11101110
lineno = i
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :meth:`doctest.DocTestFinder.find` in presence of class names with special characters. Patch by Gertjan van Zwieten.

0 commit comments

Comments
 (0)