diff --git a/Include/cpython/bytearrayobject.h b/Include/cpython/bytearrayobject.h index 9ba176eb2d3ac2..998d36695b5ba0 100644 --- a/Include/cpython/bytearrayobject.h +++ b/Include/cpython/bytearrayobject.h @@ -11,24 +11,12 @@ typedef struct { Py_ssize_t ob_exports; /* How many buffer exports */ } PyByteArrayObject; -PyAPI_DATA(char) _PyByteArray_empty_string[]; - /* Macros and static inline functions, trading safety for speed */ #define _PyByteArray_CAST(op) \ (assert(PyByteArray_Check(op)), _Py_CAST(PyByteArrayObject*, op)) -static inline char* PyByteArray_AS_STRING(PyObject *op) -{ - PyByteArrayObject *self = _PyByteArray_CAST(op); - if (Py_SIZE(self)) { - return self->ob_start; - } - return _PyByteArray_empty_string; -} +PyAPI_DATA(char*) PyByteArray_AS_STRING(PyObject *op); #define PyByteArray_AS_STRING(self) PyByteArray_AS_STRING(_PyObject_CAST(self)) -static inline Py_ssize_t PyByteArray_GET_SIZE(PyObject *op) { - PyByteArrayObject *self = _PyByteArray_CAST(op); - return Py_SIZE(self); -} +PyAPI_DATA(Py_ssize_t) PyByteArray_GET_SIZE(PyObject *op); #define PyByteArray_GET_SIZE(self) PyByteArray_GET_SIZE(_PyObject_CAST(self)) diff --git a/Include/cpython/bytesobject.h b/Include/cpython/bytesobject.h index e982031c107de2..f47dacbee4c1f2 100644 --- a/Include/cpython/bytesobject.h +++ b/Include/cpython/bytesobject.h @@ -28,20 +28,10 @@ PyAPI_FUNC(PyObject*) _PyBytes_FromHex( PyAPI_FUNC(PyObject *) _PyBytes_DecodeEscape(const char *, Py_ssize_t, const char *, const char **); -/* Macros and static inline functions, trading safety for speed */ -#define _PyBytes_CAST(op) \ - (assert(PyBytes_Check(op)), _Py_CAST(PyBytesObject*, op)) - -static inline char* PyBytes_AS_STRING(PyObject *op) -{ - return _PyBytes_CAST(op)->ob_sval; -} +PyAPI_FUNC(char*) PyBytes_AS_STRING(PyObject *op); #define PyBytes_AS_STRING(op) PyBytes_AS_STRING(_PyObject_CAST(op)) -static inline Py_ssize_t PyBytes_GET_SIZE(PyObject *op) { - PyBytesObject *self = _PyBytes_CAST(op); - return Py_SIZE(self); -} +PyAPI_FUNC(Py_ssize_t) PyBytes_GET_SIZE(PyObject *op); #define PyBytes_GET_SIZE(self) PyBytes_GET_SIZE(_PyObject_CAST(self)) /* _PyBytes_Join(sep, x) is like sep.join(x). sep must be PyBytesObject*, diff --git a/Misc/NEWS.d/next/C API/2022-09-22-17-43-33.gh-issue-97016.d5iveG.rst b/Misc/NEWS.d/next/C API/2022-09-22-17-43-33.gh-issue-97016.d5iveG.rst new file mode 100644 index 00000000000000..c12c8cc264e758 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2022-09-22-17-43-33.gh-issue-97016.d5iveG.rst @@ -0,0 +1,9 @@ +Convert the following static inline functions to regular functions: + +* :c:func:`PyByteArray_AS_STRING()` +* :c:func:`PyByteArray_GET_SIZE()` +* :c:func:`PyBytes_AS_STRING()` +* :c:func:`PyBytes_GET_SIZE()` + +Remove the ``_PyByteArray_empty_string`` variable. It was excluded from the +limited C API. Patch by Victor Stinner. diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index b2962fd137d93e..740af2b4a9bec9 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -15,8 +15,22 @@ class bytearray "PyByteArrayObject *" "&PyByteArray_Type" [clinic start generated code]*/ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/ -/* For PyByteArray_AS_STRING(). */ -char _PyByteArray_empty_string[] = ""; +/* For _PyByteArray_AS_STRING(). */ +static char _PyByteArray_empty_string[] = ""; + +static inline char* _PyByteArray_AS_STRING(PyByteArrayObject *self) +{ + if (Py_SIZE(self)) { + return self->ob_start; + } + return _PyByteArray_empty_string; +} +#define _PyByteArray_AS_STRING(self) _PyByteArray_AS_STRING(_PyByteArray_CAST(self)) + +static inline Py_ssize_t _PyByteArray_GET_SIZE(PyByteArrayObject *self) { + return Py_SIZE(self); +} +#define _PyByteArray_GET_SIZE(self) _PyByteArray_GET_SIZE(_PyByteArray_CAST(self)) /* Helpers */ @@ -50,7 +64,7 @@ bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags) "bytearray_getbuffer: view==NULL argument is obsolete"); return -1; } - ptr = (void *) PyByteArray_AS_STRING(obj); + ptr = (void *) _PyByteArray_AS_STRING(obj); /* cannot fail if view != NULL and readonly == 0 */ (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags); obj->ob_exports++; @@ -95,7 +109,7 @@ _PyByteArray_FromBufferObject(PyObject *obj) } result = PyByteArray_FromStringAndSize(NULL, view.len); if (result != NULL && - PyBuffer_ToContiguous(PyByteArray_AS_STRING(result), + PyBuffer_ToContiguous(_PyByteArray_AS_STRING(result), &view, view.len, 'C') < 0) { Py_CLEAR(result); @@ -154,7 +168,7 @@ PyByteArray_Size(PyObject *self) assert(self != NULL); assert(PyByteArray_Check(self)); - return PyByteArray_GET_SIZE(self); + return _PyByteArray_GET_SIZE(self); } char * @@ -163,7 +177,7 @@ PyByteArray_AsString(PyObject *self) assert(self != NULL); assert(PyByteArray_Check(self)); - return PyByteArray_AS_STRING(self); + return _PyByteArray_AS_STRING(self); } int @@ -199,7 +213,7 @@ PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size) else { /* Minor downsize; quick exit */ Py_SET_SIZE(self, size); - PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */ + _PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */ return 0; } } @@ -225,7 +239,7 @@ PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size) PyErr_NoMemory(); return -1; } - memcpy(sval, PyByteArray_AS_STRING(self), + memcpy(sval, _PyByteArray_AS_STRING(self), Py_MIN((size_t)requested_size, (size_t)Py_SIZE(self))); PyObject_Free(obj->ob_bytes); } @@ -311,7 +325,7 @@ bytearray_iconcat(PyByteArrayObject *self, PyObject *other) PyBuffer_Release(&vo); return NULL; } - memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len); + memcpy(_PyByteArray_AS_STRING(self) + size, vo.buf, vo.len); PyBuffer_Release(&vo); Py_INCREF(self); return (PyObject *)self; @@ -327,7 +341,7 @@ bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count) return PyErr_NoMemory(); Py_ssize_t size = mysize * count; PyByteArrayObject* result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size); - const char* buf = PyByteArray_AS_STRING(self); + const char* buf = _PyByteArray_AS_STRING(self); if (result != NULL && size != 0) { _PyBytes_Repeat(result->ob_bytes, size, buf, mysize); } @@ -351,7 +365,7 @@ bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count) if (PyByteArray_Resize((PyObject *)self, size) < 0) return NULL; - char* buf = PyByteArray_AS_STRING(self); + char* buf = _PyByteArray_AS_STRING(self); _PyBytes_Repeat(buf, size, buf, mysize); Py_INCREF(self); @@ -378,7 +392,7 @@ bytearray_subscript(PyByteArrayObject *self, PyObject *index) return NULL; if (i < 0) - i += PyByteArray_GET_SIZE(self); + i += _PyByteArray_GET_SIZE(self); if (i < 0 || i >= Py_SIZE(self)) { PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); @@ -392,17 +406,17 @@ bytearray_subscript(PyByteArrayObject *self, PyObject *index) if (PySlice_Unpack(index, &start, &stop, &step) < 0) { return NULL; } - slicelength = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self), + slicelength = PySlice_AdjustIndices(_PyByteArray_GET_SIZE(self), &start, &stop, step); if (slicelength <= 0) return PyByteArray_FromStringAndSize("", 0); else if (step == 1) { return PyByteArray_FromStringAndSize( - PyByteArray_AS_STRING(self) + start, slicelength); + _PyByteArray_AS_STRING(self) + start, slicelength); } else { - char *source_buf = PyByteArray_AS_STRING(self); + char *source_buf = _PyByteArray_AS_STRING(self); char *result_buf; PyObject *result; @@ -410,7 +424,7 @@ bytearray_subscript(PyByteArrayObject *self, PyObject *index) if (result == NULL) return NULL; - result_buf = PyByteArray_AS_STRING(result); + result_buf = _PyByteArray_AS_STRING(result); for (cur = start, i = 0; i < slicelength; cur += step, i++) { result_buf[i] = source_buf[cur]; @@ -432,7 +446,7 @@ bytearray_setslice_linear(PyByteArrayObject *self, char *bytes, Py_ssize_t bytes_len) { Py_ssize_t avail = hi - lo; - char *buf = PyByteArray_AS_STRING(self); + char *buf = _PyByteArray_AS_STRING(self); Py_ssize_t growth = bytes_len - avail; int res = 0; assert(avail >= 0); @@ -481,7 +495,7 @@ bytearray_setslice_linear(PyByteArrayObject *self, Py_SET_SIZE(self, Py_SIZE(self) + growth); res = -1; } - buf = PyByteArray_AS_STRING(self); + buf = _PyByteArray_AS_STRING(self); } else if (growth > 0) { if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) { @@ -493,7 +507,7 @@ bytearray_setslice_linear(PyByteArrayObject *self, Py_SIZE(self) + growth) < 0) { return -1; } - buf = PyByteArray_AS_STRING(self); + buf = _PyByteArray_AS_STRING(self); /* Make the place for the additional bytes */ /* 0 lo hi old_size @@ -523,8 +537,8 @@ bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi, if (values == (PyObject *)self) { /* Make a copy and call this function recursively */ int err; - values = PyByteArray_FromStringAndSize(PyByteArray_AS_STRING(values), - PyByteArray_GET_SIZE(values)); + values = PyByteArray_FromStringAndSize(_PyByteArray_AS_STRING(values), + _PyByteArray_GET_SIZE(values)); if (values == NULL) return -1; err = bytearray_setslice(self, lo, hi, values); @@ -585,7 +599,7 @@ bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value) } assert(0 <= ival && ival < 256); - PyByteArray_AS_STRING(self)[i] = ival; + _PyByteArray_AS_STRING(self)[i] = ival; return 0; } @@ -594,7 +608,7 @@ bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu { Py_ssize_t start, stop, step, slicelen, needed; char *buf, *bytes; - buf = PyByteArray_AS_STRING(self); + buf = _PyByteArray_AS_STRING(self); if (_PyIndex_Check(index)) { Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError); @@ -612,7 +626,7 @@ bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu } if (i < 0) { - i += PyByteArray_GET_SIZE(self); + i += _PyByteArray_GET_SIZE(self); } if (i < 0 || i >= Py_SIZE(self)) { @@ -637,7 +651,7 @@ bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu if (PySlice_Unpack(index, &start, &stop, &step) < 0) { return -1; } - slicelen = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self), &start, + slicelen = PySlice_AdjustIndices(_PyByteArray_GET_SIZE(self), &start, &stop, step); } else { @@ -669,7 +683,7 @@ bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu } else { assert(PyByteArray_Check(values)); - bytes = PyByteArray_AS_STRING(values); + bytes = _PyByteArray_AS_STRING(values); needed = Py_SIZE(values); } /* Make sure b[5:2] = ... inserts before 5, not before 2. */ @@ -701,21 +715,21 @@ bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu i < slicelen; cur += step, i++) { Py_ssize_t lim = step - 1; - if (cur + step >= (size_t)PyByteArray_GET_SIZE(self)) - lim = PyByteArray_GET_SIZE(self) - cur - 1; + if (cur + step >= (size_t)_PyByteArray_GET_SIZE(self)) + lim = _PyByteArray_GET_SIZE(self) - cur - 1; memmove(buf + cur - i, buf + cur + 1, lim); } /* Move the tail of the bytes, in one chunk */ cur = start + (size_t)slicelen*step; - if (cur < (size_t)PyByteArray_GET_SIZE(self)) { + if (cur < (size_t)_PyByteArray_GET_SIZE(self)) { memmove(buf + cur - slicelen, buf + cur, - PyByteArray_GET_SIZE(self) - cur); + _PyByteArray_GET_SIZE(self) - cur); } if (PyByteArray_Resize((PyObject *)self, - PyByteArray_GET_SIZE(self) - slicelen) < 0) + _PyByteArray_GET_SIZE(self) - slicelen) < 0) return -1; return 0; @@ -820,7 +834,7 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg, if (count > 0) { if (PyByteArray_Resize((PyObject *)self, count)) return -1; - memset(PyByteArray_AS_STRING(self), 0, count); + memset(_PyByteArray_AS_STRING(self), 0, count); } return 0; } @@ -834,7 +848,7 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg, return -1; size = view.len; if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail; - if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self), + if (PyBuffer_ToContiguous(_PyByteArray_AS_STRING(self), &view, size, 'C') < 0) goto fail; PyBuffer_Release(&view); @@ -850,7 +864,7 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg, return -1; } PyObject **items = PySequence_Fast_ITEMS(arg); - char *s = PyByteArray_AS_STRING(self); + char *s = _PyByteArray_AS_STRING(self); for (Py_ssize_t i = 0; i < size; i++) { int value; if (!PyLong_CheckExact(items[i])) { @@ -908,11 +922,11 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg, /* Append the byte */ if (Py_SIZE(self) + 1 < self->ob_alloc) { Py_SET_SIZE(self, Py_SIZE(self) + 1); - PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0'; + _PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0'; } else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0) goto error; - PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value; + _PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value; } /* Clean up and return success */ @@ -961,7 +975,7 @@ bytearray_repr(PyByteArrayObject *self) /* Figure out which quote to use; single is preferred */ quote = '\''; - start = PyByteArray_AS_STRING(self); + start = _PyByteArray_AS_STRING(self); for (test = start; test < start+length; ++test) { if (*test == '"') { quote = '\''; /* back to single */ @@ -978,7 +992,7 @@ bytearray_repr(PyByteArrayObject *self) *p++ = *quote_prefix++; *p++ = quote; - bytes = PyByteArray_AS_STRING(self); + bytes = _PyByteArray_AS_STRING(self); for (i = 0; i < length; i++) { /* There's at least enough room for a hex escape and a closing quote. */ @@ -1104,8 +1118,8 @@ bytearray_dealloc(PyByteArrayObject *self) #define STRINGLIB(F) stringlib_##F #define STRINGLIB_CHAR char #define STRINGLIB_SIZEOF_CHAR 1 -#define STRINGLIB_LEN PyByteArray_GET_SIZE -#define STRINGLIB_STR PyByteArray_AS_STRING +#define STRINGLIB_LEN _PyByteArray_GET_SIZE +#define STRINGLIB_STR _PyByteArray_AS_STRING #define STRINGLIB_NEW PyByteArray_FromStringAndSize #define STRINGLIB_ISSPACE Py_ISSPACE #define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r')) @@ -1126,13 +1140,13 @@ bytearray_dealloc(PyByteArrayObject *self) static PyObject * bytearray_find(PyByteArrayObject *self, PyObject *args) { - return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); + return _Py_bytes_find(_PyByteArray_AS_STRING(self), _PyByteArray_GET_SIZE(self), args); } static PyObject * bytearray_count(PyByteArrayObject *self, PyObject *args) { - return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); + return _Py_bytes_count(_PyByteArray_AS_STRING(self), _PyByteArray_GET_SIZE(self), args); } /*[clinic input] @@ -1160,44 +1174,44 @@ static PyObject * bytearray_copy_impl(PyByteArrayObject *self) /*[clinic end generated code: output=68cfbcfed484c132 input=6597b0c01bccaa9e]*/ { - return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self), - PyByteArray_GET_SIZE(self)); + return PyByteArray_FromStringAndSize(_PyByteArray_AS_STRING((PyObject *)self), + _PyByteArray_GET_SIZE(self)); } static PyObject * bytearray_index(PyByteArrayObject *self, PyObject *args) { - return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); + return _Py_bytes_index(_PyByteArray_AS_STRING(self), _PyByteArray_GET_SIZE(self), args); } static PyObject * bytearray_rfind(PyByteArrayObject *self, PyObject *args) { - return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); + return _Py_bytes_rfind(_PyByteArray_AS_STRING(self), _PyByteArray_GET_SIZE(self), args); } static PyObject * bytearray_rindex(PyByteArrayObject *self, PyObject *args) { - return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); + return _Py_bytes_rindex(_PyByteArray_AS_STRING(self), _PyByteArray_GET_SIZE(self), args); } static int bytearray_contains(PyObject *self, PyObject *arg) { - return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg); + return _Py_bytes_contains(_PyByteArray_AS_STRING(self), _PyByteArray_GET_SIZE(self), arg); } static PyObject * bytearray_startswith(PyByteArrayObject *self, PyObject *args) { - return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); + return _Py_bytes_startswith(_PyByteArray_AS_STRING(self), _PyByteArray_GET_SIZE(self), args); } static PyObject * bytearray_endswith(PyByteArrayObject *self, PyObject *args) { - return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); + return _Py_bytes_endswith(_PyByteArray_AS_STRING(self), _PyByteArray_GET_SIZE(self), args); } /*[clinic input] @@ -1217,8 +1231,8 @@ static PyObject * bytearray_removeprefix_impl(PyByteArrayObject *self, Py_buffer *prefix) /*[clinic end generated code: output=6cabc585e7f502e0 input=968aada38aedd262]*/ { - const char *self_start = PyByteArray_AS_STRING(self); - Py_ssize_t self_len = PyByteArray_GET_SIZE(self); + const char *self_start = _PyByteArray_AS_STRING(self); + Py_ssize_t self_len = _PyByteArray_GET_SIZE(self); const char *prefix_start = prefix->buf; Py_ssize_t prefix_len = prefix->len; @@ -1249,8 +1263,8 @@ static PyObject * bytearray_removesuffix_impl(PyByteArrayObject *self, Py_buffer *suffix) /*[clinic end generated code: output=2bc8cfb79de793d3 input=c1827e810b2f6b99]*/ { - const char *self_start = PyByteArray_AS_STRING(self); - Py_ssize_t self_len = PyByteArray_GET_SIZE(self); + const char *self_start = _PyByteArray_AS_STRING(self); + Py_ssize_t self_len = _PyByteArray_GET_SIZE(self); const char *suffix_start = suffix->buf; Py_ssize_t suffix_len = suffix->len; @@ -1322,12 +1336,12 @@ bytearray_translate_impl(PyByteArrayObject *self, PyObject *table, vdel.len = 0; } - inlen = PyByteArray_GET_SIZE(input_obj); + inlen = _PyByteArray_GET_SIZE(input_obj); result = PyByteArray_FromStringAndSize((char *)NULL, inlen); if (result == NULL) goto done; - output_start = output = PyByteArray_AS_STRING(result); - input = PyByteArray_AS_STRING(input_obj); + output_start = output = _PyByteArray_AS_STRING(result); + input = _PyByteArray_AS_STRING(input_obj); if (vdel.len == 0 && table_chars != NULL) { /* If no deletions are required, use faster code */ @@ -1440,8 +1454,8 @@ bytearray_split_impl(PyByteArrayObject *self, PyObject *sep, Py_ssize_t maxsplit) /*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/ { - Py_ssize_t len = PyByteArray_GET_SIZE(self), n; - const char *s = PyByteArray_AS_STRING(self), *sub; + Py_ssize_t len = _PyByteArray_GET_SIZE(self), n; + const char *s = _PyByteArray_AS_STRING(self), *sub; PyObject *list; Py_buffer vsub; @@ -1491,9 +1505,9 @@ bytearray_partition(PyByteArrayObject *self, PyObject *sep) result = stringlib_partition( (PyObject*) self, - PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), + _PyByteArray_AS_STRING(self), _PyByteArray_GET_SIZE(self), bytesep, - PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) + _PyByteArray_AS_STRING(bytesep), _PyByteArray_GET_SIZE(bytesep) ); Py_DECREF(bytesep); @@ -1529,9 +1543,9 @@ bytearray_rpartition(PyByteArrayObject *self, PyObject *sep) result = stringlib_rpartition( (PyObject*) self, - PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), + _PyByteArray_AS_STRING(self), _PyByteArray_GET_SIZE(self), bytesep, - PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) + _PyByteArray_AS_STRING(bytesep), _PyByteArray_GET_SIZE(bytesep) ); Py_DECREF(bytesep); @@ -1551,8 +1565,8 @@ bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep, Py_ssize_t maxsplit) /*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/ { - Py_ssize_t len = PyByteArray_GET_SIZE(self), n; - const char *s = PyByteArray_AS_STRING(self), *sub; + Py_ssize_t len = _PyByteArray_GET_SIZE(self), n; + const char *s = _PyByteArray_AS_STRING(self), *sub; PyObject *list; Py_buffer vsub; @@ -1588,7 +1602,7 @@ bytearray_reverse_impl(PyByteArrayObject *self) Py_ssize_t i, j, n = Py_SIZE(self); j = n / 2; - head = PyByteArray_AS_STRING(self); + head = _PyByteArray_AS_STRING(self); tail = head + n - 1; for (i = 0; i < j; i++) { swap = *head; @@ -1634,7 +1648,7 @@ bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item) } if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) return NULL; - buf = PyByteArray_AS_STRING(self); + buf = _PyByteArray_AS_STRING(self); if (index < 0) { index += n; @@ -1673,7 +1687,7 @@ bytearray_append_impl(PyByteArrayObject *self, int item) if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) return NULL; - PyByteArray_AS_STRING(self)[n] = item; + _PyByteArray_AS_STRING(self)[n] = item; Py_RETURN_NONE; } @@ -1727,7 +1741,7 @@ bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints) Py_DECREF(it); return NULL; } - buf = PyByteArray_AS_STRING(bytearray_obj); + buf = _PyByteArray_AS_STRING(bytearray_obj); while ((item = PyIter_Next(it)) != NULL) { if (! _getbytevalue(item, &value)) { @@ -1758,7 +1772,7 @@ bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints) } /* Recompute the `buf' pointer, since the resizing operation may have invalidated it. */ - buf = PyByteArray_AS_STRING(bytearray_obj); + buf = _PyByteArray_AS_STRING(bytearray_obj); } } Py_DECREF(it); @@ -1819,7 +1833,7 @@ bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index) if (!_canresize(self)) return NULL; - buf = PyByteArray_AS_STRING(self); + buf = _PyByteArray_AS_STRING(self); value = buf[index]; memmove(buf + index, buf + index + 1, n - index); if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) @@ -1843,7 +1857,7 @@ bytearray_remove_impl(PyByteArrayObject *self, int value) /*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/ { Py_ssize_t where, n = Py_SIZE(self); - char *buf = PyByteArray_AS_STRING(self); + char *buf = _PyByteArray_AS_STRING(self); where = stringlib_find_char(buf, n, value); if (where < 0) { @@ -1882,7 +1896,7 @@ bytearray_strip_impl_helper(PyByteArrayObject* self, PyObject* bytes, int stript bytesptr = (const char*)vbytes.buf; byteslen = vbytes.len; } - myptr = PyByteArray_AS_STRING(self); + myptr = _PyByteArray_AS_STRING(self); mysize = Py_SIZE(self); Py_ssize_t left = 0; @@ -2028,8 +2042,8 @@ bytearray_splitlines_impl(PyByteArrayObject *self, int keepends) /*[clinic end generated code: output=4223c94b895f6ad9 input=99a27ad959b9cf6b]*/ { return stringlib_splitlines( - (PyObject*) self, PyByteArray_AS_STRING(self), - PyByteArray_GET_SIZE(self), keepends + (PyObject*) self, _PyByteArray_AS_STRING(self), + _PyByteArray_GET_SIZE(self), keepends ); } @@ -2084,8 +2098,8 @@ static PyObject * bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep) /*[clinic end generated code: output=29c4e5ef72c565a0 input=808667e49bcccb54]*/ { - char* argbuf = PyByteArray_AS_STRING(self); - Py_ssize_t arglen = PyByteArray_GET_SIZE(self); + char* argbuf = _PyByteArray_AS_STRING(self); + Py_ssize_t arglen = _PyByteArray_GET_SIZE(self); return _Py_strhex_with_sep(argbuf, arglen, sep, bytes_per_sep); } @@ -2103,7 +2117,7 @@ _common_reduce(PyByteArrayObject *self, int proto) if (!Py_SIZE(self)) { return Py_BuildValue("(O()N)", Py_TYPE(self), state); } - buf = PyByteArray_AS_STRING(self); + buf = _PyByteArray_AS_STRING(self); if (proto < 3) { /* use str based reduction for backwards compatibility with Python 2.x */ PyObject *latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL); @@ -2262,7 +2276,7 @@ bytearray_mod(PyObject *v, PyObject *w) { if (!PyByteArray_Check(v)) Py_RETURN_NOTIMPLEMENTED; - return _PyBytes_FormatEx(PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1); + return _PyBytes_FormatEx(_PyByteArray_AS_STRING(v), _PyByteArray_GET_SIZE(v), w, 1); } static PyNumberMethods bytearray_as_number = { @@ -2366,9 +2380,9 @@ bytearrayiter_next(bytesiterobject *it) return NULL; assert(PyByteArray_Check(seq)); - if (it->it_index < PyByteArray_GET_SIZE(seq)) { + if (it->it_index < _PyByteArray_GET_SIZE(seq)) { return _PyLong_FromUnsignedChar( - (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index++]); + (unsigned char)_PyByteArray_AS_STRING(seq)[it->it_index++]); } it->it_seq = NULL; @@ -2381,7 +2395,7 @@ bytearrayiter_length_hint(bytesiterobject *it, PyObject *Py_UNUSED(ignored)) { Py_ssize_t len = 0; if (it->it_seq) { - len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index; + len = _PyByteArray_GET_SIZE(it->it_seq) - it->it_index; if (len < 0) { len = 0; } @@ -2412,8 +2426,8 @@ bytearrayiter_setstate(bytesiterobject *it, PyObject *state) if (it->it_seq != NULL) { if (index < 0) index = 0; - else if (index > PyByteArray_GET_SIZE(it->it_seq)) - index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */ + else if (index > _PyByteArray_GET_SIZE(it->it_seq)) + index = _PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */ it->it_index = index; } Py_RETURN_NONE; @@ -2482,3 +2496,17 @@ bytearray_iter(PyObject *seq) _PyObject_GC_TRACK(it); return (PyObject *)it; } + +// --- Public C API functions ------------------------------------------------ + +#undef PyByteArray_AS_STRING +char* PyByteArray_AS_STRING(PyObject *op) +{ + return _PyByteArray_AS_STRING(op); +} + +#undef PyByteArray_GET_SIZE +Py_ssize_t PyByteArray_GET_SIZE(PyObject *op) +{ + return _PyByteArray_GET_SIZE(op); +} diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 80660881920fb7..354f5f33ebc0b5 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -24,6 +24,19 @@ class bytes "PyBytesObject *" "&PyBytes_Type" #include "clinic/bytesobject.c.h" +#define _PyBytes_CAST(op) \ + (assert(PyBytes_Check(op)), _Py_CAST(PyBytesObject*, op)) + +static inline char* _PyBytes_AS_STRING(PyBytesObject *self) { + return self->ob_sval; +} +#define _PyBytes_AS_STRING(op) _PyBytes_AS_STRING(_PyBytes_CAST(op)) + +static inline Py_ssize_t _PyBytes_GET_SIZE(PyBytesObject *self) { + return Py_SIZE(self); +} +#define _PyBytes_GET_SIZE(op) _PyBytes_GET_SIZE(_PyBytes_CAST(op)) + /* PyBytesObject_SIZE gives the basic size of a bytes object; any memory allocation for a bytes object of length n should request PyBytesObject_SIZE + n bytes. @@ -485,8 +498,8 @@ formatlong(PyObject *v, int flags, int prec, int type) static int byte_converter(PyObject *arg, char *p) { - if (PyBytes_Check(arg) && PyBytes_GET_SIZE(arg) == 1) { - *p = PyBytes_AS_STRING(arg)[0]; + if (PyBytes_Check(arg) && _PyBytes_GET_SIZE(arg) == 1) { + *p = _PyBytes_AS_STRING(arg)[0]; return 1; } else if (PyByteArray_Check(arg) && PyByteArray_GET_SIZE(arg) == 1) { @@ -525,8 +538,8 @@ format_obj(PyObject *v, const char **pbuf, Py_ssize_t *plen) PyObject *func, *result; /* is it a bytes object? */ if (PyBytes_Check(v)) { - *pbuf = PyBytes_AS_STRING(v); - *plen = PyBytes_GET_SIZE(v); + *pbuf = _PyBytes_AS_STRING(v); + *plen = _PyBytes_GET_SIZE(v); Py_INCREF(v); return v; } @@ -550,8 +563,8 @@ format_obj(PyObject *v, const char **pbuf, Py_ssize_t *plen) Py_DECREF(result); return NULL; } - *pbuf = PyBytes_AS_STRING(result); - *plen = PyBytes_GET_SIZE(result); + *pbuf = _PyBytes_AS_STRING(result); + *plen = _PyBytes_GET_SIZE(result); return result; } /* does it support buffer protocol? */ @@ -560,8 +573,8 @@ format_obj(PyObject *v, const char **pbuf, Py_ssize_t *plen) result = _PyBytes_FromBuffer(v); if (result == NULL) return NULL; - *pbuf = PyBytes_AS_STRING(result); - *plen = PyBytes_GET_SIZE(result); + *pbuf = _PyBytes_AS_STRING(result); + *plen = _PyBytes_GET_SIZE(result); return result; } PyErr_Format(PyExc_TypeError, @@ -906,8 +919,8 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len, if (!formatfloat(v, flags, prec, c, &temp, NULL, res)) goto error; - pbuf = PyBytes_AS_STRING(temp); - len = PyBytes_GET_SIZE(temp); + pbuf = _PyBytes_AS_STRING(temp); + len = _PyBytes_GET_SIZE(temp); sign = 1; if (flags & F_ZERO) fill = '0'; @@ -1245,10 +1258,10 @@ PyBytes_AsStringAndSize(PyObject *obj, return -1; } - *s = PyBytes_AS_STRING(obj); + *s = _PyBytes_AS_STRING(obj); if (len != NULL) - *len = PyBytes_GET_SIZE(obj); - else if (strlen(*s) != (size_t)PyBytes_GET_SIZE(obj)) { + *len = _PyBytes_GET_SIZE(obj); + else if (strlen(*s) != (size_t)_PyBytes_GET_SIZE(obj)) { PyErr_SetString(PyExc_ValueError, "embedded null byte"); return -1; @@ -1428,8 +1441,8 @@ bytes_concat(PyObject *a, PyObject *b) result = PyBytes_FromStringAndSize(NULL, va.len + vb.len); if (result != NULL) { - memcpy(PyBytes_AS_STRING(result), va.buf, va.len); - memcpy(PyBytes_AS_STRING(result) + va.len, vb.buf, vb.len); + memcpy(_PyBytes_AS_STRING(result), va.buf, va.len); + memcpy(_PyBytes_AS_STRING(result) + va.len, vb.buf, vb.len); } done: @@ -1486,7 +1499,7 @@ _Py_COMP_DIAG_POP static int bytes_contains(PyObject *self, PyObject *arg) { - return _Py_bytes_contains(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), arg); + return _Py_bytes_contains(_PyBytes_AS_STRING(self), _PyBytes_GET_SIZE(self), arg); } static PyObject * @@ -1598,8 +1611,8 @@ bytes_subscript(PyBytesObject* self, PyObject* item) if (i == -1 && PyErr_Occurred()) return NULL; if (i < 0) - i += PyBytes_GET_SIZE(self); - if (i < 0 || i >= PyBytes_GET_SIZE(self)) { + i += _PyBytes_GET_SIZE(self); + if (i < 0 || i >= _PyBytes_GET_SIZE(self)) { PyErr_SetString(PyExc_IndexError, "index out of range"); return NULL; @@ -1616,30 +1629,30 @@ bytes_subscript(PyBytesObject* self, PyObject* item) if (PySlice_Unpack(item, &start, &stop, &step) < 0) { return NULL; } - slicelength = PySlice_AdjustIndices(PyBytes_GET_SIZE(self), &start, + slicelength = PySlice_AdjustIndices(_PyBytes_GET_SIZE(self), &start, &stop, step); if (slicelength <= 0) { return PyBytes_FromStringAndSize("", 0); } else if (start == 0 && step == 1 && - slicelength == PyBytes_GET_SIZE(self) && + slicelength == _PyBytes_GET_SIZE(self) && PyBytes_CheckExact(self)) { Py_INCREF(self); return (PyObject *)self; } else if (step == 1) { return PyBytes_FromStringAndSize( - PyBytes_AS_STRING(self) + start, + _PyBytes_AS_STRING(self) + start, slicelength); } else { - source_buf = PyBytes_AS_STRING(self); + source_buf = _PyBytes_AS_STRING(self); result = PyBytes_FromStringAndSize(NULL, slicelength); if (result == NULL) return NULL; - result_buf = PyBytes_AS_STRING(result); + result_buf = _PyBytes_AS_STRING(result); for (cur = start, i = 0; i < slicelength; cur += step, i++) { result_buf[i] = source_buf[cur]; @@ -1727,8 +1740,8 @@ static PyObject * bytes_split_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit) /*[clinic end generated code: output=52126b5844c1d8ef input=8b809b39074abbfa]*/ { - Py_ssize_t len = PyBytes_GET_SIZE(self), n; - const char *s = PyBytes_AS_STRING(self), *sub; + Py_ssize_t len = _PyBytes_GET_SIZE(self), n; + const char *s = _PyBytes_AS_STRING(self), *sub; Py_buffer vsub; PyObject *list; @@ -1768,7 +1781,7 @@ bytes_partition_impl(PyBytesObject *self, Py_buffer *sep) { return stringlib_partition( (PyObject*) self, - PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), + _PyBytes_AS_STRING(self), _PyBytes_GET_SIZE(self), sep->obj, (const char *)sep->buf, sep->len ); } @@ -1795,7 +1808,7 @@ bytes_rpartition_impl(PyBytesObject *self, Py_buffer *sep) { return stringlib_rpartition( (PyObject*) self, - PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), + _PyBytes_AS_STRING(self), _PyBytes_GET_SIZE(self), sep->obj, (const char *)sep->buf, sep->len ); } @@ -1812,8 +1825,8 @@ static PyObject * bytes_rsplit_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit) /*[clinic end generated code: output=ba698d9ea01e1c8f input=0f86c9f28f7d7b7b]*/ { - Py_ssize_t len = PyBytes_GET_SIZE(self), n; - const char *s = PyBytes_AS_STRING(self), *sub; + Py_ssize_t len = _PyBytes_GET_SIZE(self), n; + const char *s = _PyBytes_AS_STRING(self), *sub; Py_buffer vsub; PyObject *list; @@ -1865,27 +1878,27 @@ _PyBytes_Join(PyObject *sep, PyObject *x) static PyObject * bytes_find(PyBytesObject *self, PyObject *args) { - return _Py_bytes_find(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args); + return _Py_bytes_find(_PyBytes_AS_STRING(self), _PyBytes_GET_SIZE(self), args); } static PyObject * bytes_index(PyBytesObject *self, PyObject *args) { - return _Py_bytes_index(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args); + return _Py_bytes_index(_PyBytes_AS_STRING(self), _PyBytes_GET_SIZE(self), args); } static PyObject * bytes_rfind(PyBytesObject *self, PyObject *args) { - return _Py_bytes_rfind(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args); + return _Py_bytes_rfind(_PyBytes_AS_STRING(self), _PyBytes_GET_SIZE(self), args); } static PyObject * bytes_rindex(PyBytesObject *self, PyObject *args) { - return _Py_bytes_rindex(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args); + return _Py_bytes_rindex(_PyBytes_AS_STRING(self), _PyBytes_GET_SIZE(self), args); } @@ -1893,8 +1906,8 @@ Py_LOCAL_INLINE(PyObject *) do_xstrip(PyBytesObject *self, int striptype, PyObject *sepobj) { Py_buffer vsep; - const char *s = PyBytes_AS_STRING(self); - Py_ssize_t len = PyBytes_GET_SIZE(self); + const char *s = _PyBytes_AS_STRING(self); + Py_ssize_t len = _PyBytes_GET_SIZE(self); char *sep; Py_ssize_t seplen; Py_ssize_t i, j; @@ -1933,8 +1946,8 @@ do_xstrip(PyBytesObject *self, int striptype, PyObject *sepobj) Py_LOCAL_INLINE(PyObject *) do_strip(PyBytesObject *self, int striptype) { - const char *s = PyBytes_AS_STRING(self); - Py_ssize_t len = PyBytes_GET_SIZE(self), i, j; + const char *s = _PyBytes_AS_STRING(self); + Py_ssize_t len = _PyBytes_GET_SIZE(self), i, j; i = 0; if (striptype != RIGHTSTRIP) { @@ -2027,7 +2040,7 @@ bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes) static PyObject * bytes_count(PyBytesObject *self, PyObject *args) { - return _Py_bytes_count(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args); + return _Py_bytes_count(_PyBytes_AS_STRING(self), _PyBytes_GET_SIZE(self), args); } @@ -2063,8 +2076,8 @@ bytes_translate_impl(PyBytesObject *self, PyObject *table, int trans_table[256]; if (PyBytes_Check(table)) { - table_chars = PyBytes_AS_STRING(table); - tablen = PyBytes_GET_SIZE(table); + table_chars = _PyBytes_AS_STRING(table); + tablen = _PyBytes_GET_SIZE(table); } else if (table == Py_None) { table_chars = NULL; @@ -2086,8 +2099,8 @@ bytes_translate_impl(PyBytesObject *self, PyObject *table, if (deletechars != NULL) { if (PyBytes_Check(deletechars)) { - del_table_chars = PyBytes_AS_STRING(deletechars); - dellen = PyBytes_GET_SIZE(deletechars); + del_table_chars = _PyBytes_AS_STRING(deletechars); + dellen = _PyBytes_GET_SIZE(deletechars); } else { if (PyObject_GetBuffer(deletechars, &del_table_view, PyBUF_SIMPLE) != 0) { @@ -2103,15 +2116,15 @@ bytes_translate_impl(PyBytesObject *self, PyObject *table, dellen = 0; } - inlen = PyBytes_GET_SIZE(input_obj); + inlen = _PyBytes_GET_SIZE(input_obj); result = PyBytes_FromStringAndSize((char *)NULL, inlen); if (result == NULL) { PyBuffer_Release(&del_table_view); PyBuffer_Release(&table_view); return NULL; } - output_start = output = PyBytes_AS_STRING(result); - input = PyBytes_AS_STRING(input_obj); + output_start = output = _PyBytes_AS_STRING(result); + input = _PyBytes_AS_STRING(input_obj); if (dellen == 0 && table_chars != NULL) { /* If no deletions are required, use faster code */ @@ -2231,8 +2244,8 @@ static PyObject * bytes_removeprefix_impl(PyBytesObject *self, Py_buffer *prefix) /*[clinic end generated code: output=f006865331a06ab6 input=0c93bac817a8502c]*/ { - const char *self_start = PyBytes_AS_STRING(self); - Py_ssize_t self_len = PyBytes_GET_SIZE(self); + const char *self_start = _PyBytes_AS_STRING(self); + Py_ssize_t self_len = _PyBytes_GET_SIZE(self); const char *prefix_start = prefix->buf; Py_ssize_t prefix_len = prefix->len; @@ -2269,8 +2282,8 @@ static PyObject * bytes_removesuffix_impl(PyBytesObject *self, Py_buffer *suffix) /*[clinic end generated code: output=d887d308e3242eeb input=9f4e1da8c637bbf1]*/ { - const char *self_start = PyBytes_AS_STRING(self); - Py_ssize_t self_len = PyBytes_GET_SIZE(self); + const char *self_start = _PyBytes_AS_STRING(self); + Py_ssize_t self_len = _PyBytes_GET_SIZE(self); const char *suffix_start = suffix->buf; Py_ssize_t suffix_len = suffix->len; @@ -2294,13 +2307,13 @@ bytes_removesuffix_impl(PyBytesObject *self, Py_buffer *suffix) static PyObject * bytes_startswith(PyBytesObject *self, PyObject *args) { - return _Py_bytes_startswith(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args); + return _Py_bytes_startswith(_PyBytes_AS_STRING(self), _PyBytes_GET_SIZE(self), args); } static PyObject * bytes_endswith(PyBytesObject *self, PyObject *args) { - return _Py_bytes_endswith(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args); + return _Py_bytes_endswith(_PyBytes_AS_STRING(self), _PyBytes_GET_SIZE(self), args); } @@ -2344,8 +2357,8 @@ bytes_splitlines_impl(PyBytesObject *self, int keepends) /*[clinic end generated code: output=3484149a5d880ffb input=a8b32eb01ff5a5ed]*/ { return stringlib_splitlines( - (PyObject*) self, PyBytes_AS_STRING(self), - PyBytes_GET_SIZE(self), keepends + (PyObject*) self, _PyBytes_AS_STRING(self), + _PyBytes_GET_SIZE(self), keepends ); } @@ -2477,8 +2490,8 @@ static PyObject * bytes_hex_impl(PyBytesObject *self, PyObject *sep, int bytes_per_sep) /*[clinic end generated code: output=1f134da504064139 input=1a21282b1f1ae595]*/ { - const char *argbuf = PyBytes_AS_STRING(self); - Py_ssize_t arglen = PyBytes_GET_SIZE(self); + const char *argbuf = _PyBytes_AS_STRING(self); + Py_ssize_t arglen = _PyBytes_GET_SIZE(self); return _Py_strhex_with_sep(argbuf, arglen, sep, bytes_per_sep); } @@ -2558,7 +2571,7 @@ bytes_mod(PyObject *self, PyObject *arg) if (!PyBytes_Check(self)) { Py_RETURN_NOTIMPLEMENTED; } - return _PyBytes_FormatEx(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), + return _PyBytes_FormatEx(_PyBytes_AS_STRING(self), _PyBytes_GET_SIZE(self), arg, 0); } @@ -2903,11 +2916,11 @@ bytes_subtype_new(PyTypeObject *type, PyObject *tmp) assert(PyType_IsSubtype(type, &PyBytes_Type)); assert(PyBytes_Check(tmp)); - n = PyBytes_GET_SIZE(tmp); + n = _PyBytes_GET_SIZE(tmp); pnew = type->tp_alloc(type, n); if (pnew != NULL) { - memcpy(PyBytes_AS_STRING(pnew), - PyBytes_AS_STRING(tmp), n+1); + memcpy(_PyBytes_AS_STRING(pnew), + _PyBytes_AS_STRING(tmp), n+1); _Py_COMP_DIAG_PUSH _Py_COMP_DIAG_IGNORE_DEPR_DECLS ((PyBytesObject *)pnew)->ob_shash = @@ -2999,7 +3012,7 @@ PyBytes_Concat(PyObject **pv, PyObject *w) return; } - oldsize = PyBytes_GET_SIZE(*pv); + oldsize = _PyBytes_GET_SIZE(*pv); if (oldsize > PY_SSIZE_T_MAX - wb.len) { PyErr_NoMemory(); goto error; @@ -3007,7 +3020,7 @@ PyBytes_Concat(PyObject **pv, PyObject *w) if (_PyBytes_Resize(pv, oldsize + wb.len) < 0) goto error; - memcpy(PyBytes_AS_STRING(*pv) + oldsize, wb.buf, wb.len); + memcpy(_PyBytes_AS_STRING(*pv) + oldsize, wb.buf, wb.len); PyBuffer_Release(&wb); return; @@ -3160,7 +3173,7 @@ striter_next(striterobject *it) return NULL; assert(PyBytes_Check(seq)); - if (it->it_index < PyBytes_GET_SIZE(seq)) { + if (it->it_index < _PyBytes_GET_SIZE(seq)) { return _PyLong_FromUnsignedChar( (unsigned char)seq->ob_sval[it->it_index++]); } @@ -3175,7 +3188,7 @@ striter_len(striterobject *it, PyObject *Py_UNUSED(ignored)) { Py_ssize_t len = 0; if (it->it_seq) - len = PyBytes_GET_SIZE(it->it_seq) - it->it_index; + len = _PyBytes_GET_SIZE(it->it_seq) - it->it_index; return PyLong_FromSsize_t(len); } @@ -3204,8 +3217,8 @@ striter_setstate(striterobject *it, PyObject *state) if (it->it_seq != NULL) { if (index < 0) index = 0; - else if (index > PyBytes_GET_SIZE(it->it_seq)) - index = PyBytes_GET_SIZE(it->it_seq); /* iterator exhausted */ + else if (index > _PyBytes_GET_SIZE(it->it_seq)) + index = _PyBytes_GET_SIZE(it->it_seq); /* iterator exhausted */ it->it_index = index; } Py_RETURN_NONE; @@ -3316,7 +3329,7 @@ _PyBytesWriter_AsString(_PyBytesWriter *writer) } else { assert(writer->buffer != NULL); - return PyBytes_AS_STRING(writer->buffer); + return _PyBytes_AS_STRING(writer->buffer); } } @@ -3414,7 +3427,7 @@ _PyBytesWriter_Resize(_PyBytesWriter *writer, void *str, Py_ssize_t size) if (writer->use_bytearray) dest = PyByteArray_AS_STRING(writer->buffer); else - dest = PyBytes_AS_STRING(writer->buffer); + dest = _PyBytes_AS_STRING(writer->buffer); memcpy(dest, writer->small_buffer, pos); @@ -3581,3 +3594,17 @@ _PyBytes_Repeat(char* dest, Py_ssize_t len_dest, } } + +// --- Public C API functions ------------------------------------------------ + +#undef PyBytes_AS_STRING +char* PyBytes_AS_STRING(PyObject *op) +{ + return _PyBytes_AS_STRING(op); +} + +#undef PyBytes_GET_SIZE +Py_ssize_t PyBytes_GET_SIZE(PyObject *op) +{ + return _PyBytes_GET_SIZE(op); +}