Different behaviour of regexp in 3.6.0b2

Lele Gaifax lele at metapensiero.it
Fri Oct 14 11:40:18 EDT 2016


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?

In the meantime, I will alert the pgcli people.

Thanks in advance,
bye, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
lele at metapensiero.it  |                 -- Fortunato Depero, 1929.




More information about the Python-list mailing list