cpython: 10a79a33d09b (original) (raw)

--- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -1,4 +1,5 @@ -from test.support import verbose, run_unittest +from test.support import verbose, run_unittest, gc_collect +import io import re from re import Scanner import sys @@ -16,6 +17,17 @@ import unittest class ReTests(unittest.TestCase):

+ def test_weakref(self): s = 'QabbbcR' x = re.compile('ab+c')

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -549,6 +549,9 @@ Tests Extension Modules ----------------- +- Issue #14212: The re module didn't retain a reference to buffers it was

--- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -1664,7 +1664,7 @@ state_reset(SRE_STATE* state) } static void* -getstring(PyObject* string, Py_ssize_t* p_length, int* p_charsize) +getstring(PyObject* string, Py_ssize_t* p_length, int* p_charsize, Py_buffer view) { / given a python object, return a data pointer, a length (in characters), and a character size. return NULL if the object @@ -1674,7 +1674,6 @@ getstring(PyObject* string, Py_ssize_t* Py_ssize_t size, bytes; int charsize; void* ptr;

/* Unicode objects do not support the buffer API. So, get the data directly instead. / @@ -1686,26 +1685,21 @@ getstring(PyObject string, Py_ssize_t* } /* get pointer to string buffer */

-

if (bytes < 0) { PyErr_SetString(PyExc_TypeError, "buffer has negative size");

#endif else { PyErr_SetString(PyExc_TypeError, "buffer size mismatch");

} LOCAL(PyObject*) @@ -1747,20 +1746,21 @@ state_init(SRE_STATE* state, PatternObje state->lastmark = -1; state->lastindex = -1;

- - if (charsize == 1 && pattern->charsize > 1) { - PyErr_SetString(PyExc_TypeError,

+

/* adjust boundaries */ if (start < 0) @@ -1797,11 +1797,17 @@ state_init(SRE_STATE* state, PatternObje state->lower = sre_lower; return string;

} LOCAL(void) state_fini(SRE_STATE* state) {

if (PyCallable_Check(ptemplate)) { /* sub/subn takes either a function or a template / @@ -2306,7 +2315,8 @@ pattern_subx(PatternObject self, PyObje } else { /* if not callable, check if it's a literal string */ int literal;

@@ -2320,6 +2330,8 @@ pattern_subx(PatternObject* self, PyObje PyErr_Clear(); literal = 0; }

@@ -2661,6 +2673,7 @@ static PyObject Py_ssize_t groups = 0; PyObject groupindex = NULL; PyObject* indexgroup = NULL; + if (!PyArg_ParseTuple(args, "OiO!|nOO", &pattern, &flags, &PyList_Type, &code, &groups, &groupindex, &indexgroup)) @@ -2675,6 +2688,7 @@ static PyObject * self->pattern = NULL; self->groupindex = NULL; self->indexgroup = NULL;

self->codesize = n; @@ -2694,15 +2708,15 @@ static PyObject * return NULL; } - if (pattern == Py_None) - self->charsize = -1; - else { - Py_ssize_t p_length; - if (!getstring(pattern, &p_length, &self->charsize)) { - Py_DECREF(self); - return NULL; - } - }

Py_INCREF(pattern); self->pattern = pattern;

--- a/Modules/sre.h +++ b/Modules/sre.h @@ -31,6 +31,7 @@ typedef struct { int flags; /* flags used when compiling pattern source */ PyObject weakreflist; / List of weak references / int charsize; / pattern charsize (or -1) */