]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Backport 1.56 and 1.68 from trunk:
authorGuido van Rossum <guido@python.org>
Mon, 23 Sep 2002 20:49:43 +0000 (20:49 +0000)
committerGuido van Rossum <guido@python.org>
Mon, 23 Sep 2002 20:49:43 +0000 (20:49 +0000)
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.

Lib/test/test_unicode.py

index 5d1c35a6d36f93572ebba9de7c7a710085924d9c..cb3f9f3ee674136f9676511e018182c0e8e4178a 100644 (file)
@@ -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')