You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Regular expressions with 0 to 65536 repetitions raises OverflowError #57378
Regular expressions with 0 to 65536 repetitions and above makes Python crash with a "OverflowError: regular expression code size limit exceeded" exception.
65535 repetitions do not raise this issue.
Tested and confirmed this with versions 2.7.1 and 3.2.2.
C:\Python27>python.exe
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> re.search('(?s)\A.{0,65535}test', 'test')
<_sre.SRE_Match object at 0x00B4E4B8>
>>> re.search('(?s)\A.{0,65536}test', 'test')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\re.py", line 142, in search
return _compile(pattern, flags).search(string)
File "C:\Python27\lib\re.py", line 243, in _compile
p = sre_compile.compile(pattern, flags)
File "C:\Python27\lib\sre_compile.py", line 523, in compile
groupindex, indexgroup
OverflowError: regular expression code size limit exceeded
>>>
C:\Python32>python.exe
Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> re.search('(?s)\A.{0,65535}test', 'test')
<_sre.SRE_Match object at 0x00A6F250>
>>> re.search('(?s)\A.{0,65536}test', 'test')
Traceback (most recent call last):
File "C:\Python32\lib\functools.py", line 176, in wrapper
result = cache[key]
KeyError: (<class 'str'>, '(?s)\\A.{0,65536}test', 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python32\lib\re.py", line 158, in search
return _compile(pattern, flags).search(string)
File "C:\Python32\lib\re.py", line 255, in _compile
return _compile_typed(type(pattern), pattern, flags)
File "C:\Python32\lib\functools.py", line 180, in wrapper
result = user_function(*args, **kwds)
File "C:\Python32\lib\re.py", line 267, in _compile_typed
return sre_compile.compile(pattern, flags)
File "C:\Python32\lib\sre_compile.py", line 514, in compile
groupindex, indexgroup
OverflowError: regular expression code size limit exceeded
>>>
changed the title [-]Regular expressions with 0 to 65536 repetitions and above makes Python crash[/-][+]Regular expressions with 0 to 65536 repetitions raises OverflowError[/+]on Oct 13, 2011
mrabarnett commented on Oct 13, 2011
mrabarnettmannequin
Mannequin
More actions
The quantifiers use 65535 to represent no upper limit, so ".{0,65535}" is equivalent to ".*".
Unfortunately, in Python this does not work and raises a "nothing to repeat" sre_constants error:
re.search('(?s)\A(.{0,65535}){0,3}test', 'test')
This, however works, which yields 65536 repetitions of DOTALL:
re.search('(?s)\A.{0,65535}.{0,1}test', 'test')
In the end this solves my problem sort or less, but requires extra logic in my script and complicates stuff unnecessary.
A suggestion might be to make repetitions of repeats possible?
mrabarnett commented on Oct 14, 2011
mrabarnettmannequin
Mannequin
More actions
The limit is an implementation detail. The pattern is compiled into codes which are then interpreted, and it just happens that the codes are (usually) 16 bits, giving a range of 0..65535, but it uses 65535 to represent no limit and doesn't warn if you actually write 65535.
Matthew, do you think this should be documented somewhere or that the behavior should be changed (e.g. raising a warning when 65535 is used)?
If not I'll just close the issue.
mrabarnett commented on Feb 29, 2012
mrabarnettmannequin
Mannequin
More actions
Ideally, it should raise an exception (or a warning) because the behaviour is unexpected.
IIRC a few days ago I've seen a similar issue and the cause was that they did something wrong while porting the rc to Debian, but I don't remember the details. If I'm not mistaken they also fixed it shortly after.
Activity
techmaurice commentedon Oct 13, 2011
Regular expressions with 0 to 65536 repetitions and above makes Python crash with a "OverflowError: regular expression code size limit exceeded" exception.
65535 repetitions do not raise this issue.
Tested and confirmed this with versions 2.7.1 and 3.2.2.
briancurtin commentedon Oct 13, 2011
I might be missing something, but what's the issue? 65535 is the limit, and doing 65536 gives a clear overflow exception (no crash).
[-]Regular expressions with 0 to 65536 repetitions and above makes Python crash[/-][+]Regular expressions with 0 to 65536 repetitions raises OverflowError[/+]mrabarnett commentedon Oct 13, 2011
The quantifiers use 65535 to represent no upper limit, so ".{0,65535}" is equivalent to ".*".
For example:
but:
techmaurice commentedon Oct 14, 2011
So if I understand correctly, the maximum of 65535 repetitions is by design?
Have tried a workaround by repeating the repetitions by placing it inside a capturing group, which is perfectly legal with Perl regular expressions:
Unfortunately, in Python this does not work and raises a "nothing to repeat" sre_constants error:
re.search('(?s)\A(.{0,65535}){0,3}test', 'test')
This, however works, which yields 65536 repetitions of DOTALL:
re.search('(?s)\A.{0,65535}.{0,1}test', 'test')
In the end this solves my problem sort or less, but requires extra logic in my script and complicates stuff unnecessary.
A suggestion might be to make repetitions of repeats possible?
mrabarnett commentedon Oct 14, 2011
The limit is an implementation detail. The pattern is compiled into codes which are then interpreted, and it just happens that the codes are (usually) 16 bits, giving a range of 0..65535, but it uses 65535 to represent no limit and doesn't warn if you actually write 65535.
There's an alternative regex implementation here:
http://pypi.python.org/pypi/regex
vstinner commentedon Jan 31, 2012
Issue bpo-13914 has been marked as a duplicate of this issue.
ezio-melotti commentedon Feb 29, 2012
Matthew, do you think this should be documented somewhere or that the behavior should be changed (e.g. raising a warning when 65535 is used)?
If not I'll just close the issue.
mrabarnett commentedon Feb 29, 2012
Ideally, it should raise an exception (or a warning) because the behaviour is unexpected.
serhiy-storchaka commentedon Jan 23, 2013
Now RuntimeError is raised in this case.
Here is a patch, which:
13 remaining items
python-dev commentedon Feb 18, 2013
New changeset a80ea934da9a by Serhiy Storchaka in branch '2.7':
Fix issue bpo-13169: Reimport MAXREPEAT into sre_constants.py.
http://hg.python.org/cpython/rev/a80ea934da9a
New changeset a6231ed7bff4 by Serhiy Storchaka in branch '3.2':
Fix issue bpo-13169: Reimport MAXREPEAT into sre_constants.py.
http://hg.python.org/cpython/rev/a6231ed7bff4
New changeset 88c04657c9f1 by Serhiy Storchaka in branch '3.3':
Fix issue bpo-13169: Reimport MAXREPEAT into sre_constants.py.
http://hg.python.org/cpython/rev/88c04657c9f1
New changeset 3dd5be5c4794 by Serhiy Storchaka in branch 'default':
Fix issue bpo-13169: Reimport MAXREPEAT into sre_constants.py.
http://hg.python.org/cpython/rev/3dd5be5c4794
mcgfeller commentedon Apr 4, 2013
I see (under Windows) the same symptoms as reported for Debian under http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=704084.
Python refuses to start.
2.7.4.rc1 Windows 32-bit.
vstinner commentedon Apr 4, 2013
"Python refuses to start. 2.7.4.rc1 Windows 32-bit."
Oh oh. I reopen the issue and set its priority to release blocker.
birkenfeld commentedon Apr 4, 2013
"Python refuses to start." is not a very good description.
mcgfeller commentedon Apr 4, 2013
@georg, the referenced Debian issue (http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=704084) already contains the stack.
birkenfeld commentedon Apr 4, 2013
And this happens when you simply start Python, not executing any code?
Can you start with "python -S", then do "import _sre", and see if it has a _sre.MAXREPEAT attribute?
ezio-melotti commentedon Apr 4, 2013
IIRC a few days ago I've seen a similar issue and the cause was that they did something wrong while porting the rc to Debian, but I don't remember the details. If I'm not mistaken they also fixed it shortly after.
birkenfeld commentedon Apr 4, 2013
Just tested with 2.7.4rc1 32bit on Windows 7; no problem here.
I suspect your 2.7.4rc1 install picks up a python27.dll from an earlier version.
mcgfeller commentedon Apr 4, 2013
Sorry for passing on my confusion, and thanks for your help!
There was indeed an old python.dll lying in one of the places Windows likes to put DLLs. Deleting it resolved the problem.
Thanks again and sorry to use your valuable time.
Best regards, Martin
birkenfeld commentedon Apr 4, 2013
Thanks for the confirmation!