FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.14] gh-156995: Fix _PyBytes_Resize() in Free Threading (#156996) by vstinner · Pull Request #157373 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (2) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
14 changes: 13 additions & 1 deletion Lib/test/test_bytes.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,6 @@ def test_resize(self):
self.assertRaises(MemoryError, bytearray().resize, sys.maxsize)
self.assertRaises(MemoryError, bytearray(1000).resize, sys.maxsize)


def test_setitem(self):
def setitem_as_mapping(b, i, val):
b[i] = val
Expand Down Expand Up @@ -2841,5 +2840,18 @@ def resize_stress(ba):
with threading_helper.start_threads(threads):
pass

@threading_helper.reap_threads
@threading_helper.requires_working_threading()
def test_free_threading_bytearray_resize_other_thread(self):
# Shrinking a bytearray whose buffer another thread owns must not
# adopt the immortal single-byte bytes object a the buffer.
ba = bytearray(b'abc')
thread = threading.Thread(target=ba.resize, args=(1,))
with threading_helper.start_threads([thread]):
pass
ba[0] = ord('X')
self.assertEqual(ba, bytearray(b'X'))
self.assertEqual(ord(b'a'), ord('a'))

if __name__ == "__main__":
unittest.main()
52 changes: 34 additions & 18 deletions Lib/test/test_capi/test_bytes.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import unittest
from test.support import import_helper

Expand Down Expand Up @@ -231,26 +232,41 @@ def test_decodeescape(self):

def test_resize(self):
"""Test _PyBytes_Resize()"""
resize = _testcapi.bytes_resize
_resize = _testcapi.bytes_resize

def resize(obj, size, new):
result = _resize(obj, size, new)
if 1 <= len(result):
if new or size != len(obj):
# gh-156995: Make sure that the result is a fresh object.
# Previously, _PyBytes_Resize(&obj, 1) returned a singleton
# if _PyObject_IsUniquelyReferenced() is false.
self.assertEqual(sys.getrefcount(result), 1)
self.assertFalse(sys._is_immortal(result))
else:
# check that the result is the empty bytes string singleton
self.assertTrue(sys._is_immortal(result))
return result

for new in True, False:
self.assertEqual(resize(b'abc', 0, new), b'')
self.assertEqual(resize(b'abc', 1, new), b'a')
self.assertEqual(resize(b'abc', 2, new), b'ab')
self.assertEqual(resize(b'abc', 3, new), b'abc')
b = resize(b'abc', 4, new)
self.assertEqual(len(b), 4)
self.assertEqual(b[:3], b'abc')

self.assertEqual(resize(b'a', 0, new), b'')
self.assertEqual(resize(b'a', 1, new), b'a')
b = resize(b'a', 2, new)
self.assertEqual(len(b), 2)
self.assertEqual(b[:1], b'a')

self.assertEqual(resize(b'', 0, new), b'')
self.assertEqual(len(resize(b'', 1, new)), 1)
self.assertEqual(len(resize(b'', 2, new)), 2)
with self.subTest(new=new):
self.assertEqual(resize(b'abc', 0, new), b'')
self.assertEqual(resize(b'abc', 1, new), b'a')
self.assertEqual(resize(b'abc', 2, new), b'ab')
self.assertEqual(resize(b'abc', 3, new), b'abc')
b = resize(b'abc', 4, new)
self.assertEqual(len(b), 4)
self.assertEqual(b[:3], b'abc')

self.assertEqual(resize(b'a', 0, new), b'')
self.assertEqual(resize(b'a', 1, new), b'a')
b = resize(b'a', 2, new)
self.assertEqual(len(b), 2)
self.assertEqual(b[:1], b'a')

self.assertEqual(resize(b'', 0, new), b'')
self.assertEqual(len(resize(b'', 1, new)), 1)
self.assertEqual(len(resize(b'', 2, new)), 2)

self.assertRaises(SystemError, resize, b'abc', -1, False)
self.assertRaises(SystemError, resize, bytearray(b'abc'), 3, False)
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
On Free Threading, ``_PyBytes_Resize(&obj, 1)`` no longer returns a single byte
singleton is the current thread is different than the thread which created the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
singleton is the current thread is different than the thread which created the
singleton if the current thread is different than the thread which created the

nit, is -> if

vstinner Sep 14, 2026 •
edited
Loading

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Ooops, I wrote #157477 to fix the typo. Thanks for the post-merge review :-)

object. Patch by Stan Ulbrych.
14 changes: 6 additions & 8 deletions Objects/bytesobject.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3243,14 +3243,12 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
return 0;
}
if (!_PyObject_IsUniquelyReferenced(v)) {
if (oldsize < newsize) {
*pv = _PyBytes_FromSize(newsize, 0);
if (*pv) {
memcpy(PyBytes_AS_STRING(*pv), PyBytes_AS_STRING(v), oldsize);
}
}
else {
*pv = PyBytes_FromStringAndSize(PyBytes_AS_STRING(v), newsize);
// Allocate and then copy so we don't get a shared immortal
// one-character singleton!
*pv = _PyBytes_FromSize(newsize, 0);
if (*pv) {
memcpy(PyBytes_AS_STRING(*pv), PyBytes_AS_STRING(v),
Py_MIN(oldsize, newsize));
}
Py_DECREF(v);
return (*pv == NULL) ? -1 : 0;
Expand Down
Loading

Back | FazBrowse Home | New Git URL