mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
[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 8579327879
)
Co-authored-by: Gertjan van Zwieten <git@gjvz.nl>
* Update 2023-08-07-14-12-07.gh-issue-107715.238r2f.rst
---------
Co-authored-by: Gertjan van Zwieten <git@gjvz.nl>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
9c8dfcec64
commit
ea6865242c
2 changed files with 2 additions and 1 deletions
|
@ -1110,7 +1110,7 @@ class DocTestFinder:
|
||||||
if source_lines is None:
|
if source_lines is None:
|
||||||
return None
|
return None
|
||||||
pat = re.compile(r'^\s*class\s*%s\b' %
|
pat = re.compile(r'^\s*class\s*%s\b' %
|
||||||
getattr(obj, '__name__', '-'))
|
re.escape(getattr(obj, '__name__', '-')))
|
||||||
for i, line in enumerate(source_lines):
|
for i, line in enumerate(source_lines):
|
||||||
if pat.match(line):
|
if pat.match(line):
|
||||||
lineno = i
|
lineno = i
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix :meth:`doctest.DocTestFinder.find` in presence of class names with special characters. Patch by Gertjan van Zwieten.
|
Loading…
Add table
Add a link
Reference in a new issue