Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d89a2e7

Browse files
author
Fredrik Lundh
committed
bug #416670
added copy/deepcopy support to SRE (still not enabled, since it's not covered by the test suite)
1 parent ee2f18d commit d89a2e7

1 file changed

Lines changed: 87 additions & 16 deletions

File tree

Modules/_sre.c

Lines changed: 87 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,22 +1697,22 @@ pattern_search(PatternObject* self, PyObject* args, PyObject* kw)
16971697
}
16981698

16991699
static PyObject*
1700-
call(char* function, PyObject* args)
1700+
call(char* module, char* function, PyObject* args)
17011701
{
17021702
PyObject* name;
1703-
PyObject* module;
1703+
PyObject* mod;
17041704
PyObject* func;
17051705
PyObject* result;
17061706

1707-
name = PyString_FromString(SRE_MODULE);
1707+
name = PyString_FromString(module);
17081708
if (!name)
17091709
return NULL;
1710-
module = PyImport_Import(name);
1710+
mod = PyImport_Import(name);
17111711
Py_DECREF(name);
1712-
if (!module)
1712+
if (!mod)
17131713
return NULL;
1714-
func = PyObject_GetAttrString(module, function);
1715-
Py_DECREF(module);
1714+
func = PyObject_GetAttrString(mod, function);
1715+
Py_DECREF(mod);
17161716
if (!func)
17171717
return NULL;
17181718
result = PyObject_CallObject(func, args);
@@ -1721,6 +1721,26 @@ call(char* function, PyObject* args)
17211721
return result;
17221722
}
17231723

1724+
#ifdef USE_BUILTIN_COPY
1725+
static int
1726+
deepcopy(PyObject** object, PyObject* memo)
1727+
{
1728+
PyObject* copy;
1729+
1730+
copy = call(
1731+
"copy", "deepcopy",
1732+
Py_BuildValue("OO", *object, memo)
1733+
);
1734+
if (!copy)
1735+
return 0;
1736+
1737+
Py_DECREF(*object);
1738+
*object = copy;
1739+
1740+
return 1; /* success */
1741+
}
1742+
#endif
1743+
17241744
static PyObject*
17251745
pattern_sub(PatternObject* self, PyObject* args, PyObject* kw)
17261746
{
@@ -1733,7 +1753,10 @@ pattern_sub(PatternObject* self, PyObject* args, PyObject* kw)
17331753
return NULL;
17341754

17351755
/* delegate to Python code */
1736-
return call("_sub", Py_BuildValue("OOOO", self, template, string, count));
1756+
return call(
1757+
SRE_MODULE, "_sub",
1758+
Py_BuildValue("OOOO", self, template, string, count)
1759+
);
17371760
}
17381761

17391762
static PyObject*
@@ -1748,7 +1771,10 @@ pattern_subn(PatternObject* self, PyObject* args, PyObject* kw)
17481771
return NULL;
17491772

17501773
/* delegate to Python code */
1751-
return call("_subn", Py_BuildValue("OOOO", self, template, string, count));
1774+
return call(
1775+
SRE_MODULE, "_subn",
1776+
Py_BuildValue("OOOO", self, template, string, count)
1777+
);
17521778
}
17531779

17541780
static PyObject*
@@ -1762,7 +1788,10 @@ pattern_split(PatternObject* self, PyObject* args, PyObject* kw)
17621788
return NULL;
17631789

17641790
/* delegate to Python code */
1765-
return call("_split", Py_BuildValue("OOO", self, string, maxsplit));
1791+
return call(
1792+
SRE_MODULE, "_split",
1793+
Py_BuildValue("OOO", self, string, maxsplit)
1794+
);
17661795
}
17671796

