gh-121645: Add PyBytes_Join() function (#121646)

* Replace _PyBytes_Join() with PyBytes_Join().
* Keep _PyBytes_Join() as an alias to PyBytes_Join().
This commit is contained in:
Victor Stinner 2024-08-30 14:57:33 +02:00 committed by GitHub
parent 7fca268bee
commit 3d60dfbe17
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 101 additions and 12 deletions

View file

@ -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()