From: Guido van Rossum Date: Mon, 23 Sep 2002 20:49:43 +0000 (+0000) Subject: Backport 1.56 and 1.68 from trunk: X-Git-Tag: v2.2.2b1~152 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a9858559d682804186cafae63af397b2b34f77fe;p=thirdparty%2FPython%2Fcpython.git Backport 1.56 and 1.68 from trunk: 1.56: Apply diff3.txt from SF patch http://www.python.org/sf/536241 If a str or unicode method returns the original object, make sure that for str and unicode subclasses the original will not be returned. This should prevent SF bug http://www.python.org/sf/460020 from reappearing. 1.68: Fix SF bug 599128, submitted by Inyeol Lee: .replace() would do the wrong thing for a unicode subclass when there were zero string replacements. The example given in the SF bug report was only one way to trigger this; replacing a string of length >= 2 that's not found is another. The code would actually write outside allocated memory if replacement string was longer than the search string. --- diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 5d1c35a6d36f..cb3f9f3ee674 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -50,6 +50,25 @@ def test(method, input, output, *args): exc = sys.exc_info()[:2] else: exc = None + if value == output and type(value) is type(output): + # if the original is returned make sure that + # this doesn't happen with subclasses + if value is input: + class usub(unicode): + def __repr__(self): + return 'usub(%r)' % unicode.__repr__(self) + input = usub(input) + try: + f = getattr(input, method) + value = apply(f, args) + except: + value = sys.exc_type + exc = sys.exc_info()[:2] + if value is input: + if verbose: + print 'no' + print '*',f, `input`, `output`, `value` + return if value != output or type(value) is not type(output): if verbose: print 'no' @@ -186,6 +205,8 @@ 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) +test('replace', u'abc', u'abc', u'ab', u'--', 0) +test('replace', u'abc', u'abc', u'xy', u'--') test('startswith', u'hello', 1, u'he') test('startswith', u'hello', 1, u'hello')