Repository navigation
bpo-29995: re.escape() now escapes only special characters. #1007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
394162b
1c1699a
e0c3beb
9524b56
c00c226
8e4547a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -786,13 +786,17 @@ form. | |
|
|
||
| .. function:: escape(string) | ||
|
|
||
| Escape all the characters in pattern except ASCII letters, numbers and ``'_'``. | ||
| Escape special characters in a string. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what the style on naming for functions that take either str or bytes is, but can see argument for "...characters in string or bytes pattern".
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want to avoid the word "pattern". The argument of The name "string" is often used for text and byte strings in this module. It is the name of parameters in many functions and the name of the attribute in the match object.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The normal way to write this would be "escape special characters in pattern", since pattern is the formal argument name in the source. That is, I agree with your previous comment that string should be replaced by pattern. The existing docs even use 'pattern' in the body of the text, so I'm not sure how we ended up with 'string' as the formal argument name in the docs.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opened bpo-30045 for renaming the parameter to "string".
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In #1048 the |
||
| This is useful if you want to match an arbitrary literal string that may | ||
| have regular expression metacharacters in it. | ||
|
|
||
| .. versionchanged:: 3.3 | ||
| The ``'_'`` character is no longer escaped. | ||
|
|
||
| .. versionchanged:: 3.7 | ||
| Only characters that can have special meaning in a regular expression | ||
| are escaped. | ||
|
|
||
|
|
||
| .. function:: purge() | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -241,39 +241,21 @@ def template(pattern, flags=0): | |
| "Compile a template pattern, returning a pattern object" | ||
| return _compile(pattern, flags|T) | ||
|
|
||
| _alphanum_str = frozenset( | ||
| "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890") | ||
| _alphanum_bytes = frozenset( | ||
| b"_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890") | ||
| # SPECIAL_CHARS | ||
| # closing ')', '}' and ']' | ||
| # '-' (a range in character set) | ||
| # '#' (comment) and WHITESPACE (ignored) in verbose mode | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For my own reference,
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| _special_chars_map = {i: '\\' + chr(i) for i in b'()[]{}?*+-|^$\\.# \t\n\r\v\f'} | ||
|
|
||
| def escape(pattern): | ||
| """ | ||
| Escape all the characters in pattern except ASCII letters, numbers and '_'. | ||
| Escape special characters in a string. | ||
| """ | ||
| if isinstance(pattern, str): | ||
| alphanum = _alphanum_str | ||
| s = list(pattern) | ||
| for i, c in enumerate(pattern): | ||
| if c not in alphanum: | ||
| if c == "\000": | ||
| s[i] = "\\000" | ||
| else: | ||
| s[i] = "\\" + c | ||
| return "".join(s) | ||
| return pattern.translate(_special_chars_map) | ||
| else: | ||
| alphanum = _alphanum_bytes | ||
| s = [] | ||
| esc = ord(b"\\") | ||
| for c in pattern: | ||
| if c in alphanum: | ||
| s.append(c) | ||
| else: | ||
| if c == 0: | ||
| s.extend(b"\\000") | ||
| else: | ||
| s.append(esc) | ||
| s.append(c) | ||
| return bytes(s) | ||
| pattern = str(pattern, 'latin1') | ||
| return pattern.translate(_special_chars_map).encode('latin1') | ||
|
|
||
| # -------------------------------------------------------------------- | ||
| # internals | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To
escape(pattern)to match implementation naming?