forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pythongh-87193: Support bytes objects with refcount > 1 in _PyBytes_R…
…esize() (pythonGH-117160) Create a new bytes object and destroy the old one if it has refcount > 1.
- Loading branch information
1 parent
01e7405
commit 0c1a42c
Showing
11 changed files
with
123 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/C API/2024-03-22-19-29-24.gh-issue-87193.u7O-jY.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
:c:func:`_PyBytes_Resize` can now be called for bytes objects with reference | ||
count > 1, including 1-byte bytes objects. It creates a new bytes object and | ||
destroys the old one if it has reference count > 1. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "parts.h" | ||
#include "util.h" | ||
|
||
|
||
/* Test _PyBytes_Resize() */ | ||
static PyObject * | ||
bytes_resize(PyObject *Py_UNUSED(module), PyObject *args) | ||
{ | ||
PyObject *obj; | ||
Py_ssize_t newsize; | ||
int new; | ||
|
||
if (!PyArg_ParseTuple(args, "Onp", &obj, &newsize, &new)) | ||
return NULL; | ||
|
||
NULLABLE(obj); | ||
if (new) { | ||
assert(obj != NULL); | ||
assert(PyBytes_CheckExact(obj)); | ||
PyObject *newobj = PyBytes_FromStringAndSize(NULL, PyBytes_Size(obj)); | ||
if (newobj == NULL) { | ||
return NULL; | ||
} | ||
memcpy(PyBytes_AsString(newobj), PyBytes_AsString(obj), PyBytes_Size(obj)); | ||
obj = newobj; | ||
} | ||
else { | ||
Py_XINCREF(obj); | ||
} | ||
if (_PyBytes_Resize(&obj, newsize) < 0) { | ||
assert(obj == NULL); | ||
} | ||
else { | ||
assert(obj != NULL); | ||
} | ||
return obj; | ||
} | ||
|
||
|
||
static PyMethodDef test_methods[] = { | ||
{"bytes_resize", bytes_resize, METH_VARARGS}, | ||
{NULL}, | ||
}; | ||
|
||
int | ||
_PyTestCapi_Init_Bytes(PyObject *m) | ||
{ | ||
if (PyModule_AddFunctions(m, test_methods) < 0) { | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters