[Python-checkins] cpython (merge 3.3 -> default): (Merge 3.3) Issue #17137: When an Unicode string is resized, the internal wide

victor.stinner python-checkins at python.org
Thu Feb 7 23:18:41 CET 2013


http://hg.python.org/cpython/rev/c10a3ddba483
changeset:   82059:c10a3ddba483
parent:      82057:b8a6bc70fc08
parent:      82058:3b316ea5aa82
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Thu Feb 07 23:17:34 2013 +0100
summary:
  (Merge 3.3) Issue #17137: When an Unicode string is resized, the internal wide
character string (wstr) format is now cleared.

files:
  Lib/test/test_unicode.py |  15 +++++++++++++++
  Misc/NEWS                |   3 +++
  Objects/unicodeobject.c  |   4 ++++
  3 files changed, 22 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -2191,6 +2191,21 @@
         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):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 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.
 
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -717,6 +717,10 @@
         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;
+    }
 #ifdef Py_DEBUG
     unicode_fill_invalid(unicode, old_length);
 #endif

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list