Py_END_CRITICAL_SECTION();
}
+typedef PyObject* (*_ba_bytes_op)(const char *buf, Py_ssize_t len,
+ PyObject *sub, Py_ssize_t start,
+ Py_ssize_t end);
+
+static PyObject *
+_bytearray_with_buffer(PyByteArrayObject *self, _ba_bytes_op op, PyObject *sub,
+ Py_ssize_t start, Py_ssize_t end)
+{
+ PyObject *res;
+
+ _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
+
+ /* Increase exports to prevent bytearray storage from changing during op. */
+ self->ob_exports++;
+ res = op(PyByteArray_AS_STRING(self), Py_SIZE(self), sub, start, end);
+ self->ob_exports--;
+ return res;
+}
+
static int
_canresize(PyByteArrayObject *self)
{
Py_ssize_t end)
/*[clinic end generated code: output=413e1cab2ae87da0 input=df3aa94840d893a7]*/
{
- return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
- sub, start, end);
+ return _bytearray_with_buffer(self, _Py_bytes_find, sub, start, end);
}
/*[clinic input]
Py_ssize_t start, Py_ssize_t end)
/*[clinic end generated code: output=a21ee2692e4f1233 input=e8fcdca8272857e0]*/
{
- return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
- sub, start, end);
+ return _bytearray_with_buffer(self, _Py_bytes_count, sub, start, end);
}
/*[clinic input]
Py_ssize_t start, Py_ssize_t end)
/*[clinic end generated code: output=067a1e78efc672a7 input=c37f177cfee19fe4]*/
{
- return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
- sub, start, end);
+ return _bytearray_with_buffer(self, _Py_bytes_index, sub, start, end);
}
/*[clinic input]
Py_ssize_t start, Py_ssize_t end)
/*[clinic end generated code: output=51bf886f932b283c input=1265b11c437d2750]*/
{
- return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
- sub, start, end);
+ return _bytearray_with_buffer(self, _Py_bytes_rfind, sub, start, end);
}
/*[clinic input]
Py_ssize_t start, Py_ssize_t end)
/*[clinic end generated code: output=38e1cf66bafb08b9 input=7d198b3d6b0a62ce]*/
{
- return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
- sub, start, end);
+ return _bytearray_with_buffer(self, _Py_bytes_rindex, sub, start, end);
}
static int
bytearray_contains(PyObject *self, PyObject *arg)
{
- int ret;
+ int ret = -1;
Py_BEGIN_CRITICAL_SECTION(self);
- ret = _Py_bytes_contains(PyByteArray_AS_STRING(self),
+ PyByteArrayObject *ba = _PyByteArray_CAST(self);
+
+ /* Increase exports to prevent bytearray storage from changing during _Py_bytes_contains(). */
+ ba->ob_exports++;
+ ret = _Py_bytes_contains(PyByteArray_AS_STRING(ba),
PyByteArray_GET_SIZE(self),
arg);
+ ba->ob_exports--;
Py_END_CRITICAL_SECTION();
return ret;
}
Py_ssize_t start, Py_ssize_t end)
/*[clinic end generated code: output=a3d9b6d44d3662a6 input=93f9ffee684f109a]*/
{
- return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
- subobj, start, end);
+ return _bytearray_with_buffer(self, _Py_bytes_startswith, subobj, start, end);
}
/*[clinic input]
Py_ssize_t start, Py_ssize_t end)
/*[clinic end generated code: output=e75ea8c227954caa input=d158b030a11d0b06]*/
{
- return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
- subobj, start, end);
+ return _bytearray_with_buffer(self, _Py_bytes_endswith, subobj, start, end);
}
/*[clinic input]
Py_ssize_t maxsplit)
/*[clinic end generated code: output=833e2cf385d9a04d input=dd9f6e2910cc3a34]*/
{
- Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
- const char *s = PyByteArray_AS_STRING(self), *sub;
- PyObject *list;
- Py_buffer vsub;
+ PyObject *list = NULL;
+
+ /* Increase exports to prevent bytearray storage from changing during _Py_bytes_contains(). */
+ self->ob_exports++;
+ const char *sbuf = PyByteArray_AS_STRING(self);
+ Py_ssize_t slen = PyByteArray_GET_SIZE((PyObject *)self);
if (maxsplit < 0)
maxsplit = PY_SSIZE_T_MAX;
- if (sep == Py_None)
- return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
+ if (sep == Py_None) {
+ list = stringlib_split_whitespace((PyObject*)self, sbuf, slen, maxsplit);
+ goto done;
+ }
- if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
- return NULL;
- sub = vsub.buf;
- n = vsub.len;
+ Py_buffer vsub;
+ if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) {
+ goto done;
+ }
- list = stringlib_split(
- (PyObject*) self, s, len, sub, n, maxsplit
- );
+ list = stringlib_split((PyObject*)self, sbuf, slen,
+ (const char *)vsub.buf, vsub.len, maxsplit);
PyBuffer_Release(&vsub);
+
+done:
+ self->ob_exports--;
return list;
}
Py_ssize_t maxsplit)
/*[clinic end generated code: output=a55e0b5a03cb6190 input=60e9abf305128ff4]*/
{
- Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
- const char *s = PyByteArray_AS_STRING(self), *sub;
- PyObject *list;
- Py_buffer vsub;
+ PyObject *list = NULL;
+
+ /* Increase exports to prevent bytearray storage from changing during _Py_bytes_contains(). */
+ self->ob_exports++;
+ const char *sbuf = PyByteArray_AS_STRING(self);
+ Py_ssize_t slen = PyByteArray_GET_SIZE((PyObject *)self);
if (maxsplit < 0)
maxsplit = PY_SSIZE_T_MAX;
- if (sep == Py_None)
- return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
+ if (sep == Py_None) {
+ list = stringlib_rsplit_whitespace((PyObject*)self, sbuf, slen, maxsplit);
+ goto done;
+ }
- if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
- return NULL;
- sub = vsub.buf;
- n = vsub.len;
+ Py_buffer vsub;
+ if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) {
+ goto done;
+ }
- list = stringlib_rsplit(
- (PyObject*) self, s, len, sub, n, maxsplit
- );
+ list = stringlib_rsplit((PyObject*)self, sbuf, slen,
+ (const char *)vsub.buf, vsub.len, maxsplit);
PyBuffer_Release(&vsub);
+
+done:
+ self->ob_exports--;
return list;
}