cpython: 2e684ce772de (original) (raw)

--- a/Include/bytes_methods.h +++ b/Include/bytes_methods.h @@ -22,7 +22,7 @@ extern void _Py_bytes_capitalize(char *r extern void _Py_bytes_swapcase(char *result, char s, Py_ssize_t len); / The maketrans() static method. / -extern PyObject _Py_bytes_maketrans(PyObject *frm, PyObject to); +extern PyObject Py_bytes_maketrans(Py_buffer *frm, Py_buffer to); / Shared doc strings. */ extern const char Py_isspace__doc[];

--- a/Lib/ctypes/test/test_frombuffer.py +++ b/Lib/ctypes/test/test_frombuffer.py @@ -10,7 +10,7 @@ class X(Structure): self._init_called = True class Test(unittest.TestCase):

@@ -23,25 +23,37 @@ class Test(unittest.TestCase): a[0], a[-1] = 200, -200 self.assertEqual(x[:], a.tolist())

+

expected = x[:] del a; gc.collect(); gc.collect(); gc.collect() self.assertEqual(x[:], expected)

self.assertEqual(x[:], a.tolist()[1:])

def test_from_buffer_copy(self): a = array.array("i", range(16)) @@ -56,26 +68,30 @@ class Test(unittest.TestCase): a[0], a[-1] = 200, -200 self.assertEqual(x[:], list(range(16)))

del a; gc.collect(); gc.collect(); gc.collect() self.assertEqual(x[:], list(range(16))) x = (c_char * 16).from_buffer_copy(b"a" * 16) self.assertEqual(x[:], b"a" * 16)

self.assertEqual(x[:], a.tolist()[1:])

if name == 'main': unittest.main()

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ Release date: TBA Core and Builtins ----------------- +- Issue #22896: Avoid using PyObject_AsCharBuffer(), PyObject_AsReadBuffer()

--- a/Modules/_codecsmodule.c +++ b/Modules/_codecsmodule.c @@ -288,8 +288,6 @@ unicode_internal_decode(PyObject *self, { PyObject *obj; const char *errors = NULL;

if (!PyArg_ParseTuple(args, "O|z:unicode_internal_decode", &obj, &errors)) @@ -302,11 +300,16 @@ unicode_internal_decode(PyObject *self, return codec_tuple(obj, PyUnicode_GET_LENGTH(obj)); } else {

if (PyErr_WarnEx(PyExc_DeprecationWarning, "unicode_internal codec has been deprecated", @@ -745,6 +746,7 @@ unicode_internal_encode(PyObject *self, if (PyUnicode_Check(obj)) { Py_UNICODE *u;

if (PyUnicode_READY(obj) < 0) return NULL; @@ -759,9 +761,13 @@ unicode_internal_encode(PyObject *self, PyUnicode_GET_LENGTH(obj)); } else {

--- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -463,39 +463,45 @@ KeepRef(CDataObject *target, Py_ssize_t static PyObject * CDataType_from_buffer(PyObject *type, PyObject *args) {

-

if (offset < 0) { PyErr_SetString(PyExc_ValueError, "offset cannot be negative");

+

-

-

if (offset < 0) { PyErr_SetString(PyExc_ValueError, "offset cannot be negative");

--- a/Modules/_io/bytesio.c +++ b/Modules/_io/bytesio.c @@ -553,17 +553,18 @@ PyDoc_STRVAR(readinto_doc, "is set not to block as has no data to read."); static PyObject * -bytesio_readinto(bytesio *self, PyObject *buffer) +bytesio_readinto(bytesio *self, PyObject *arg) {

/* adjust invalid sizes */

@@ -571,10 +572,11 @@ bytesio_readinto(bytesio *self, PyObject len = 0; }

return PyLong_FromSsize_t(len); }

--- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -522,19 +522,20 @@ static int return -1; sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT); } else if (PyObject_CheckBuffer(py_val)) {

--- a/Modules/_sqlite/statement.c +++ b/Modules/_sqlite/statement.c @@ -94,7 +94,6 @@ int pysqlite_statement_create(pysqlite_S int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter) { int rc = SQLITE_OK;

--- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -1842,8 +1842,8 @@ static PyObject * s_pack_into(PyObject *self, PyObject *args) { PyStructObject *soself;

/* Validate arguments. +1 is for the first arg as buffer. */ soself = (PyStructObject *)self; @@ -1868,34 +1868,37 @@ s_pack_into(PyObject *self, PyObject ar } / Extract a writable memory buffer from the first argument */

/* Extract the offset from the first argument */ offset = PyNumber_AsSsize_t(PyTuple_GET_ITEM(args, 1), PyExc_IndexError);

/* Support negative offsets. */ if (offset < 0)

/* Check boundaries */

--- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -250,27 +250,7 @@ PyObject_AsCharBuffer(PyObject *obj, const char **buffer, Py_ssize_t *buffer_len) {

-

-

} int @@ -294,28 +274,18 @@ int PyObject_AsReadBuffer(PyObject *obj, const void **buffer, Py_ssize_t *buffer_len) {

-

*buffer = view.buf; *buffer_len = view.len;

@@ -341,9 +311,7 @@ int PyObject_AsWriteBuffer(PyObject *obj *buffer = view.buf; *buffer_len = view.len;

@@ -352,13 +320,15 @@ int PyObject_AsWriteBuffer(PyObject *obj int PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags) {

+

} static int @@ -676,10 +646,14 @@ void PyBuffer_Release(Py_buffer *view) { PyObject *obj = view->obj;

} PyObject * @@ -1288,8 +1262,7 @@ PyNumber_Long(PyObject *o) { PyNumberMethods *m; PyObject *trunc_func;

+

-

+

} PyObject *

--- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -80,24 +80,6 @@ bytearray_releasebuffer(PyByteArrayObjec obj->ob_exports--; } -static Py_ssize_t -_getbuffer(PyObject *obj, Py_buffer *view) -{

-

-

-} - static int _canresize(PyByteArrayObject *self) { @@ -268,8 +250,8 @@ PyByteArray_Concat(PyObject *a, PyObject va.len = -1; vb.len = -1;

@@ -335,7 +317,7 @@ bytearray_iconcat(PyByteArrayObject *sel Py_ssize_t size; Py_buffer vo;

@@ -595,14 +577,14 @@ bytearray_setslice(PyByteArrayObject *se needed = 0; } else {

+

if (self_size != other_size && (op == Py_EQ || op == Py_NE)) { /* Shortcut: if the lengths differ, the objects differ */ @@ -1170,7 +1152,7 @@ bytearray_find_internal(PyByteArrayObjec return -2; if (subobj) {

sub = subbuf.buf; @@ -1238,7 +1220,7 @@ bytearray_count(PyByteArrayObject *self, return NULL; if (sub_obj) {

sub = vsub.buf; @@ -1397,7 +1379,7 @@ bytearray_contains(PyObject *self, PyObj Py_buffer varg; Py_ssize_t pos; PyErr_Clear();

@@ -1428,7 +1410,7 @@ Py_LOCAL(int) str = PyByteArray_AS_STRING(self);

ADJUST_INDICES(start, end, len); @@ -1621,7 +1603,7 @@ bytearray_translate_impl(PyByteArrayObje if (table == Py_None) { table_chars = NULL; table = NULL;

@@ -1634,7 +1616,7 @@ bytearray_translate_impl(PyByteArrayObje } if (deletechars != NULL) {

@@ -1699,8 +1681,8 @@ done: @staticmethod bytearray.maketrans

-

+

exit:

+ return return_value; } static PyObject * -bytearray_maketrans_impl(PyObject frm, PyObject to) -/[clinic end generated code: output=307752019d9b25b5 input=ea9bdc6b328c15e2]/ +bytearray_maketrans_impl(Py_buffer frm, Py_buffer to) +/[clinic end generated code: output=d332622814c26f4b input=5925a81d2fbbf151]/ { return _Py_bytes_maketrans(frm, to); } @@ -2243,8 +2232,8 @@ replace(PyByteArrayObject self, /[clinic input] bytearray.replace

@@ -2273,47 +2262,40 @@ PyDoc_STRVAR(bytearray_replace__doc__, {"replace", (PyCFunction)bytearray_replace, METH_VARARGS, bytearray_replace__doc__}, static PyObject * -bytearray_replace_impl(PyByteArrayObject *self, PyObject *old, PyObject *new, Py_ssize_t count); +bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old, Py_buffer *new, Py_ssize_t count); static PyObject * bytearray_replace(PyByteArrayObject *self, PyObject *args) { PyObject *return_value = NULL;

exit:

+ return return_value; } static PyObject * -bytearray_replace_impl(PyByteArrayObject *self, PyObject old, PyObject new, Py_ssize_t count) -/[clinic end generated code: output=4d2e3c9130da0f96 input=9aaaa123608dfc1f]/ +bytearray_replace_impl(PyByteArrayObject *self, Py_buffer old, Py_buffer new, Py_ssize_t count) +/[clinic end generated code: output=9997fbbd5bac4883 input=aa379d988637c7fb]/ {

-

-

-

} /*[clinic input] @@ -2383,7 +2365,7 @@ bytearray_split_impl(PyByteArrayObject if (sep == Py_None) return stringlib_split_whitespace((PyObject) self, s, len, maxsplit);

@@ -3159,7 +3141,7 @@ bytearray_lstrip_impl(PyByteArrayObject byteslen = 6; } else {

@@ -3227,7 +3209,7 @@ bytearray_rstrip_impl(PyByteArrayObject byteslen = 6; } else {

--- a/Objects/bytes_methods.c +++ b/Objects/bytes_methods.c @@ -363,59 +363,27 @@ for use in the bytes or bytearray transl in frm is mapped to the byte at the same position in to.\n[](#l12.4) The bytes objects frm and to must be of the same length."); -static Py_ssize_t -_getbuffer(PyObject *obj, Py_buffer *view) -{

-

-

-} - PyObject * -_Py_bytes_maketrans(PyObject *frm, PyObject *to) +_Py_bytes_maketrans(Py_buffer *frm, Py_buffer *to) { PyObject *res = NULL;

-

--- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -12,33 +12,6 @@ class bytes "PyBytesObject*" "&PyBytes_T [clinic start generated code]/ /[clinic end generated code: output=da39a3ee5e6b4b0d input=1a1d9102afc1b00c]*/ -static Py_ssize_t -_getbuffer(PyObject *obj, Py_buffer *view) -{

-

-

-} - #ifdef COUNT_ALLOCS Py_ssize_t null_strings, one_strings; #endif @@ -1349,8 +1322,8 @@ bytes_concat(PyObject *a, PyObject *b) va.len = -1; vb.len = -1;

@@ -1448,7 +1421,7 @@ bytes_contains(PyObject *self, PyObject Py_buffer varg; Py_ssize_t pos; PyErr_Clear();

@@ -1737,7 +1710,7 @@ bytes_split_impl(PyBytesObjectself, PyO maxsplit = PY_SSIZE_T_MAX; if (sep == Py_None) return stringlib_split_whitespace((PyObject) self, s, len, maxsplit);

#define BYTES_PARTITION_METHODDEF [](#l13.78)

+ +static PyObject * +bytes_partition_impl(PyBytesObject *self, Py_buffer *sep); static PyObject * -bytes_partition(PyBytesObject self, PyObject sep) -/[clinic end generated code: output=b41e119c873c08bc input=6c5b9dcc5a9fd62e]/ +bytes_partition(PyBytesObject *self, PyObject *args) {

-

-

+

+ +exit:

+

+} + +static PyObject +bytes_partition_impl(PyBytesObject self, Py_buffer sep) +/[clinic end generated code: output=3006727cfbf83aa4 input=bc855dc63ca949de]/ +{ return stringlib_partition( (PyObject) self, PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),

} @@ -1805,7 +1791,7 @@ bytes_partition(PyBytesObject *self, PyO bytes.rpartition self: self(type="PyBytesObject *")

#define BYTES_RPARTITION_METHODDEF [](#l13.141)

+ +static PyObject * +bytes_rpartition_impl(PyBytesObject *self, Py_buffer *sep); static PyObject * -bytes_rpartition(PyBytesObject self, PyObject sep) -/[clinic end generated code: output=3a620803657196ee input=79bc2932e78e5ce0]/ +bytes_rpartition(PyBytesObject *self, PyObject *args) {

-

-

+

+ +exit:

+

+} + +static PyObject +bytes_rpartition_impl(PyBytesObject self, Py_buffer sep) +/[clinic end generated code: output=57b169dc47fa90e8 input=6588fff262a9170e]/ +{ return stringlib_rpartition( (PyObject) self, PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),

} @@ -1916,7 +1915,7 @@ bytes_rsplit_impl(PyBytesObjectself, Py maxsplit = PY_SSIZE_T_MAX; if (sep == Py_None) return stringlib_rsplit_whitespace((PyObject) self, s, len, maxsplit);

if (subobj) {

sub = subbuf.buf; @@ -2118,7 +2117,7 @@ do_xstrip(PyBytesObject *self, int strip Py_ssize_t seplen; Py_ssize_t i, j;

if (sub_obj) {

sub = vsub.buf; @@ -2450,6 +2449,8 @@ bytes_translate_impl(PyBytesObject self /[clinic end generated code: output=f0f29a57f41df5d8 input=d8fa5519d7cc4be7]*/ { char *input, *output;

if (tablen != 256) { PyErr_SetString(PyExc_ValueError, "translation table must be 256 characters long");

@@ -2490,8 +2502,11 @@ bytes_translate_impl(PyBytesObject *self inlen = PyBytes_GET_SIZE(input_obj); result = PyBytes_FromStringAndSize((char *)NULL, inlen);

for (i = 0; i < dellen; i++) trans_table[(int) Py_CHARMASK(del_table_chars[i])] = -1;

for (i = inlen; --i >= 0; ) { c = Py_CHARMASK(*input++); @@ -2544,8 +2564,8 @@ bytes_translate_impl(PyBytesObject *self @staticmethod bytes.maketrans

-

+

exit:

+ return return_value; } static PyObject * -bytes_maketrans_impl(PyObject frm, PyObject to) -/[clinic end generated code: output=89a3c3556975e466 input=d204f680f85da382]/ +bytes_maketrans_impl(Py_buffer frm, Py_buffer to) +/[clinic end generated code: output=7df47390c476ac60 input=de7a8fc5632bb8f1]/ { return _Py_bytes_maketrans(frm, to); } @@ -3093,8 +3120,8 @@ replace(PyBytesObject self, /[clinic input] bytes.replace

@@ -3123,50 +3150,40 @@ PyDoc_STRVAR(bytes_replace__doc__, {"replace", (PyCFunction)bytes_replace, METH_VARARGS, bytes_replace__doc__}, static PyObject -bytes_replace_impl(PyBytesObjectself, PyObject *old, PyObject new, Py_ssize_t count); +bytes_replace_impl(PyBytesObjectself, Py_buffer *old, Py_buffer *new, Py_ssize_t count); static PyObject bytes_replace(PyBytesObjectself, PyObject *args) { PyObject *return_value = NULL;

exit:

+ return return_value; } static PyObject -bytes_replace_impl(PyBytesObjectself, PyObject old, PyObject new, Py_ssize_t count) -/[clinic end generated code: output=14ce72f4f9cb91cf input=d3ac254ea50f4ac1]/ +bytes_replace_impl(PyBytesObject*self, Py_buffer old, Py_buffer new, Py_ssize_t count) +/[clinic end generated code: output=f07bd9ecf29ee8d8 input=b2fbbf0bf04de8e5]/ {

-

-

- return (PyObject *)replace((PyBytesObject *) self,

} /** End DALKE **/ @@ -3181,6 +3198,7 @@ Py_LOCAL(int) { Py_ssize_t len = PyBytes_GET_SIZE(self); Py_ssize_t slen;

if (end-slen > start) start = end - slen; }

+

+ +notfound:

@@ -3978,7 +4008,7 @@ PyBytes_Concat(PyObject **pv, PyObject * Py_buffer wb; wb.len = -1;

--- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -767,6 +767,7 @@ complex_subtype_from_string(PyTypeObject int got_bracket=0; PyObject *s_buffer = NULL; Py_ssize_t len;

if (PyUnicode_Check(v)) { s_buffer = _PyUnicode_TransformDecimalAndSpaceToASCII(v); @@ -776,7 +777,11 @@ complex_subtype_from_string(PyTypeObject if (s == NULL) goto error; }

@@ -890,6 +895,7 @@ complex_subtype_from_string(PyTypeObject if (s-start != len) goto parse_error;

error:

--- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -1922,8 +1922,6 @@ static int UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds) { PyUnicodeErrorObject *ude;

if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) return -1; @@ -1944,21 +1942,27 @@ UnicodeDecodeError_init(PyObject *self, return -1; }

+ if (!PyBytes_Check(ude->object)) {

-

- return 0; + +error:

} static PyObject *

--- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -131,6 +131,7 @@ PyFloat_FromString(PyObject *v) double x; PyObject *s_buffer = NULL; Py_ssize_t len;

@@ -170,6 +175,7 @@ PyFloat_FromString(PyObject *v) else result = PyFloat_FromDouble(x);

--- a/Objects/stringlib/join.h +++ b/Objects/stringlib/join.h @@ -58,7 +58,14 @@ STRINGLIB(bytes_join)(PyObject *sep, PyO for (i = 0, nbufs = 0; i < seqlen; i++) { Py_ssize_t itemlen; item = PySequence_Fast_GET_ITEM(seq, i);

--- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -723,10 +723,10 @@ builtin_chr_impl(PyModuleDef *module, in } -static char * -source_as_string(PyObject *cmd, char *funcname, char *what, PyCompilerFlags *cf) +static const char * +source_as_string(PyObject *cmd, const char *funcname, const char *what, PyCompilerFlags *cf, Py_buffer *view) {

-

+

result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, optimize);

@@ -1098,6 +1103,7 @@ builtin_eval_impl(PyModuleDef *module, P (void)PyEval_MergeCompilerFlags(&cf); result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);

@@ -1204,11 +1210,12 @@ builtin_exec_impl(PyModuleDef *module, P v = PyEval_EvalCode(source, globals, locals); } else {

@@ -1216,6 +1223,7 @@ builtin_exec_impl(PyModuleDef *module, P locals, &cf); else v = PyRun_String(str, Py_file_input, globals, locals);