summaryrefslogtreecommitdiffstats
path: root/Modules/_sre.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-09-10 22:02:25 (GMT)
committerGuido van Rossum <guido@python.org>2007-09-10 22:02:25 (GMT)
commit1ff91d95a280449cfd9c723a081cb7b19a52e758 (patch)
treec537822cc870185f7042767b7ed5ca40b7d5da50 /Modules/_sre.c
parent98d19dafd9c9d95338887b9e53c77ec6960918e0 (diff)
downloadcpython-1ff91d95a280449cfd9c723a081cb7b19a52e758.zip
cpython-1ff91d95a280449cfd9c723a081cb7b19a52e758.tar.gz
cpython-1ff91d95a280449cfd9c723a081cb7b19a52e758.tar.bz2
Patch # 1140 (my code, approved by Effbot).
Make sure the type of the return value of re.sub(x, y, z) is the type of y+x (i.e. unicode if either is unicode, str if they are both str) even if there are no substitutions or if x==z (which triggered various special cases in join_list()). Could be backported to 2.5; no need to port to 3.0.
Diffstat (limited to 'Modules/_sre.c')
-rw-r--r--Modules/_sre.c25
1 files changed, 8 insertions, 17 deletions
diff --git a/Modules/_sre.c b/Modules/_sre.c
index 7dafaeb..51a7348 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -1979,7 +1979,7 @@ deepcopy(PyObject** object, PyObject* memo)
#endif
static PyObject*
-join_list(PyObject* list, PyObject* pattern)
+join_list(PyObject* list, PyObject* string)
{
/* join list elements */
@@ -1990,24 +1990,15 @@ join_list(PyObject* list, PyObject* pattern)
#endif
PyObject* result;
- switch (PyList_GET_SIZE(list)) {
- case 0:
- Py_DECREF(list);
- return PySequence_GetSlice(pattern, 0, 0);
- case 1:
- result = PyList_GET_ITEM(list, 0);
- Py_INCREF(result);
- Py_DECREF(list);
- return result;
- }
-
- /* two or more elements: slice out a suitable separator from the
- first member, and use that to join the entire list */
-
- joiner = PySequence_GetSlice(pattern, 0, 0);
+ joiner = PySequence_GetSlice(string, 0, 0);
if (!joiner)
return NULL;
+ if (PyList_GET_SIZE(list) == 0) {
+ Py_DECREF(list);
+ return joiner;
+ }
+
#if PY_VERSION_HEX >= 0x01060000
function = PyObject_GetAttrString(joiner, "join");
if (!function) {
@@ -2443,7 +2434,7 @@ next:
Py_DECREF(filter);
/* convert list to single string (also removes list) */
- item = join_list(list, self->pattern);
+ item = join_list(list, string);
if (!item)
return NULL;