Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.4k
gh-129559: Addbytearray.resize()#129560
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 ourterms of service andprivacy 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 from15 commits
e240327925fcbd29c04add15ef67d19903218829a2df779e2facc91fd8b9faf7429cf4c183116dd46a8569bbdc1ec4aa3d336299a1a0e157cf89ec1ddc7b09a49374b298d0522f6b0a3bf00f338df7b0243e46ddFile 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 |
|---|---|---|
| @@ -73,7 +73,11 @@ Direct API functions | ||
| .. c:function:: int PyByteArray_Resize(PyObject *bytearray, Py_ssize_t len) | ||
| Resize the internal buffer of *bytearray* to *len*. Failure is a ``-1`` return with an exception set. | ||
cmaloney marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| .. versionchanged:: next | ||
| A negative *len* will now result in a failure. | ||
gpshead marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| Macros | ||
| ^^^^^^ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2841,6 +2841,13 @@ objects. | ||
| optional *sep* and *bytes_per_sep* parameters to insert separators | ||
| between bytes in the hex output. | ||
| .. method:: resize(size) | ||
| Resize the :class:`bytearray` to contain *size* bytes. | ||
gpshead marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| If :class:`bytearray` needs to grow, all new bytes will be set to null bytes. | ||
| ||
| .. versionadded:: next | ||
| Since bytearray objects are sequences of integers (akin to a list), for a | ||
| bytearray object *b*, ``b[0]`` will be an integer, while ``b[0:1]`` will be | ||
| a bytearray object of length 1. (This contrasts with text strings, where | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1359,6 +1359,38 @@ def by(s): | ||
| b = by("Hello, world") | ||
| self.assertEqual(re.findall(br"\w+", b), [by("Hello"), by("world")]) | ||
| def test_resize(self): | ||
| ba = bytearray(b'abcdef') | ||
| self.assertIsNone(ba.resize(3)) | ||
| self.assertEqual(ba, bytearray(b'abc')) | ||
| self.assertIsNone(ba.resize(10)) | ||
| self.assertEqual(len(ba), 10) | ||
| # Bytes beyond set values must be cleared. | ||
| self.assertEqual(ba, bytearray(b'abc\0\0\0\0\0\0\0')) | ||
| ba[3:10] = b'defghij' | ||
| self.assertEqual(ba, bytearray(b'abcdefghij')) | ||
| self.assertIsNone(ba.resize(2**20)) | ||
| self.assertEqual(len(ba), 2**20) | ||
| self.assertEqual(ba, bytearray(b'abcdefghij' + b'\0' * (2 ** 20 - 10))) | ||
| self.assertIsNone(ba.resize(0)) | ||
| self.assertEqual(ba, bytearray()) | ||
| self.assertIsNone(ba.resize(10)) | ||
| self.assertEqual(ba, bytearray(b'\0' * 10)) | ||
| ba = ByteArraySubclass(b'abcdef') | ||
| self.assertIsNone(ba.resize(3)) | ||
| self.assertEqual(ba, bytearray(b'abc')) | ||
cmaloney marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| # Check arguments | ||
| self.assertRaises(TypeError, lambda: bytearray().resize()) | ||
| self.assertRaises(TypeError, bytearray().resize, 10, 10) | ||
| self.assertRaises(BufferError, lambda: bytearray().resize(-1)) | ||
gpshead marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| self.assertRaises(BufferError, lambda: bytearray().resize(-200)) | ||
| self.assertRaises(MemoryError, lambda: bytearray().resize(sys.maxsize)) | ||
| self.assertRaises(MemoryError, lambda: bytearray(1000).resize(sys.maxsize)) | ||
| def test_setitem(self): | ||
| def setitem_as_mapping(b, i, val): | ||
| b[i] = val | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Add:meth:`bytearray.resize` method to:class:`bytearray` wrapping | ||
| :c:func:`PyByteArray_Resize` so:class:`bytearray` can be efficiently | ||
| resized in place. | ||
cmaloney marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -184,7 +184,12 @@ PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size) | ||
| assert(self != NULL); | ||
| assert(PyByteArray_Check(self)); | ||
| assert(logical_offset <= alloc); | ||
| if (requested_size < 0) { | ||
| PyErr_Format(PyExc_ValueError, | ||
| "Can only resize to positive sizes, got %zd", requested_size); | ||
| return -1; | ||
| } | ||
| if (requested_size == Py_SIZE(self)) { | ||
| return 0; | ||
| @@ -1388,6 +1393,31 @@ bytearray_removesuffix_impl(PyByteArrayObject *self, Py_buffer *suffix) | ||
| } | ||
| /*[clinic input] | ||
| bytearray.resize | ||
| size: Py_ssize_t | ||
| New size to resize to.. | ||
| / | ||
| Resize the internal buffer of bytearray to len. | ||
| [clinic start generated code]*/ | ||
| static PyObject * | ||
| bytearray_resize_impl(PyByteArrayObject *self, Py_ssize_t size) | ||
| /*[clinic end generated code: output=f73524922990b2d9 input=75fd4d17c4aa47d3]*/ | ||
| { | ||
| Py_ssize_t start_size = PyByteArray_GET_SIZE(self); | ||
| int result = PyByteArray_Resize((PyObject *)self, size); | ||
cmaloney marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| if (result < 0) { | ||
| return NULL; | ||
| } | ||
| // Set new bytes to provide consistent / safer behavior in Python version. | ||
cmaloney marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| if (size > start_size) { | ||
| memset(PyByteArray_AS_STRING(self) + start_size, 0, size - start_size); | ||
| } | ||
| Py_RETURN_NONE; | ||
| } | ||
| /*[clinic input] | ||
| bytearray.translate | ||
| @@ -2361,6 +2391,7 @@ static PyMethodDef bytearray_methods[] = { | ||
| BYTEARRAY_REPLACE_METHODDEF | ||
| BYTEARRAY_REMOVEPREFIX_METHODDEF | ||
| BYTEARRAY_REMOVESUFFIX_METHODDEF | ||
| BYTEARRAY_RESIZE_METHODDEF | ||
| BYTEARRAY_REVERSE_METHODDEF | ||
| BYTEARRAY_RFIND_METHODDEF | ||
| BYTEARRAY_RINDEX_METHODDEF | ||
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.