]> granicus.if.org Git - python/commitdiff
Unicode replace() method with empty pattern argument should fail, like
authorGuido van Rossum <guido@python.org>
Fri, 9 Aug 2002 15:36:48 +0000 (15:36 +0000)
committerGuido van Rossum <guido@python.org>
Fri, 9 Aug 2002 15:36:48 +0000 (15:36 +0000)
it does for 8-bit strings.

Lib/test/test_unicode.py
Objects/unicodeobject.c

index 429b673d9f58ac1a881ef23a78b0e7578e025aca..a915b2e35862210da8281a74f4b2822acbed60de 100644 (file)
@@ -206,6 +206,12 @@ test('replace', u'one!two!three!', u'one!two!three!', u'!', u'@', 0)
 test('replace', u'one!two!three!', u'one@two@three@', u'!', u'@')
 test('replace', u'one!two!three!', u'one!two!three!', u'x', u'@')
 test('replace', u'one!two!three!', u'one!two!three!', u'x', u'@', 2)
+try:
+    u"abc".replace(u"", u"x")
+except ValueError:
+    pass
+else:
+    raise TestFailed, "u.replace('', ...) should raise ValueError"
 
 test('startswith', u'hello', True, u'he')
 test('startswith', u'hello', True, u'hello')
index 03b5dbd9704d2a8b652c28c1cc2f394c27865bc0..d6fd62af8040a2ee3bd8e005aa012ea1b2f357b1 100644 (file)
@@ -3455,6 +3455,11 @@ PyObject *replace(PyUnicodeObject *self,
 {
     PyUnicodeObject *u;
 
+    if (str1->length == 0) {
+       PyErr_SetString(PyExc_ValueError, "empty pattern string");
+       return NULL;
+    }
+
     if (maxcount < 0)
        maxcount = INT_MAX;