17681797
static PyObject*
@@ -1872,11 +1901,12 @@ pattern_findall(PatternObject* self, PyObject* args, PyObject* kw)
18721901
static PyObject*
18731902
pattern_copy(PatternObject* self, PyObject* args)
18741903
{
1875-
#if USE_BUILTIN_COPY
1904+
#ifdef USE_BUILTIN_COPY
18761905
PatternObject* copy;
18771906
int offset;
18781907

1879-
/* work in progress */
1908+
if (args != Py_None && !PyArg_ParseTuple(args, ":__copy__"))
1909+
return NULL;
18801910

18811911
copy = PyObject_NEW_VAR(PatternObject, &Pattern_Type, self->codesize);
18821912
if (!copy)
@@ -1901,8 +1931,28 @@ pattern_copy(PatternObject* self, PyObject* args)
19011931
static PyObject*
19021932
pattern_deepcopy(PatternObject* self, PyObject* args)
19031933
{
1934+
#ifdef USE_BUILTIN_COPY
1935+
PatternObject* copy;
1936+
1937+
PyObject* memo;
1938+
if (!PyArg_ParseTuple(args, "O:__deepcopy__", &memo))
1939+
return NULL;
1940+
1941+
copy = (PatternObject*) pattern_copy(self, Py_None);
1942+
if (!copy)
1943+
return NULL;
1944+
1945+
if (!deepcopy(&copy->groupindex, memo) ||
1946+
!deepcopy(&copy->indexgroup, memo) ||
1947+
!deepcopy(&copy->pattern, memo)) {
1948+
Py_DECREF(copy);
1949+
return NULL;
1950+
}
1951+
1952+
#else
19041953
PyErr_SetString(PyExc_TypeError, "cannot deepcopy this pattern object");
19051954
return NULL;
1955+
#endif
19061956
}
19071957

19081958
static PyMethodDef pattern_methods[] = {
@@ -2035,7 +2085,7 @@ match_expand(MatchObject* self, PyObject* args)
20352085

20362086
/* delegate to Python code */
20372087
return call(
2038-
"_expand",
2088+
SRE_MODULE, "_expand",
20392089
Py_BuildValue("OOO", self->pattern, self, template)
20402090
);
20412091
}
@@ -2276,11 +2326,12 @@ match_regs(MatchObject* self)
22762326
static PyObject*
22772327
match_copy(MatchObject* self, PyObject* args)
22782328
{
2279-
#if USE_BUILTIN_COPY
2329+
#ifdef USE_BUILTIN_COPY
22802330
MatchObject* copy;
22812331
int slots, offset;
22822332

2283-
/* works in progress */
2333+
if (args != Py_None && !PyArg_ParseTuple(args, ":__copy__"))
2334+
return NULL;
22842335

22852336
slots = 2 * (self->pattern->groups+1);
22862337

@@ -2301,16 +2352,36 @@ match_copy(MatchObject* self, PyObject* args)
23012352

23022353
return (PyObject*) copy;
23032354
#else
2304-
PyErr_SetString(PyExc_TypeError, "cannot deepcopy this match object");
2355+
PyErr_SetString(PyExc_TypeError, "cannot copy this match object");
23052356
return NULL;
23062357
#endif
23072358
}
23082359

23092360
static PyObject*
23102361
match_deepcopy(MatchObject* self, PyObject* args)
23112362
{
2363+
#ifdef USE_BUILTIN_COPY
2364+
MatchObject* copy;
2365+
2366+
PyObject* memo;
2367+
if (!PyArg_ParseTuple(args, "O:__deepcopy__", &memo))
2368+
return NULL;
2369+
2370+
copy = (MatchObject*) match_copy(self, Py_None);
2371+
if (!copy)
2372+
return NULL;
2373+
2374+
if (!deepcopy((PyObject**) &copy->pattern, memo) ||
2375+
!deepcopy(&copy->string, memo) ||
2376+
!deepcopy(&copy->regs, memo)) {
2377+
Py_DECREF(copy);
2378+
return NULL;
2379+
}
2380+
2381+
#else
23122382
PyErr_SetString(PyExc_TypeError, "cannot deepcopy this match object");
23132383
return NULL;
2384+
#endif
23142385
}
23152386

23162387
static PyMethodDef match_methods[] = {

0 commit comments

Comments
 (0)