Skip to content

Commit 2eca5da

Browse files
authored
gh-89653: PEP 670: Convert PyBytes macros to functions (#91990)
Convert the following macros to static inline functions: * PyByteArray_AS_STRING() * PyByteArray_GET_SIZE() * PyBytes_AS_STRING() * PyBytes_GET_SIZE() Limited C API version 3.11 no longer casts arguments. Add _PyBytes_CAST() and _PyByteArray_CAST() macros.
1 parent 4159443 commit 2eca5da

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

Doc/c-api/bytearray.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ These macros trade safety for speed and they don't check pointers.
7777
7878
.. c:function:: char* PyByteArray_AS_STRING(PyObject *bytearray)
7979
80-
Macro version of :c:func:`PyByteArray_AsString`.
80+
Similar to :c:func:`PyByteArray_AsString`, but without error checking.
8181
8282
8383
.. c:function:: Py_ssize_t PyByteArray_GET_SIZE(PyObject *bytearray)
8484
85-
Macro version of :c:func:`PyByteArray_Size`.
85+
Similar to :c:func:`PyByteArray_Size`, but without error checking.

Doc/c-api/bytes.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ called with a non-bytes parameter.
134134
135135
.. c:function:: Py_ssize_t PyBytes_GET_SIZE(PyObject *o)
136136
137-
Macro form of :c:func:`PyBytes_Size` but without error checking.
137+
Similar to :c:func:`PyBytes_Size`, but without error checking.
138138
139139
140140
.. c:function:: char* PyBytes_AsString(PyObject *o)
@@ -151,7 +151,7 @@ called with a non-bytes parameter.
151151
152152
.. c:function:: char* PyBytes_AS_STRING(PyObject *string)
153153
154-
Macro form of :c:func:`PyBytes_AsString` but without error checking.
154+
Similar to :c:func:`PyBytes_AsString`, but without error checking.
155155
156156
157157
.. c:function:: int PyBytes_AsStringAndSize(PyObject *obj, char **buffer, Py_ssize_t *length)

Include/cpython/bytearrayobject.h

+24-6
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,28 @@ typedef struct {
1111
Py_ssize_t ob_exports; /* How many buffer exports */
1212
} PyByteArrayObject;
1313

14-
/* Macros, trading safety for speed */
15-
#define PyByteArray_AS_STRING(self) \
16-
(assert(PyByteArray_Check(self)), \
17-
Py_SIZE(self) ? ((PyByteArrayObject *)(self))->ob_start : _PyByteArray_empty_string)
18-
#define PyByteArray_GET_SIZE(self) (assert(PyByteArray_Check(self)), Py_SIZE(self))
19-
2014
PyAPI_DATA(char) _PyByteArray_empty_string[];
15+
16+
/* Macros and static inline functions, trading safety for speed */
17+
#define _PyByteArray_CAST(op) \
18+
(assert(PyByteArray_Check(op)), _Py_CAST(PyByteArrayObject*, op))
19+
20+
static inline char* PyByteArray_AS_STRING(PyObject *op)
21+
{
22+
PyByteArrayObject *self = _PyByteArray_CAST(op);
23+
if (Py_SIZE(self)) {
24+
return self->ob_start;
25+
}
26+
return _PyByteArray_empty_string;
27+
}
28+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
29+
# define PyByteArray_AS_STRING(self) PyByteArray_AS_STRING(_PyObject_CAST(self))
30+
#endif
31+
32+
static inline Py_ssize_t PyByteArray_GET_SIZE(PyObject *op) {
33+
PyByteArrayObject *self = _PyByteArray_CAST(op);
34+
return Py_SIZE(self);
35+
}
36+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
37+
# define PyByteArray_GET_SIZE(self) PyByteArray_GET_SIZE(_PyObject_CAST(self))
38+
#endif

Include/cpython/bytesobject.h

+19-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,25 @@ PyAPI_FUNC(PyObject*) _PyBytes_FromHex(
2828
PyAPI_FUNC(PyObject *) _PyBytes_DecodeEscape(const char *, Py_ssize_t,
2929
const char *, const char **);
3030

31-
/* Macro, trading safety for speed */
32-
#define PyBytes_AS_STRING(op) (assert(PyBytes_Check(op)), \
33-
(((PyBytesObject *)(op))->ob_sval))
34-
#define PyBytes_GET_SIZE(op) (assert(PyBytes_Check(op)),Py_SIZE(op))
31+
/* Macros and static inline functions, trading safety for speed */
32+
#define _PyBytes_CAST(op) \
33+
(assert(PyBytes_Check(op)), _Py_CAST(PyBytesObject*, op))
34+
35+
static inline char* PyBytes_AS_STRING(PyObject *op)
36+
{
37+
return _PyBytes_CAST(op)->ob_sval;
38+
}
39+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
40+
# define PyBytes_AS_STRING(op) PyBytes_AS_STRING(_PyObject_CAST(op))
41+
#endif
42+
43+
static inline Py_ssize_t PyBytes_GET_SIZE(PyObject *op) {
44+
PyBytesObject *self = _PyBytes_CAST(op);
45+
return Py_SIZE(self);
46+
}
47+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
48+
# define PyBytes_GET_SIZE(self) PyBytes_GET_SIZE(_PyObject_CAST(self))
49+
#endif
3550

3651
/* _PyBytes_Join(sep, x) is like sep.join(x). sep must be PyBytesObject*,
3752
x must be an iterable object. */

0 commit comments

Comments
 (0)