Issue #22896: Avoid to use PyObject_AsCharBuffer(), PyObject_AsReadBuffer()

and PyObject_AsWriteBuffer().
This commit is contained in:
Serhiy Storchaka 2015-02-03 01:25:42 +02:00
commit 3dd3e26680
18 changed files with 422 additions and 397 deletions

View file

@ -80,24 +80,6 @@ bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
obj->ob_exports--;
}
static Py_ssize_t
_getbuffer(PyObject *obj, Py_buffer *view)
{
PyBufferProcs *buffer = Py_TYPE(obj)->tp_as_buffer;
if (buffer == NULL || buffer->bf_getbuffer == NULL)
{
PyErr_Format(PyExc_TypeError,
"a bytes-like object is required, not '%.100s'",
Py_TYPE(obj)->tp_name);
return -1;
}
if (buffer->bf_getbuffer(obj, view, PyBUF_SIMPLE) < 0)
return -1;
return view->len;
}
static int
_canresize(PyByteArrayObject *self)
{
@ -268,8 +250,8 @@ PyByteArray_Concat(PyObject *a, PyObject *b)
va.len = -1;
vb.len = -1;
if (_getbuffer(a, &va) < 0 ||
_getbuffer(b, &vb) < 0) {
if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 ||
PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) {
PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
goto done;
@ -335,7 +317,7 @@ bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
Py_ssize_t size;
Py_buffer vo;
if (_getbuffer(other, &vo) < 0) {
if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) {
PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
return NULL;
@ -595,14 +577,14 @@ bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
needed = 0;
}
else {
if (_getbuffer(values, &vbytes) < 0) {
PyErr_Format(PyExc_TypeError,
"can't set bytearray slice from %.100s",
Py_TYPE(values)->tp_name);
return -1;
}
needed = vbytes.len;
bytes = vbytes.buf;
if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) {
PyErr_Format(PyExc_TypeError,
"can't set bytearray slice from %.100s",
Py_TYPE(values)->tp_name);
return -1;
}
needed = vbytes.len;
bytes = vbytes.buf;
}
if (lo < 0)
@ -1047,18 +1029,18 @@ bytearray_richcompare(PyObject *self, PyObject *other, int op)
Py_RETURN_NOTIMPLEMENTED;
}
self_size = _getbuffer(self, &self_bytes);
if (self_size < 0) {
if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
PyErr_Clear();
Py_RETURN_NOTIMPLEMENTED;
}
self_size = self_bytes.len;
other_size = _getbuffer(other, &other_bytes);
if (other_size < 0) {
if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
PyErr_Clear();
PyBuffer_Release(&self_bytes);
Py_RETURN_NOTIMPLEMENTED;
}
other_size = other_bytes.len;
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(PyByteArrayObject *self, PyObject *args, int dir)
return -2;
if (subobj) {
if (_getbuffer(subobj, &subbuf) < 0)
if (PyObject_GetBuffer(subobj, &subbuf, PyBUF_SIMPLE) != 0)
return -2;
sub = subbuf.buf;
@ -1238,7 +1220,7 @@ bytearray_count(PyByteArrayObject *self, PyObject *args)
return NULL;
if (sub_obj) {
if (_getbuffer(sub_obj, &vsub) < 0)
if (PyObject_GetBuffer(sub_obj, &vsub, PyBUF_SIMPLE) != 0)
return NULL;
sub = vsub.buf;
@ -1397,7 +1379,7 @@ bytearray_contains(PyObject *self, PyObject *arg)
Py_buffer varg;
Py_ssize_t pos;
PyErr_Clear();
if (_getbuffer(arg, &varg) < 0)
if (PyObject_GetBuffer(arg, &varg, PyBUF_SIMPLE) != 0)
return -1;
pos = stringlib_find(PyByteArray_AS_STRING(self), Py_SIZE(self),
varg.buf, varg.len, 0);
@ -1428,7 +1410,7 @@ _bytearray_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start
str = PyByteArray_AS_STRING(self);
if (_getbuffer(substr, &vsubstr) < 0)
if (PyObject_GetBuffer(substr, &vsubstr, PyBUF_SIMPLE) != 0)
return -1;
ADJUST_INDICES(start, end, len);
@ -1621,7 +1603,7 @@ bytearray_translate_impl(PyByteArrayObject *self, PyObject *table, int group_rig
if (table == Py_None) {
table_chars = NULL;
table = NULL;
} else if (_getbuffer(table, &vtable) < 0) {
} else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
return NULL;
} else {
if (vtable.len != 256) {
@ -1634,7 +1616,7 @@ bytearray_translate_impl(PyByteArrayObject *self, PyObject *table, int group_rig
}
if (deletechars != NULL) {
if (_getbuffer(deletechars, &vdel) < 0) {
if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
if (table != NULL)
PyBuffer_Release(&vtable);
return NULL;
@ -1699,8 +1681,8 @@ done:
@staticmethod
bytearray.maketrans
frm: object
to: object
frm: Py_buffer
to: Py_buffer
/
Return a translation table useable for the bytes or bytearray translate method.
@ -1726,28 +1708,35 @@ PyDoc_STRVAR(bytearray_maketrans__doc__,
{"maketrans", (PyCFunction)bytearray_maketrans, METH_VARARGS|METH_STATIC, bytearray_maketrans__doc__},
static PyObject *
bytearray_maketrans_impl(PyObject *frm, PyObject *to);
bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to);
static PyObject *
bytearray_maketrans(void *null, PyObject *args)
{
PyObject *return_value = NULL;
PyObject *frm;
PyObject *to;
Py_buffer frm = {NULL, NULL};
Py_buffer to = {NULL, NULL};
if (!PyArg_UnpackTuple(args, "maketrans",
2, 2,
if (!PyArg_ParseTuple(args,
"y*y*:maketrans",
&frm, &to))
goto exit;
return_value = bytearray_maketrans_impl(frm, to);
return_value = bytearray_maketrans_impl(&frm, &to);
exit:
/* Cleanup for frm */
if (frm.obj)
PyBuffer_Release(&frm);
/* Cleanup for to */
if (to.obj)
PyBuffer_Release(&to);
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
old: object
new: object
old: Py_buffer
new: Py_buffer
count: Py_ssize_t = -1
Maximum number of occurrences to replace.
-1 (the default value) means replace all occurrences.
@ -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;
PyObject *old;
PyObject *new;
Py_buffer old = {NULL, NULL};
Py_buffer new = {NULL, NULL};
Py_ssize_t count = -1;
if (!PyArg_ParseTuple(args,
"OO|n:replace",
"y*y*|n:replace",
&old, &new, &count))
goto exit;
return_value = bytearray_replace_impl(self, old, new, count);
return_value = bytearray_replace_impl(self, &old, &new, count);
exit:
/* Cleanup for old */
if (old.obj)
PyBuffer_Release(&old);
/* Cleanup for new */
if (new.obj)
PyBuffer_Release(&new);
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]*/
{
PyObject *res;
Py_buffer vold, vnew;
if (_getbuffer(old, &vold) < 0)
return NULL;
if (_getbuffer(new, &vnew) < 0) {
PyBuffer_Release(&vold);
return NULL;
}
res = (PyObject *)replace((PyByteArrayObject *) self,
vold.buf, vold.len,
vnew.buf, vnew.len, count);
PyBuffer_Release(&vold);
PyBuffer_Release(&vnew);
return res;
return (PyObject *)replace((PyByteArrayObject *) self,
old->buf, old->len,
new->buf, new->len, count);
}
/*[clinic input]
@ -2383,7 +2365,7 @@ bytearray_split_impl(PyByteArrayObject *self, PyObject *sep, Py_ssize_t maxsplit
if (sep == Py_None)
return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
if (_getbuffer(sep, &vsub) < 0)
if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
return NULL;
sub = vsub.buf;
n = vsub.len;
@ -2566,7 +2548,7 @@ bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep, Py_ssize_t maxspli
if (sep == Py_None)
return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
if (_getbuffer(sep, &vsub) < 0)
if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
return NULL;
sub = vsub.buf;
n = vsub.len;
@ -3088,7 +3070,7 @@ bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
byteslen = 6;
}
else {
if (_getbuffer(bytes, &vbytes) < 0)
if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
return NULL;
bytesptr = (char *) vbytes.buf;
byteslen = vbytes.len;
@ -3159,7 +3141,7 @@ bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
byteslen = 6;
}
else {
if (_getbuffer(bytes, &vbytes) < 0)
if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
return NULL;
bytesptr = (char *) vbytes.buf;
byteslen = vbytes.len;
@ -3227,7 +3209,7 @@ bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
byteslen = 6;
}
else {
if (_getbuffer(bytes, &vbytes) < 0)
if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
return NULL;
bytesptr = (char *) vbytes.buf;
byteslen = vbytes.len;