Skip to content

Commit

Permalink
pythongh-107715: Escape class name in regular expression (pythonGH-10…
Browse files Browse the repository at this point in the history
…7716)

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 <git@gjvz.nl>
  • Loading branch information
gertjanvanzwieten authored and miss-islington committed Aug 7, 2023
1 parent e5582bd commit b059c0e
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Lib/doctest.py
Expand Up @@ -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
Expand Down
@@ -0,0 +1 @@
Fix `doctest.DocTestFinder.find` in presence of class names with special characters. Patch by Gertjan van Zwieten.

0 comments on commit b059c0e

Please sign in to comment.