From f36921c4b0ca499134b44ff3594c6c43768799c2 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Fri, 9 Aug 2002 15:36:48 +0000 Subject: [PATCH] Unicode replace() method with empty pattern argument should fail, like it does for 8-bit strings. --- Lib/test/test_unicode.py | 6 ++++++ Objects/unicodeobject.c | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 429b673d9f5..a915b2e3586 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -206,6 +206,12 @@ def __getitem__(self, i): return self.seq[i] 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 03b5dbd9704..d6fd62af804 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;