summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2024-03-25 15:32:11 (GMT)
committerGitHub <noreply@github.com>2024-03-25 15:32:11 (GMT)
commit0c1a42cf9c8cd0d4534d5c1d58f118ce7c5c446e (patch)
tree929ca228612a2f48b5e7afa86101fcecb04ad29d /Lib
parent01e7405da400e8997f8964d06cc414045e144681 (diff)
downloadcpython-0c1a42cf9c8cd0d4534d5c1d58f118ce7c5c446e.zip
cpython-0c1a42cf9c8cd0d4534d5c1d58f118ce7c5c446e.tar.gz
cpython-0c1a42cf9c8cd0d4534d5c1d58f118ce7c5c446e.tar.bz2
gh-87193: Support bytes objects with refcount > 1 in _PyBytes_Resize() (GH-117160)
Create a new bytes object and destroy the old one if it has refcount > 1.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_capi/test_bytes.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/Lib/test/test_capi/test_bytes.py b/Lib/test/test_capi/test_bytes.py
index a2ba770..f14d554 100644
--- a/Lib/test/test_capi/test_bytes.py
+++ b/Lib/test/test_capi/test_bytes.py
@@ -2,6 +2,7 @@ import unittest
from test.support import import_helper
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
+_testcapi = import_helper.import_module('_testcapi')
from _testcapi import PY_SSIZE_T_MIN, PY_SSIZE_T_MAX
NULL = None
@@ -217,6 +218,35 @@ class CAPITest(unittest.TestCase):
# CRASHES decodeescape(b'abc', NULL, -1)
# CRASHES decodeescape(NULL, NULL, 1)
+ def test_resize(self):
+ """Test _PyBytes_Resize()"""
+ resize = _testcapi.bytes_resize
+
+ 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)
+
+ self.assertRaises(SystemError, resize, b'abc', -1, False)
+ self.assertRaises(SystemError, resize, bytearray(b'abc'), 3, False)
+
+ # CRASHES resize(NULL, 0, False)
+ # CRASHES resize(NULL, 3, False)
+
if __name__ == "__main__":
unittest.main()