This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: re.sub shows key error on regex escape chars provided in repl param
Type: behavior Stage: resolved
Components: Regular Expressions Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, ezio.melotti, mrabarnett, rhettinger, siddheshsathe
Priority: normal Keywords:

Created on 2022-03-15 08:17 by siddheshsathe, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (6)
msg415225 - (view) Author: Siddhesh Sathe (siddheshsathe) Date: 2022-03-15 08:17
Python 3.9.10 (main, Jan 15 2022, 18:56:52)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> re.sub(r"{\w}", "\s", "Hello! {user}")
Traceback (most recent call last):
  File "/usr/lib/python3.9/sre_parse.py", line 1039, in parse_template
    this = chr(ESCAPES[this][1])
KeyError: '\\s'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.9/re.py", line 210, in sub
    return _compile(pattern, flags).sub(repl, string, count)
  File "/usr/lib/python3.9/re.py", line 327, in _subx
    template = _compile_repl(template, pattern)
  File "/usr/lib/python3.9/re.py", line 318, in _compile_repl
    return sre_parse.parse_template(repl, pattern)
  File "/usr/lib/python3.9/sre_parse.py", line 1042, in parse_template
    raise s.error('bad escape %s' % this, len(this))
re.error: bad escape \s at position 0
>>>
msg415241 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-03-15 13:13
Isn't '\s' covered by: " Unknown escapes of ASCII letters are reserved for future use and treated as errors" (https://docs.python.org/3/library/re.html#re.sub)?
msg415444 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2022-03-17 20:36
There seem to be a number of escaping problems in the OP's example.

Is this what was intended?

>>> re.sub(r"\{(\w+)\}", r"\1", "Hello! {user}")
'Hello! user'
msg415448 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-03-17 21:04
Yes, I assume that's what the OP intended, but then stumbled across the error with '\s'.

In any event, I don't think there's a bug here so I'm going to close this. @siddheshsathe: if you disagree, please respond here.
msg415454 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2022-03-17 22:16
I'd just like to point out that to a user it could _look_ like a bug, that an error occurred while reporting, because the traceback isn't giving a 'clean' report; the stuff about the KeyError is an internal detail.
msg415459 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-03-17 23:14
I agree the error message could be better. Also, "s.error('bad escape %s' % this, len(this))" should probably be "from None", since as @mrabarnett notes the KeyError is an implementation detail.
History
Date User Action Args
2022-04-11 14:59:57adminsetgithub: 91179
2022-03-17 23:14:33eric.smithsetmessages: + msg415459
2022-03-17 22:16:35mrabarnettsetmessages: + msg415454
2022-03-17 21:04:27eric.smithsetstatus: open -> closed
type: enhancement -> behavior
messages: + msg415448

resolution: not a bug
stage: resolved
2022-03-17 20:36:37rhettingersetstatus: pending -> open
nosy: + rhettinger
messages: + msg415444

2022-03-17 13:17:16eric.smithsetstatus: open -> pending
2022-03-15 13:13:15eric.smithsetnosy: + eric.smith
messages: + msg415241
2022-03-15 08:17:57siddheshsathecreate