mirror of
https://github.com/python/cpython.git
synced 2025-08-01 23:53:15 +00:00
gh-89653: PEP 670: Convert unicodeobject.h macros to functions (#91799)
Convert unicodeobject.h macros to static inline functions: * PyUnicode_AS_DATA() * PyUnicode_AS_UNICODE() * PyUnicode_GET_DATA_SIZE() * PyUnicode_GET_SIZE() Static inline functions are wrapped by macros which casts arguments with _PyObject_CAST() to prevent introducing new compiler warnings when passing "const PyObject*".
This commit is contained in:
parent
364ed94092
commit
636ad7b47e
1 changed files with 44 additions and 18 deletions
|
@ -590,10 +590,14 @@ Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicodeAndSize(
|
||||||
/* Fast access macros */
|
/* Fast access macros */
|
||||||
|
|
||||||
Py_DEPRECATED(3.3)
|
Py_DEPRECATED(3.3)
|
||||||
static inline Py_ssize_t PyUnicode_WSTR_LENGTH(PyObject *op) {
|
static inline Py_ssize_t PyUnicode_WSTR_LENGTH(PyObject *op)
|
||||||
return PyUnicode_IS_COMPACT_ASCII(op) ?
|
{
|
||||||
_PyASCIIObject_CAST(op)->length :
|
if (PyUnicode_IS_COMPACT_ASCII(op)) {
|
||||||
_PyCompactUnicodeObject_CAST(op)->wstr_length;
|
return _PyASCIIObject_CAST(op)->length;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return _PyCompactUnicodeObject_CAST(op)->wstr_length;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#define PyUnicode_WSTR_LENGTH(op) PyUnicode_WSTR_LENGTH(_PyObject_CAST(op))
|
#define PyUnicode_WSTR_LENGTH(op) PyUnicode_WSTR_LENGTH(_PyObject_CAST(op))
|
||||||
|
|
||||||
|
@ -603,16 +607,25 @@ static inline Py_ssize_t PyUnicode_WSTR_LENGTH(PyObject *op) {
|
||||||
on request. Use PyUnicode_GET_LENGTH() for the length in code points. */
|
on request. Use PyUnicode_GET_LENGTH() for the length in code points. */
|
||||||
|
|
||||||
/* Py_DEPRECATED(3.3) */
|
/* Py_DEPRECATED(3.3) */
|
||||||
#define PyUnicode_GET_SIZE(op) \
|
static inline Py_ssize_t PyUnicode_GET_SIZE(PyObject *op)
|
||||||
(_PyASCIIObject_CAST(op)->wstr ? \
|
{
|
||||||
PyUnicode_WSTR_LENGTH(op) : \
|
_Py_COMP_DIAG_PUSH
|
||||||
((void)PyUnicode_AsUnicode(_PyObject_CAST(op)),\
|
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
|
||||||
assert(_PyASCIIObject_CAST(op)->wstr), \
|
if (_PyASCIIObject_CAST(op)->wstr == NULL) {
|
||||||
PyUnicode_WSTR_LENGTH(op)))
|
(void)PyUnicode_AsUnicode(op);
|
||||||
|
assert(_PyASCIIObject_CAST(op)->wstr != NULL);
|
||||||
|
}
|
||||||
|
return PyUnicode_WSTR_LENGTH(op);
|
||||||
|
_Py_COMP_DIAG_POP
|
||||||
|
}
|
||||||
|
#define PyUnicode_GET_SIZE(op) PyUnicode_GET_SIZE(_PyObject_CAST(op))
|
||||||
|
|
||||||
/* Py_DEPRECATED(3.3) */
|
/* Py_DEPRECATED(3.3) */
|
||||||
#define PyUnicode_GET_DATA_SIZE(op) \
|
static inline Py_ssize_t PyUnicode_GET_DATA_SIZE(PyObject *op)
|
||||||
(PyUnicode_GET_SIZE(op) * Py_UNICODE_SIZE)
|
{
|
||||||
|
return PyUnicode_GET_SIZE(op) * Py_UNICODE_SIZE;
|
||||||
|
}
|
||||||
|
#define PyUnicode_GET_DATA_SIZE(op) PyUnicode_GET_DATA_SIZE(_PyObject_CAST(op))
|
||||||
|
|
||||||
/* Alias for PyUnicode_AsUnicode(). This will create a wchar_t/Py_UNICODE
|
/* Alias for PyUnicode_AsUnicode(). This will create a wchar_t/Py_UNICODE
|
||||||
representation on demand. Using this macro is very inefficient now,
|
representation on demand. Using this macro is very inefficient now,
|
||||||
|
@ -620,13 +633,26 @@ static inline Py_ssize_t PyUnicode_WSTR_LENGTH(PyObject *op) {
|
||||||
use PyUnicode_WRITE() and PyUnicode_READ(). */
|
use PyUnicode_WRITE() and PyUnicode_READ(). */
|
||||||
|
|
||||||
/* Py_DEPRECATED(3.3) */
|
/* Py_DEPRECATED(3.3) */
|
||||||
#define PyUnicode_AS_UNICODE(op) \
|
static inline Py_UNICODE* PyUnicode_AS_UNICODE(PyObject *op)
|
||||||
(_PyASCIIObject_CAST(op)->wstr ? _PyASCIIObject_CAST(op)->wstr : \
|
{
|
||||||
PyUnicode_AsUnicode(_PyObject_CAST(op)))
|
wchar_t *wstr = _PyASCIIObject_CAST(op)->wstr;
|
||||||
|
if (wstr != NULL) {
|
||||||
|
return wstr;
|
||||||
|
}
|
||||||
|
|
||||||
|
_Py_COMP_DIAG_PUSH
|
||||||
|
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
|
||||||
|
return PyUnicode_AsUnicode(op);
|
||||||
|
_Py_COMP_DIAG_POP
|
||||||
|
}
|
||||||
|
#define PyUnicode_AS_UNICODE(op) PyUnicode_AS_UNICODE(_PyObject_CAST(op))
|
||||||
|
|
||||||
/* Py_DEPRECATED(3.3) */
|
/* Py_DEPRECATED(3.3) */
|
||||||
#define PyUnicode_AS_DATA(op) \
|
static inline const char* PyUnicode_AS_DATA(PyObject *op)
|
||||||
((const char *)(PyUnicode_AS_UNICODE(op)))
|
{
|
||||||
|
return (const char *)PyUnicode_AS_UNICODE(op);
|
||||||
|
}
|
||||||
|
#define PyUnicode_AS_DATA(op) PyUnicode_AS_DATA(_PyObject_CAST(op))
|
||||||
|
|
||||||
|
|
||||||
/* --- _PyUnicodeWriter API ----------------------------------------------- */
|
/* --- _PyUnicodeWriter API ----------------------------------------------- */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue