Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bbbac2e

Browse files
committed
Issue #17137: When an Unicode string is resized, the internal wide character
string (wstr) format is now cleared.
1 parent 2efdc90 commit bbbac2e

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

Lib/test/test_unicode.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,6 +2167,21 @@ def test_getnewargs(self):
21672167
self.assertEqual(args[0], text)
21682168
self.assertEqual(len(args), 1)
21692169

2170+
def test_resize(self):
2171+
for length in range(1, 100, 7):
2172+
# generate a fresh string (refcount=1)
2173+
text = 'a' * length + 'b'
2174+
2175+
# fill wstr internal field
2176+
abc = text.encode('unicode_internal')
2177+
self.assertEqual(abc.decode('unicode_internal'), text)
2178+
2179+
# resize text: wstr field must be cleared and then recomputed
2180+
text += 'c'
2181+
abcdef = text.encode('unicode_internal')
2182+
self.assertNotEqual(abc, abcdef)
2183+
self.assertEqual(abcdef.decode('unicode_internal'), text)
2184+
21702185

21712186
class StringModuleTest(unittest.TestCase):
21722187
def test_formatter_parser(self):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.3.1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #17137: When an Unicode string is resized, the internal wide character
16+
string (wstr) format is now cleared.
17+
1518
- Issue #17043: The unicode-internal decoder no longer read past the end of
1619
input buffer.
1720

Objects/unicodeobject.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,10 @@ resize_compact(PyObject *unicode, Py_ssize_t length)
702702
if (!PyUnicode_IS_ASCII(unicode))
703703
_PyUnicode_WSTR_LENGTH(unicode) = length;
704704
}
705+
else if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) {
706+
PyObject_DEL(_PyUnicode_WSTR(unicode));
707+
_PyUnicode_WSTR(unicode) = NULL;
708+
}
705709
PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode),
706710
length, 0);
707711
assert(_PyUnicode_CheckConsistency(unicode, 0));

0 commit comments

Comments
 (0)