Lele Gaifax wrote:

> Hi all,
> 
> trying out pgcli with Python 3.6.0b2 I got an error related to what seem a
> different behaviour, or even a bug, of re.sub().
> 
> The original intent is to replace spaces within a string with the regular
> expression \s+ (see
> 
https://github.com/dbcli/pgcli/blob/master/pgcli/packages/prioritization.py#L11,
> ignore the fact that the re.sub() call seem underoptimal).
> 
> With Python 3.5.2 is straightforward:
> 
>   $ python3.5
>   Python 3.5.2+ (default, Sep 22 2016, 12:18:14)
>   [GCC 6.2.0 20160927] on linux
>   Type "help", "copyright", "credits" or "license" for more information.
>   >>> import re
>   >>> re.sub(r'\s+', r'\s+', 'foo bar')
>   'foo\\s+bar'
> 
> While Python 3.6.0b2 gives:
> 
>   $ python3.6
>   Python 3.6.0b2+ (default, Oct 11 2016, 08:30:05)
>   [GCC 6.2.0 20160927] on linux
>   Type "help", "copyright", "credits" or "license" for more information.
>   >>> import re
>   >>> re.sub(r'\s+', r'\s+', 'foo bar')
>   Traceback (most recent call last):
>     File "/usr/local/python3.6/lib/python3.6/sre_parse.py", line 945, 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/local/python3.6/lib/python3.6/re.py", line 191, in sub
>       return _compile(pattern, flags).sub(repl, string, count)
>     File "/usr/local/python3.6/lib/python3.6/re.py", line 326, in _subx
>       template = _compile_repl(template, pattern)
>     File "/usr/local/python3.6/lib/python3.6/re.py", line 317, in
>     _compile_repl
>       return sre_parse.parse_template(repl, pattern)
>     File "/usr/local/python3.6/lib/python3.6/sre_parse.py", line 948, in
>     parse_template
>       raise s.error('bad escape %s' % this, len(this))
>   sre_constants.error: bad escape \s at position 0
> 
> Accordingly to the documentation
> (https://docs.python.org/3.6/library/re.html#re.sub) “unknown escapes [in
> the repl argument] such as \& are left alone”.
> 
> Am I missing something, or is this a regression?

According to

https://docs.python.org/dev/library/re.html#re.sub

rejection of \s is intentional

"""
Changed in version 3.6: Unknown escapes consisting of '\' and an ASCII 
letter now are errors.
"""

though IMHO the traceback needs a cleanup.

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to