summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2013-02-07 22:12:46 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2013-02-07 22:12:46 (GMT)
commitbbbac2ec34e99c24d7bc0eedbcc138c5f4551d48 (patch)
treed78b2fead0311d37b6f76d0a3466fd78196c8c31
parent2efdc90b0f49866d857a5b3dc21b7c791f7a0aeb (diff)
downloadcpython-bbbac2ec34e99c24d7bc0eedbcc138c5f4551d48.zip
cpython-bbbac2ec34e99c24d7bc0eedbcc138c5f4551d48.tar.gz
cpython-bbbac2ec34e99c24d7bc0eedbcc138c5f4551d48.tar.bz2
Issue #17137: When an Unicode string is resized, the internal wide character
string (wstr) format is now cleared.
-rw-r--r--Lib/test/test_unicode.py15
-rw-r--r--Misc/NEWS3
-rw-r--r--Objects/unicodeobject.c4
3 files changed, 22 insertions, 0 deletions
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
index 8fccab3..f7d8686 100644
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -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):
diff --git a/Misc/NEWS b/Misc/NEWS
index 02ec339..39a79c2 100644
--- 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.
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index abe793d..51160f8 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -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));