Issue #1160: Fix compiling large regular expressions on UCS2 builds.

Patch by Serhiy Storchaka.
This commit is contained in:
Antoine Pitrou 2012-11-20 22:30:42 +01:00
parent e78f12f7c2
commit b83ea144cc
4 changed files with 17 additions and 9 deletions

View File

@ -425,6 +425,12 @@ class ReTests(unittest.TestCase):
self.assertEqual(re.match(u"([\u2222\u2223])", self.assertEqual(re.match(u"([\u2222\u2223])",
u"\u2222", re.UNICODE).group(1), u"\u2222") u"\u2222", re.UNICODE).group(1), u"\u2222")
def test_big_codesize(self):
# Issue #1160
r = re.compile('|'.join(('%d'%x for x in range(10000))))
self.assertIsNotNone(r.match('1000'))
self.assertIsNotNone(r.match('9999'))
def test_anyall(self): def test_anyall(self):
self.assertEqual(re.match("a.b", "a\nb", re.DOTALL).group(0), self.assertEqual(re.match("a.b", "a\nb", re.DOTALL).group(0),
"a\nb") "a\nb")

View File

@ -151,6 +151,9 @@ Core and Builtins
Library Library
------- -------
- Issue #1160: Fix compiling large regular expressions on UCS2 builds.
Patch by Serhiy Storchaka.
- Issue #14313: zipfile now raises NotImplementedError when the compression - Issue #14313: zipfile now raises NotImplementedError when the compression
type is unknown. type is unknown.

View File

@ -2675,6 +2675,13 @@ _compile(PyObject* self_, PyObject* args)
PyObject *o = PyList_GET_ITEM(code, i); PyObject *o = PyList_GET_ITEM(code, i);
unsigned long value = PyInt_Check(o) ? (unsigned long)PyInt_AsLong(o) unsigned long value = PyInt_Check(o) ? (unsigned long)PyInt_AsLong(o)
: PyLong_AsUnsignedLong(o); : PyLong_AsUnsignedLong(o);
if (value == (unsigned long)-1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
PyErr_SetString(PyExc_OverflowError,
"regular expression code size limit exceeded");
}
break;
}
self->code[i] = (SRE_CODE) value; self->code[i] = (SRE_CODE) value;
if ((unsigned long) self->code[i] != value) { if ((unsigned long) self->code[i] != value) {
PyErr_SetString(PyExc_OverflowError, PyErr_SetString(PyExc_OverflowError,
@ -3035,10 +3042,8 @@ _validate_inner(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups)
GET_ARG; max = arg; GET_ARG; max = arg;
if (min > max) if (min > max)
FAIL; FAIL;
#ifdef Py_UNICODE_WIDE
if (max > 65535) if (max > 65535)
FAIL; FAIL;
#endif
if (!_validate_inner(code, code+skip-4, groups)) if (!_validate_inner(code, code+skip-4, groups))
FAIL; FAIL;
code += skip-4; code += skip-4;
@ -3056,10 +3061,8 @@ _validate_inner(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups)
GET_ARG; max = arg; GET_ARG; max = arg;
if (min > max) if (min > max)
FAIL; FAIL;
#ifdef Py_UNICODE_WIDE
if (max > 65535) if (max > 65535)
FAIL; FAIL;
#endif
if (!_validate_inner(code, code+skip-3, groups)) if (!_validate_inner(code, code+skip-3, groups))
FAIL; FAIL;
code += skip-3; code += skip-3;

View File

@ -14,12 +14,8 @@
#include "sre_constants.h" #include "sre_constants.h"
/* size of a code word (must be unsigned short or larger, and /* size of a code word (must be unsigned short or larger, and
large enough to hold a Py_UNICODE character) */ large enough to hold a UCS4 character) */
#ifdef Py_UNICODE_WIDE
#define SRE_CODE Py_UCS4 #define SRE_CODE Py_UCS4
#else
#define SRE_CODE unsigned short
#endif
typedef struct { typedef struct {
PyObject_VAR_HEAD PyObject_VAR_HEAD