mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
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:
parent
7fca268bee
commit
3d60dfbe17
10 changed files with 101 additions and 12 deletions
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue