[issue47023] re.sub shows key error on regex escape chars provided in repl param

2022-03-17 Thread Eric V. Smith


Eric V. Smith  added the comment:

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.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47023] re.sub shows key error on regex escape chars provided in repl param

2022-03-17 Thread Matthew Barnett


Matthew Barnett  added the comment:

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.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47023] re.sub shows key error on regex escape chars provided in repl param

2022-03-17 Thread Eric V. Smith


Eric V. Smith  added the comment:

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.

--
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed
type: enhancement -> behavior

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47023] re.sub shows key error on regex escape chars provided in repl param

2022-03-17 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

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'

--
nosy: +rhettinger
status: pending -> open

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47023] re.sub shows key error on regex escape chars provided in repl param

2022-03-17 Thread Eric V. Smith


Change by Eric V. Smith :


--
status: open -> pending

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47023] re.sub shows key error on regex escape chars provided in repl param

2022-03-15 Thread Eric V. Smith


Eric V. Smith  added the comment:

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)?

--
nosy: +eric.smith

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47023] re.sub shows key error on regex escape chars provided in repl param

2022-03-15 Thread Siddhesh Sathe


New submission from Siddhesh Sathe :

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 "", line 1, in 
  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
>>>

--
components: Regular Expressions
messages: 415225
nosy: ezio.melotti, mrabarnett, siddheshsathe
priority: normal
severity: normal
status: open
title: re.sub shows key error on regex escape chars provided in repl param
type: enhancement
versions: Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com