From ed83292576fd4baac5f06283448b360b9bc218cb Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sat, 17 Oct 2026 00:46:47 +0200 Subject: [PATCH] gh-158585: Backport test_capi.test_bytes from 3.15 --- Lib/test/test_capi/test_bytes.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_capi/test_bytes.py b/Lib/test/test_capi/test_bytes.py index e2a39cb4252dcf0..a50e9b88cd0180f 100644 --- a/Lib/test/test_capi/test_bytes.py +++ b/Lib/test/test_capi/test_bytes.py @@ -56,10 +56,12 @@ def test_fromstringandsize(self): self.assertEqual(fromstringandsize(b'abc'), b'abc') self.assertEqual(fromstringandsize(b'abc', 2), b'ab') self.assertEqual(fromstringandsize(b'abc\0def'), b'abc\0def') - self.assertEqual(fromstringandsize(b'a'), b'a') - self.assertEqual(fromstringandsize(b'a', 1), b'a') - self.assertEqual(fromstringandsize(b'', 0), b'') - self.assertEqual(fromstringandsize(NULL, 0), b'') + + # Return singleton for 0 or 1 bytes + self.assertIs(fromstringandsize(b'', 0), b'') + self.assertIs(fromstringandsize(NULL, 0), b'') + self.assertIs(fromstringandsize(b'a', 1), b'a') + self.assertEqual(len(fromstringandsize(NULL, 3)), 3) self.assertRaises((MemoryError, OverflowError), fromstringandsize, NULL, PY_SSIZE_T_MAX) @@ -74,7 +76,10 @@ def test_fromstring(self): fromstring = _testlimitedcapi.bytes_fromstring self.assertEqual(fromstring(b'abc\0def'), b'abc') - self.assertEqual(fromstring(b''), b'') + + # Return singleton for 0 or 1 bytes + self.assertIs(fromstring(b''), b'') + self.assertIs(fromstring(b'a'), b'a') # CRASHES fromstring(NULL)