summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2024-08-30 12:57:33 (GMT)
committerGitHub <noreply@github.com>2024-08-30 12:57:33 (GMT)
commit3d60dfbe1755e00ab20d0ee81281886be77ad5da (patch)
treeb1078f50a6b8641321f1044698867cb52f27105e /Lib
parent7fca268beee8ed13a8f161f0a0d5e21ff52d1ac1 (diff)
downloadcpython-3d60dfbe1755e00ab20d0ee81281886be77ad5da.zip
cpython-3d60dfbe1755e00ab20d0ee81281886be77ad5da.tar.gz
cpython-3d60dfbe1755e00ab20d0ee81281886be77ad5da.tar.bz2
gh-121645: Add PyBytes_Join() function (#121646)
* Replace _PyBytes_Join() with PyBytes_Join(). * Keep _PyBytes_Join() as an alias to PyBytes_Join().
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_capi/test_bytes.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/Lib/test/test_capi/test_bytes.py b/Lib/test/test_capi/test_bytes.py
index d5f047b..5908d79 100644
--- a/Lib/test/test_capi/test_bytes.py
+++ b/Lib/test/test_capi/test_bytes.py
@@ -249,6 +249,46 @@ class CAPITest(unittest.TestCase):
# CRASHES resize(NULL, 0, False)
# CRASHES resize(NULL, 3, False)
+ def test_join(self):
+ """Test PyBytes_Join()"""
+ bytes_join = _testcapi.bytes_join
+
+ self.assertEqual(bytes_join(b'', []), b'')
+ self.assertEqual(bytes_join(b'sep', []), b'')
+
+ self.assertEqual(bytes_join(b'', [b'a', b'b', b'c']), b'abc')
+ self.assertEqual(bytes_join(b'-', [b'a', b'b', b'c']), b'a-b-c')
+ self.assertEqual(bytes_join(b' - ', [b'a', b'b', b'c']), b'a - b - c')
+ self.assertEqual(bytes_join(b'-', [bytearray(b'abc'),
+ memoryview(b'def')]),
+ b'abc-def')
+
+ self.assertEqual(bytes_join(b'-', iter([b'a', b'b', b'c'])), b'a-b-c')
+
+ # invalid 'sep' argument
+ with self.assertRaises(TypeError):
+ bytes_join(bytearray(b'sep'), [])
+ with self.assertRaises(TypeError):
+ bytes_join(memoryview(b'sep'), [])
+ with self.assertRaises(TypeError):
+ bytes_join('', []) # empty Unicode string
+ with self.assertRaises(TypeError):
+ bytes_join('unicode', [])
+ with self.assertRaises(TypeError):
+ bytes_join(123, [])
+ with self.assertRaises(SystemError):
+ self.assertEqual(bytes_join(NULL, [b'a', b'b', b'c']), b'abc')
+
+ # invalid 'iterable' argument
+ with self.assertRaises(TypeError):
+ bytes_join(b'', [b'bytes', 'unicode'])
+ with self.assertRaises(TypeError):
+ bytes_join(b'', [b'bytes', 123])
+ with self.assertRaises(TypeError):
+ bytes_join(b'', 123)
+ with self.assertRaises(SystemError):
+ bytes_join(b'', NULL)
+
if __name__ == "__main__":
unittest.main()