mirror of
https://github.com/python/cpython.git
synced 2026-01-21 06:40:15 +00:00
gh-89653: PEP 670: Macros always cast arguments in cpython/ (#93766)
Header files in the Include/cpython/ are only included if the Py_LIMITED_API macro is not defined.
This commit is contained in:
parent
c2007573dd
commit
df22eec421
9 changed files with 28 additions and 92 deletions
|
|
@ -188,17 +188,13 @@ PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
|
|||
static inline unsigned int PyUnicode_CHECK_INTERNED(PyObject *op) {
|
||||
return _PyASCIIObject_CAST(op)->state.interned;
|
||||
}
|
||||
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
|
||||
# define PyUnicode_CHECK_INTERNED(op) PyUnicode_CHECK_INTERNED(_PyObject_CAST(op))
|
||||
#endif
|
||||
#define PyUnicode_CHECK_INTERNED(op) PyUnicode_CHECK_INTERNED(_PyObject_CAST(op))
|
||||
|
||||
/* For backward compatibility */
|
||||
static inline unsigned int PyUnicode_IS_READY(PyObject* Py_UNUSED(op)) {
|
||||
return 1;
|
||||
}
|
||||
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
|
||||
# define PyUnicode_IS_READY(op) PyUnicode_IS_READY(_PyObject_CAST(op))
|
||||
#endif
|
||||
#define PyUnicode_IS_READY(op) PyUnicode_IS_READY(_PyObject_CAST(op))
|
||||
|
||||
/* Return true if the string contains only ASCII characters, or 0 if not. The
|
||||
string may be compact (PyUnicode_IS_COMPACT_ASCII) or not, but must be
|
||||
|
|
@ -206,27 +202,21 @@ static inline unsigned int PyUnicode_IS_READY(PyObject* Py_UNUSED(op)) {
|
|||
static inline unsigned int PyUnicode_IS_ASCII(PyObject *op) {
|
||||
return _PyASCIIObject_CAST(op)->state.ascii;
|
||||
}
|
||||
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
|
||||
# define PyUnicode_IS_ASCII(op) PyUnicode_IS_ASCII(_PyObject_CAST(op))
|
||||
#endif
|
||||
#define PyUnicode_IS_ASCII(op) PyUnicode_IS_ASCII(_PyObject_CAST(op))
|
||||
|
||||
/* Return true if the string is compact or 0 if not.
|
||||
No type checks or Ready calls are performed. */
|
||||
static inline unsigned int PyUnicode_IS_COMPACT(PyObject *op) {
|
||||
return _PyASCIIObject_CAST(op)->state.compact;
|
||||
}
|
||||
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
|
||||
# define PyUnicode_IS_COMPACT(op) PyUnicode_IS_COMPACT(_PyObject_CAST(op))
|
||||
#endif
|
||||
#define PyUnicode_IS_COMPACT(op) PyUnicode_IS_COMPACT(_PyObject_CAST(op))
|
||||
|
||||
/* Return true if the string is a compact ASCII string (use PyASCIIObject
|
||||
structure), or 0 if not. No type checks or Ready calls are performed. */
|
||||
static inline int PyUnicode_IS_COMPACT_ASCII(PyObject *op) {
|
||||
return (_PyASCIIObject_CAST(op)->state.ascii && PyUnicode_IS_COMPACT(op));
|
||||
}
|
||||
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
|
||||
# define PyUnicode_IS_COMPACT_ASCII(op) PyUnicode_IS_COMPACT_ASCII(_PyObject_CAST(op))
|
||||
#endif
|
||||
#define PyUnicode_IS_COMPACT_ASCII(op) PyUnicode_IS_COMPACT_ASCII(_PyObject_CAST(op))
|
||||
|
||||
enum PyUnicode_Kind {
|
||||
/* Return values of the PyUnicode_KIND() function: */
|
||||
|
|
@ -236,22 +226,14 @@ enum PyUnicode_Kind {
|
|||
};
|
||||
|
||||
// PyUnicode_KIND(): Return one of the PyUnicode_*_KIND values defined above.
|
||||
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030c0000
|
||||
//
|
||||
// gh-89653: Converting this macro to a static inline function would introduce
|
||||
// new compiler warnings on "kind < PyUnicode_KIND(str)" (compare signed and
|
||||
// unsigned numbers) where kind type is an int or on
|
||||
// "unsigned int kind = PyUnicode_KIND(str)" (cast signed to unsigned).
|
||||
// Only declare the function as static inline function in the limited C API
|
||||
// version 3.12 which is stricter.
|
||||
#define PyUnicode_KIND(op) \
|
||||
(_PyASCIIObject_CAST(op)->state.kind)
|
||||
#else
|
||||
// Limited C API 3.12 and newer
|
||||
static inline int PyUnicode_KIND(PyObject *op) {
|
||||
assert(PyUnicode_IS_READY(op));
|
||||
return _PyASCIIObject_CAST(op)->state.kind;
|
||||
}
|
||||
#endif
|
||||
#define PyUnicode_KIND(op) (_PyASCIIObject_CAST(op)->state.kind)
|
||||
|
||||
/* Return a void pointer to the raw unicode buffer. */
|
||||
static inline void* _PyUnicode_COMPACT_DATA(PyObject *op) {
|
||||
|
|
@ -275,9 +257,7 @@ static inline void* PyUnicode_DATA(PyObject *op) {
|
|||
}
|
||||
return _PyUnicode_NONCOMPACT_DATA(op);
|
||||
}
|
||||
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
|
||||
# define PyUnicode_DATA(op) PyUnicode_DATA(_PyObject_CAST(op))
|
||||
#endif
|
||||
#define PyUnicode_DATA(op) PyUnicode_DATA(_PyObject_CAST(op))
|
||||
|
||||
/* Return pointers to the canonical representation cast to unsigned char,
|
||||
Py_UCS2, or Py_UCS4 for direct character access.
|
||||
|
|
@ -292,9 +272,7 @@ static inline void* PyUnicode_DATA(PyObject *op) {
|
|||
static inline Py_ssize_t PyUnicode_GET_LENGTH(PyObject *op) {
|
||||
return _PyASCIIObject_CAST(op)->length;
|
||||
}
|
||||
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
|
||||
# define PyUnicode_GET_LENGTH(op) PyUnicode_GET_LENGTH(_PyObject_CAST(op))
|
||||
#endif
|
||||
#define PyUnicode_GET_LENGTH(op) PyUnicode_GET_LENGTH(_PyObject_CAST(op))
|
||||
|
||||
/* Write into the canonical representation, this function does not do any sanity
|
||||
checks and is intended for usage in loops. The caller should cache the
|
||||
|
|
@ -319,11 +297,9 @@ static inline void PyUnicode_WRITE(int kind, void *data,
|
|||
_Py_STATIC_CAST(Py_UCS4*, data)[index] = value;
|
||||
}
|
||||
}
|
||||
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
|
||||
#define PyUnicode_WRITE(kind, data, index, value) \
|
||||
PyUnicode_WRITE(_Py_STATIC_CAST(int, kind), _Py_CAST(void*, data), \
|
||||
(index), _Py_STATIC_CAST(Py_UCS4, value))
|
||||
#endif
|
||||
|
||||
/* Read a code point from the string's canonical representation. No checks
|
||||
or ready calls are performed. */
|
||||
|
|
@ -340,12 +316,10 @@ static inline Py_UCS4 PyUnicode_READ(int kind,
|
|||
assert(kind == PyUnicode_4BYTE_KIND);
|
||||
return _Py_STATIC_CAST(const Py_UCS4*, data)[index];
|
||||
}
|
||||
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
|
||||
#define PyUnicode_READ(kind, data, index) \
|
||||
PyUnicode_READ(_Py_STATIC_CAST(int, kind), \
|
||||
_Py_STATIC_CAST(const void*, data), \
|
||||
(index))
|
||||
#endif
|
||||
|
||||
/* PyUnicode_READ_CHAR() is less efficient than PyUnicode_READ() because it
|
||||
calls PyUnicode_KIND() and might call it twice. For single reads, use
|
||||
|
|
@ -369,10 +343,8 @@ static inline Py_UCS4 PyUnicode_READ_CHAR(PyObject *unicode, Py_ssize_t index)
|
|||
assert(kind == PyUnicode_4BYTE_KIND);
|
||||
return PyUnicode_4BYTE_DATA(unicode)[index];
|
||||
}
|
||||
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
|
||||
# define PyUnicode_READ_CHAR(unicode, index) \
|
||||
PyUnicode_READ_CHAR(_PyObject_CAST(unicode), (index))
|
||||
#endif
|
||||
#define PyUnicode_READ_CHAR(unicode, index) \
|
||||
PyUnicode_READ_CHAR(_PyObject_CAST(unicode), (index))
|
||||
|
||||
/* Return a maximum character value which is suitable for creating another
|
||||
string based on op. This is always an approximation but more efficient
|
||||
|
|
@ -395,10 +367,8 @@ static inline Py_UCS4 PyUnicode_MAX_CHAR_VALUE(PyObject *op)
|
|||
assert(kind == PyUnicode_4BYTE_KIND);
|
||||
return 0x10ffffU;
|
||||
}
|
||||
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
|
||||
# define PyUnicode_MAX_CHAR_VALUE(op) \
|
||||
PyUnicode_MAX_CHAR_VALUE(_PyObject_CAST(op))
|
||||
#endif
|
||||
#define PyUnicode_MAX_CHAR_VALUE(op) \
|
||||
PyUnicode_MAX_CHAR_VALUE(_PyObject_CAST(op))
|
||||
|
||||
/* === Public API ========================================================= */
|
||||
|
||||
|
|
@ -417,9 +387,7 @@ static inline int PyUnicode_READY(PyObject* Py_UNUSED(op))
|
|||
{
|
||||
return 0;
|
||||
}
|
||||
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
|
||||
# define PyUnicode_READY(op) PyUnicode_READY(_PyObject_CAST(op))
|
||||
#endif
|
||||
#define PyUnicode_READY(op) PyUnicode_READY(_PyObject_CAST(op))
|
||||
|
||||
/* Get a copy of a Unicode string. */
|
||||
PyAPI_FUNC(PyObject*) _PyUnicode_Copy(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue