From: Guido van Rossum Date: Fri, 9 Aug 2002 15:36:48 +0000 (+0000) Subject: Unicode replace() method with empty pattern argument should fail, like X-Git-Tag: v2.3c1~4581 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f36921c4b0ca499134b44ff3594c6c43768799c2;p=python Unicode replace() method with empty pattern argument should fail, like it does for 8-bit strings. --- diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 429b673d9f..a915b2e358 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -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') diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 03b5dbd970..d6fd62af80 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -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;