[Python-checkins] CVS: python/dist/src/Modules _sre.c,2.59,2.60

Fredrik Lundh effbot@users.sourceforge.net
2001年7月03日 13:32:38 -0700


Update of /cvsroot/python/python/dist/src/Modules
In directory usw-pr-cvs1:/tmp/cvs-serv11104/Modules
Modified Files:
	_sre.c 
Log Message:
bug #416670 
added copy/deepcopy support to SRE (still not enabled, since it's not
covered by the test suite)
Index: _sre.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/_sre.c,v
retrieving revision 2.59
retrieving revision 2.60
diff -C2 -r2.59 -r2.60
*** _sre.c	2001年07月02日 19:54:28	2.59
--- _sre.c	2001年07月03日 20:32:36	2.60
***************
*** 1698,1717 ****
 
 static PyObject*
! call(char* function, PyObject* args)
 {
 PyObject* name;
! PyObject* module;
 PyObject* func;
 PyObject* result;
 
! name = PyString_FromString(SRE_MODULE);
 if (!name)
 return NULL;
! module = PyImport_Import(name);
 Py_DECREF(name);
! if (!module)
 return NULL;
! func = PyObject_GetAttrString(module, function);
! Py_DECREF(module);
 if (!func)
 return NULL;
--- 1698,1717 ----
 
 static PyObject*
! call(char* module, char* function, PyObject* args)
 {
 PyObject* name;
! PyObject* mod;
 PyObject* func;
 PyObject* result;
 
! name = PyString_FromString(module);
 if (!name)
 return NULL;
! mod = PyImport_Import(name);
 Py_DECREF(name);
! if (!mod)
 return NULL;
! func = PyObject_GetAttrString(mod, function);
! Py_DECREF(mod);
 if (!func)
 return NULL;
***************
*** 1722,1725 ****
--- 1722,1745 ----
 }
 
+ #ifdef USE_BUILTIN_COPY
+ static int
+ deepcopy(PyObject** object, PyObject* memo)
+ {
+ PyObject* copy;
+ 
+ copy = call(
+ "copy", "deepcopy",
+ Py_BuildValue("OO", *object, memo)
+ );
+ if (!copy)
+ return 0;
+ 
+ Py_DECREF(*object);
+ *object = copy;
+ 
+ return 1; /* success */
+ }
+ #endif
+ 
 static PyObject*
 pattern_sub(PatternObject* self, PyObject* args, PyObject* kw)
***************
*** 1734,1738 ****
 
 /* delegate to Python code */
! return call("_sub", Py_BuildValue("OOOO", self, template, string, count));
 }
 
--- 1754,1761 ----
 
 /* delegate to Python code */
! return call(
! SRE_MODULE, "_sub",
! Py_BuildValue("OOOO", self, template, string, count)
! );
 }
 
***************
*** 1749,1753 ****
 
 /* delegate to Python code */
! return call("_subn", Py_BuildValue("OOOO", self, template, string, count));
 }
 
--- 1772,1779 ----
 
 /* delegate to Python code */
! return call(
! SRE_MODULE, "_subn",
! Py_BuildValue("OOOO", self, template, string, count)
! );
 }
 
***************
*** 1763,1767 ****
 
 /* delegate to Python code */
! return call("_split", Py_BuildValue("OOO", self, string, maxsplit));
 }
 
--- 1789,1796 ----
 
 /* delegate to Python code */
! return call(
! SRE_MODULE, "_split",
! Py_BuildValue("OOO", self, string, maxsplit)
! );
 }
 
***************
*** 1873,1881 ****
 pattern_copy(PatternObject* self, PyObject* args)
 {
! #if USE_BUILTIN_COPY
 PatternObject* copy;
 int offset;
 
! /* work in progress */
 
 copy = PyObject_NEW_VAR(PatternObject, &Pattern_Type, self->codesize);
--- 1902,1911 ----
 pattern_copy(PatternObject* self, PyObject* args)
 {
! #ifdef USE_BUILTIN_COPY
 PatternObject* copy;
 int offset;
 
! if (args != Py_None && !PyArg_ParseTuple(args, ":__copy__"))
! return NULL;
 
 copy = PyObject_NEW_VAR(PatternObject, &Pattern_Type, self->codesize);
***************
*** 1902,1907 ****
--- 1932,1957 ----
 pattern_deepcopy(PatternObject* self, PyObject* args)
 {
+ #ifdef USE_BUILTIN_COPY
+ PatternObject* copy;
+ 
+ PyObject* memo;
+ if (!PyArg_ParseTuple(args, "O:__deepcopy__", &memo))
+ return NULL;
+ 
+ copy = (PatternObject*) pattern_copy(self, Py_None);
+ if (!copy)
+ return NULL;
+ 
+ if (!deepcopy(&copy->groupindex, memo) ||
+ !deepcopy(&copy->indexgroup, memo) ||
+ !deepcopy(&copy->pattern, memo)) {
+ Py_DECREF(copy);
+ return NULL;
+ }
+ 
+ #else
 PyErr_SetString(PyExc_TypeError, "cannot deepcopy this pattern object");
 return NULL;
+ #endif
 }
 
***************
*** 2036,2040 ****
 /* delegate to Python code */
 return call(
! "_expand",
 Py_BuildValue("OOO", self->pattern, self, template)
 );
--- 2086,2090 ----
 /* delegate to Python code */
 return call(
! SRE_MODULE, "_expand",
 Py_BuildValue("OOO", self->pattern, self, template)
 );
***************
*** 2277,2285 ****
 match_copy(MatchObject* self, PyObject* args)
 {
! #if USE_BUILTIN_COPY
 MatchObject* copy;
 int slots, offset;
 
! /* works in progress */
 
 slots = 2 * (self->pattern->groups+1);
--- 2327,2336 ----
 match_copy(MatchObject* self, PyObject* args)
 {
! #ifdef USE_BUILTIN_COPY
 MatchObject* copy;
 int slots, offset;
 
! if (args != Py_None && !PyArg_ParseTuple(args, ":__copy__"))
! return NULL;
 
 slots = 2 * (self->pattern->groups+1);
***************
*** 2302,2306 ****
 return (PyObject*) copy;
 #else
! PyErr_SetString(PyExc_TypeError, "cannot deepcopy this match object");
 return NULL;
 #endif
--- 2353,2357 ----
 return (PyObject*) copy;
 #else
! PyErr_SetString(PyExc_TypeError, "cannot copy this match object");
 return NULL;
 #endif
***************
*** 2310,2315 ****
--- 2361,2386 ----
 match_deepcopy(MatchObject* self, PyObject* args)
 {
+ #ifdef USE_BUILTIN_COPY
+ MatchObject* copy;
+ 
+ PyObject* memo;
+ if (!PyArg_ParseTuple(args, "O:__deepcopy__", &memo))
+ return NULL;
+ 
+ copy = (MatchObject*) match_copy(self, Py_None);
+ if (!copy)
+ return NULL;
+ 
+ if (!deepcopy((PyObject**) &copy->pattern, memo) ||
+ !deepcopy(&copy->string, memo) ||
+ !deepcopy(&copy->regs, memo)) {
+ Py_DECREF(copy);
+ return NULL;
+ }
+ 
+ #else
 PyErr_SetString(PyExc_TypeError, "cannot deepcopy this match object");
 return NULL;
+ #endif
 }
 

AltStyle によって変換されたページ (->オリジナル) /