[New-bugs-announce] [issue27471] sre_constants.error: bad escape \d

Noah Petherbridge report at bugs.python.org
Fri Jul 8 17:57:07 EDT 2016


New submission from Noah Petherbridge:

I found a bug in Python 3.6.0a2 that wasn't present on previous versions of Python concerning the "\d" escape sequence as used in the following regular expression:

import re
s = "hello"
s = re.sub(re.escape(r'(\d+?)'), '(?:\d+?)', s)

(The purpose of this regular expression was to translate the literal regexp string "(\d+?)" to be a non-capturing literal regexp string, to eventually be used as a re pattern).

When running this code in 3.6.0a2 I receive the following stack traces:

- - - - - - - - - -

Traceback (most recent call last):
  File "/home/kirsle/.pyenv/versions/3.6.0a2/lib/python3.6/sre_parse.py", line 877, in parse_template
    this = chr(ESCAPES[this][1])
KeyError: '\\d'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    s = re.sub(re.escape(r'(\d+?)'), '(?:\d+?)', s)
  File "/home/kirsle/.pyenv/versions/3.6.0a2/lib/python3.6/re.py", line 181, in sub
    return _compile(pattern, flags).sub(repl, string, count)
  File "/home/kirsle/.pyenv/versions/3.6.0a2/lib/python3.6/re.py", line 324, in _subx
    template = _compile_repl(template, pattern)
  File "/home/kirsle/.pyenv/versions/3.6.0a2/lib/python3.6/re.py", line 311, in _compile_repl
    p = sre_parse.parse_template(repl, pattern)
  File "/home/kirsle/.pyenv/versions/3.6.0a2/lib/python3.6/sre_parse.py", line 880, in parse_template
    raise s.error('bad escape %s' % this, len(this))
sre_constants.error: bad escape \d at position 3

- - - - - - - - - -

However, the script runs without crashing on Python 3.5.1 and 2.7.11

% python --version
Python 3.6.0a2

----------
components: Regular Expressions
messages: 270010
nosy: Noah Petherbridge, ezio.melotti, mrabarnett
priority: normal
severity: normal
status: open
title: sre_constants.error: bad escape \d
type: crash
versions: Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue27471>
_______________________________________


More information about the New-bugs-announce mailing list