gh-111178: Fix function signatures in bytesobject.c (#124806)

This commit is contained in:
Victor Stinner 2024-10-02 13:35:51 +02:00 committed by GitHub
parent 8fbf10d6cf
commit 1d3700f943
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 41 additions and 36 deletions

View File

@ -1405,8 +1405,9 @@ bytes_str(PyObject *op)
} }
static Py_ssize_t static Py_ssize_t
bytes_length(PyBytesObject *a) bytes_length(PyObject *self)
{ {
PyBytesObject *a = _PyBytes_CAST(self);
return Py_SIZE(a); return Py_SIZE(a);
} }
@ -1456,11 +1457,9 @@ bytes_concat(PyObject *a, PyObject *b)
} }
static PyObject * static PyObject *
bytes_repeat(PyBytesObject *a, Py_ssize_t n) bytes_repeat(PyObject *self, Py_ssize_t n)
{ {
Py_ssize_t size; PyBytesObject *a = _PyBytes_CAST(self);
PyBytesObject *op;
size_t nbytes;
if (n < 0) if (n < 0)
n = 0; n = 0;
/* watch out for overflows: the size can overflow int, /* watch out for overflows: the size can overflow int,
@ -1471,17 +1470,17 @@ bytes_repeat(PyBytesObject *a, Py_ssize_t n)
"repeated bytes are too long"); "repeated bytes are too long");
return NULL; return NULL;
} }
size = Py_SIZE(a) * n; Py_ssize_t size = Py_SIZE(a) * n;
if (size == Py_SIZE(a) && PyBytes_CheckExact(a)) { if (size == Py_SIZE(a) && PyBytes_CheckExact(a)) {
return Py_NewRef(a); return Py_NewRef(a);
} }
nbytes = (size_t)size; size_t nbytes = (size_t)size;
if (nbytes + PyBytesObject_SIZE <= nbytes) { if (nbytes + PyBytesObject_SIZE <= nbytes) {
PyErr_SetString(PyExc_OverflowError, PyErr_SetString(PyExc_OverflowError,
"repeated bytes are too long"); "repeated bytes are too long");
return NULL; return NULL;
} }
op = (PyBytesObject *)PyObject_Malloc(PyBytesObject_SIZE + nbytes); PyBytesObject *op = PyObject_Malloc(PyBytesObject_SIZE + nbytes);
if (op == NULL) { if (op == NULL) {
return PyErr_NoMemory(); return PyErr_NoMemory();
} }
@ -1504,8 +1503,9 @@ bytes_contains(PyObject *self, PyObject *arg)
} }
static PyObject * static PyObject *
bytes_item(PyBytesObject *a, Py_ssize_t i) bytes_item(PyObject *self, Py_ssize_t i)
{ {
PyBytesObject *a = _PyBytes_CAST(self);
if (i < 0 || i >= Py_SIZE(a)) { if (i < 0 || i >= Py_SIZE(a)) {
PyErr_SetString(PyExc_IndexError, "index out of range"); PyErr_SetString(PyExc_IndexError, "index out of range");
return NULL; return NULL;
@ -1531,21 +1531,17 @@ bytes_compare_eq(PyBytesObject *a, PyBytesObject *b)
} }
static PyObject* static PyObject*
bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op) bytes_richcompare(PyObject *aa, PyObject *bb, int op)
{ {
int c;
Py_ssize_t len_a, len_b;
Py_ssize_t min_len;
/* Make sure both arguments are strings. */ /* Make sure both arguments are strings. */
if (!(PyBytes_Check(a) && PyBytes_Check(b))) { if (!(PyBytes_Check(aa) && PyBytes_Check(bb))) {
if (_Py_GetConfig()->bytes_warning && (op == Py_EQ || op == Py_NE)) { if (_Py_GetConfig()->bytes_warning && (op == Py_EQ || op == Py_NE)) {
if (PyUnicode_Check(a) || PyUnicode_Check(b)) { if (PyUnicode_Check(aa) || PyUnicode_Check(bb)) {
if (PyErr_WarnEx(PyExc_BytesWarning, if (PyErr_WarnEx(PyExc_BytesWarning,
"Comparison between bytes and string", 1)) "Comparison between bytes and string", 1))
return NULL; return NULL;
} }
if (PyLong_Check(a) || PyLong_Check(b)) { if (PyLong_Check(aa) || PyLong_Check(bb)) {
if (PyErr_WarnEx(PyExc_BytesWarning, if (PyErr_WarnEx(PyExc_BytesWarning,
"Comparison between bytes and int", 1)) "Comparison between bytes and int", 1))
return NULL; return NULL;
@ -1553,7 +1549,10 @@ bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
} }
Py_RETURN_NOTIMPLEMENTED; Py_RETURN_NOTIMPLEMENTED;
} }
else if (a == b) {
PyBytesObject *a = _PyBytes_CAST(aa);
PyBytesObject *b = _PyBytes_CAST(bb);
if (a == b) {
switch (op) { switch (op) {
case Py_EQ: case Py_EQ:
case Py_LE: case Py_LE:
@ -1575,25 +1574,29 @@ bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
return PyBool_FromLong(eq); return PyBool_FromLong(eq);
} }
else { else {
len_a = Py_SIZE(a); Py_ssize_t len_a = Py_SIZE(a);
len_b = Py_SIZE(b); Py_ssize_t len_b = Py_SIZE(b);
min_len = Py_MIN(len_a, len_b); Py_ssize_t min_len = Py_MIN(len_a, len_b);
int c;
if (min_len > 0) { if (min_len > 0) {
c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval); c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval);
if (c == 0) if (c == 0)
c = memcmp(a->ob_sval, b->ob_sval, min_len); c = memcmp(a->ob_sval, b->ob_sval, min_len);
} }
else else {
c = 0; c = 0;
if (c != 0) }
if (c != 0) {
Py_RETURN_RICHCOMPARE(c, 0, op); Py_RETURN_RICHCOMPARE(c, 0, op);
}
Py_RETURN_RICHCOMPARE(len_a, len_b, op); Py_RETURN_RICHCOMPARE(len_a, len_b, op);
} }
} }
static Py_hash_t static Py_hash_t
bytes_hash(PyBytesObject *a) bytes_hash(PyObject *self)
{ {
PyBytesObject *a = _PyBytes_CAST(self);
_Py_COMP_DIAG_PUSH _Py_COMP_DIAG_PUSH
_Py_COMP_DIAG_IGNORE_DEPR_DECLS _Py_COMP_DIAG_IGNORE_DEPR_DECLS
if (a->ob_shash == -1) { if (a->ob_shash == -1) {
@ -1605,8 +1608,9 @@ _Py_COMP_DIAG_POP
} }
static PyObject* static PyObject*
bytes_subscript(PyBytesObject* self, PyObject* item) bytes_subscript(PyObject *op, PyObject* item)
{ {
PyBytesObject *self = _PyBytes_CAST(op);
if (_PyIndex_Check(item)) { if (_PyIndex_Check(item)) {
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
if (i == -1 && PyErr_Occurred()) if (i == -1 && PyErr_Occurred())
@ -1670,31 +1674,32 @@ bytes_subscript(PyBytesObject* self, PyObject* item)
} }
static int static int
bytes_buffer_getbuffer(PyBytesObject *self, Py_buffer *view, int flags) bytes_buffer_getbuffer(PyObject *op, Py_buffer *view, int flags)
{ {
PyBytesObject *self = _PyBytes_CAST(op);
return PyBuffer_FillInfo(view, (PyObject*)self, (void *)self->ob_sval, Py_SIZE(self), return PyBuffer_FillInfo(view, (PyObject*)self, (void *)self->ob_sval, Py_SIZE(self),
1, flags); 1, flags);
} }
static PySequenceMethods bytes_as_sequence = { static PySequenceMethods bytes_as_sequence = {
(lenfunc)bytes_length, /*sq_length*/ bytes_length, /*sq_length*/
(binaryfunc)bytes_concat, /*sq_concat*/ bytes_concat, /*sq_concat*/
(ssizeargfunc)bytes_repeat, /*sq_repeat*/ bytes_repeat, /*sq_repeat*/
(ssizeargfunc)bytes_item, /*sq_item*/ bytes_item, /*sq_item*/
0, /*sq_slice*/ 0, /*sq_slice*/
0, /*sq_ass_item*/ 0, /*sq_ass_item*/
0, /*sq_ass_slice*/ 0, /*sq_ass_slice*/
(objobjproc)bytes_contains /*sq_contains*/ bytes_contains /*sq_contains*/
}; };
static PyMappingMethods bytes_as_mapping = { static PyMappingMethods bytes_as_mapping = {
(lenfunc)bytes_length, bytes_length,
(binaryfunc)bytes_subscript, bytes_subscript,
0, 0,
}; };
static PyBufferProcs bytes_as_buffer = { static PyBufferProcs bytes_as_buffer = {
(getbufferproc)bytes_buffer_getbuffer, bytes_buffer_getbuffer,
NULL, NULL,
}; };
@ -3043,11 +3048,11 @@ PyTypeObject PyBytes_Type = {
0, /* tp_getattr */ 0, /* tp_getattr */
0, /* tp_setattr */ 0, /* tp_setattr */
0, /* tp_as_async */ 0, /* tp_as_async */
(reprfunc)bytes_repr, /* tp_repr */ bytes_repr, /* tp_repr */
&bytes_as_number, /* tp_as_number */ &bytes_as_number, /* tp_as_number */
&bytes_as_sequence, /* tp_as_sequence */ &bytes_as_sequence, /* tp_as_sequence */
&bytes_as_mapping, /* tp_as_mapping */ &bytes_as_mapping, /* tp_as_mapping */
(hashfunc)bytes_hash, /* tp_hash */ bytes_hash, /* tp_hash */
0, /* tp_call */ 0, /* tp_call */
bytes_str, /* tp_str */ bytes_str, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */ PyObject_GenericGetAttr, /* tp_getattro */