]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #17137: When an Unicode string is resized, the internal wide character
authorVictor Stinner <victor.stinner@gmail.com>
Thu, 7 Feb 2013 22:12:46 +0000 (23:12 +0100)
committerVictor Stinner <victor.stinner@gmail.com>
Thu, 7 Feb 2013 22:12:46 +0000 (23:12 +0100)
string (wstr) format is now cleared.

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

index 8fccab3cb8b7d672611e4435dfb94ff2fee3720d..f7d868659542178e47a750dc80d8ebf39141d087 100644 (file)
@@ -2167,6 +2167,21 @@ class UnicodeTest(string_tests.CommonTest,
         self.assertEqual(args[0], text)
         self.assertEqual(len(args), 1)
 
+    def test_resize(self):
+        for length in range(1, 100, 7):
+            # generate a fresh string (refcount=1)
+            text = 'a' * length + 'b'
+
+            # fill wstr internal field
+            abc = text.encode('unicode_internal')
+            self.assertEqual(abc.decode('unicode_internal'), text)
+
+            # resize text: wstr field must be cleared and then recomputed
+            text += 'c'
+            abcdef = text.encode('unicode_internal')
+            self.assertNotEqual(abc, abcdef)
+            self.assertEqual(abcdef.decode('unicode_internal'), text)
+
 
 class StringModuleTest(unittest.TestCase):
     def test_formatter_parser(self):
index 02ec3399e7aaf4af8b6a48bc72f43d4a0208b0ec..39a79c206fc5d785ff71c1321ec404e75f3dee84 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 3.3.1?
 Core and Builtins
 -----------------
 
+- Issue #17137: When an Unicode string is resized, the internal wide character
+  string (wstr) format is now cleared.
+
 - Issue #17043: The unicode-internal decoder no longer read past the end of
   input buffer.
 
index abe793dfd4870859b8dc313af323d8ef3c4d5ef5..51160f852d292c6b051ee871d29eb2de4dff5999 100644 (file)
@@ -702,6 +702,10 @@ resize_compact(PyObject *unicode, Py_ssize_t length)
         if (!PyUnicode_IS_ASCII(unicode))
             _PyUnicode_WSTR_LENGTH(unicode) = length;
     }
+    else if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) {
+        PyObject_DEL(_PyUnicode_WSTR(unicode));
+        _PyUnicode_WSTR(unicode) = NULL;
+    }
     PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode),
                     length, 0);
     assert(_PyUnicode_CheckConsistency(unicode, 0));