Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-121645: Add PyBytes_Join() function#121646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -249,6 +249,46 @@ def test_resize(self): | ||
| # CRASHES resize(NULL, 0, False) | ||
| # CRASHES resize(NULL, 3, False) | ||
| def test_join(self): | ||
vstinner marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| """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, []) | ||
vstinner marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Add :c:func:`PyBytes_Join(sep, iterable) <PyBytes_Join>` function, similar to | ||
| ``sep.join(iterable)`` in Python. Patch by Victor Stinner. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1283,7 +1283,7 @@ _buffered_readline(buffered *self, Py_ssize_t limit) | ||
| Py_CLEAR(res); | ||
| goto end; | ||
| } | ||
| Py_XSETREF(res, _PyBytes_Join((PyObject *)&_Py_SINGLETON(bytes_empty), chunks)); | ||
| Py_XSETREF(res, PyBytes_Join((PyObject *)&_Py_SINGLETON(bytes_empty), chunks)); | ||
vstinner marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| end: | ||
| LEAVE_BUFFERED(self) | ||
| @@ -1736,7 +1736,7 @@ _bufferedreader_read_all(buffered *self) | ||
| goto cleanup; | ||
| } | ||
| else { | ||
| tmp = _PyBytes_Join((PyObject *)&_Py_SINGLETON(bytes_empty), chunks); | ||
| tmp = PyBytes_Join((PyObject *)&_Py_SINGLETON(bytes_empty), chunks); | ||
| res = tmp; | ||
| goto cleanup; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.