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.

Title: sre_parse contains a confusing generic error message
Type: behavior Stage: resolved
Components: Regular Expressions Versions: Python 3.5
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: ajaksu2, eric.araujo, ezio.melotti, petri.lehtinen, pitrou, serhiy.storchaka, torne
Priority: low Keywords: patch

Created on 2009-08-05 17:14 by torne, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
sre_error_msg.diff torne, 2009-08-05 17:14 patch with a nicer error message review
Messages (8)
msg91324 - (view) Author: Torne Wuff (torne) Date: 2009-08-05 17:14
sre_parse raises an exception with the message "syntax error" - very
generic and confusing - in the case where something that looks like a
lookbehind assertion is invalid.

>>> import re
>>> re.match('(?<foo>.*)', 'foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/", line 137, in match
    return _compile(pattern, flags).match(string)
  File "/usr/lib/python2.5/", line 241, in _compile
    raise error, v # invalid expression
sre_constants.error: syntax error

This example is a typo for '(?P<foo>.*)' :)

This is the only case in sre_parse where the message "syntax error" is
used - the others are much more descriptive. Attached patch changes it
to "bad lookbehind assertion type: %s" for python 2.x head; should be
applied to 3.x also.
msg102650 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2010-04-08 22:57
Thanks for the patch, LGTM assuming we don't need a test for this.

Do you think the vague message could be less cryptic for users that didn't want lookbehind (or don't know what it is)?
msg102705 - (view) Author: Torne Wuff (torne) Date: 2010-04-09 09:40
I suspect a better message could be invented, but I'm not sure what :)
msg102825 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-04-11 12:15
In the absence of better propositions, the message in the patch seems more helpful to me than the previous, especially because “lookbehind” is a search term that matches <0.1 wink> on So I’d apply this patch.
msg211179 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2014-02-13 22:03
Serhiy: I meant that the patch looks right to me (“I would apply it”), not that I intended to do it (“I will apply it”)—I’m not set up right now to contribute to CPython again.  Would you mind committing it?

(Edited versions: exception messages are not part of a guaranteed API, but changing them can break things and we don’t do that needlessly in bugfix releases.)
msg211187 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-02-13 22:44
May be. We should compare with regex and perhaps add a test.
msg211189 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-02-13 22:47
And of course we shouldn't use just %s. can be non-printable character.
msg239630 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-03-30 18:30
Current (after issue22364) error message is:

>>> re.match('(?<foo>.*)', 'foo')
Traceback (most recent call last):
sre_constants.error: unknown extension ?<f at position 1

Is it good enough or should be improved? In regex this syntax is legal and no error is raised.
Date User Action Args
2022-04-11 14:56:51adminsetgithub: 50899
2015-04-07 17:37:33serhiy.storchakasetstatus: open -> closed
resolution: out of date
stage: patch review -> resolved
2015-03-30 18:30:16serhiy.storchakasetmessages: + msg239630
2014-02-13 22:47:02serhiy.storchakasetmessages: + msg211189
2014-02-13 22:44:39serhiy.storchakasetassignee: serhiy.storchaka

messages: + msg211187
nosy: + pitrou, ezio.melotti
2014-02-13 22:03:43eric.araujosetversions: + Python 3.5, - Python 2.6, Python 3.1, Python 2.7, Python 3.2
nosy: + serhiy.storchaka

messages: + msg211179

assignee: eric.araujo -> (no value)
2014-01-29 07:36:29serhiy.storchakasetassignee: eric.araujo
2011-10-29 18:39:52petri.lehtinensetnosy: + petri.lehtinen
2010-04-11 12:15:10eric.araujosetnosy: + eric.araujo
messages: + msg102825
2010-04-09 09:40:03tornesetmessages: + msg102705
2010-04-08 22:57:29ajaksu2setpriority: low
versions: - Python 2.5, Python 2.4, Python 3.0
nosy: + ajaksu2

messages: + msg102650

stage: patch review
2009-08-05 17:14:38tornecreate