Skip to content

Commit 4e52c66

Browse files
authored
gh-89653: PEP 670: Convert unicodeobject.h macros to functions (#91705)
Convert unicodeobject.h macros to static inline functions: * PyUnicode_CHECK_INTERNED() * PyUnicode_DATA(), _PyUnicode_COMPACT_DATA(), _PyUnicode_NONCOMPACT_DATA() * PyUnicode_GET_LENGTH() * PyUnicode_IS_ASCII() * PyUnicode_IS_COMPACT() * PyUnicode_IS_COMPACT_ASCII() * PyUnicode_IS_READY() Reorder functions to declare functions before their first usage. Static inline functions are wrapped by macros which casts arguments with _PyObject_CAST() to prevent introducing new compiler warnings when passing "const PyObject*".
1 parent 25e3574 commit 4e52c66

File tree

1 file changed

+61
-41
lines changed

1 file changed

+61
-41
lines changed

Include/cpython/unicodeobject.h

+61-41
Original file line numberDiff line numberDiff line change
@@ -287,37 +287,80 @@ PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
287287
#define SSTATE_INTERNED_IMMORTAL 2
288288

289289
/* Use only if you know it's a string */
290-
#define PyUnicode_CHECK_INTERNED(op) \
291-
(_PyASCIIObject_CAST(op)->state.interned)
290+
static inline unsigned int PyUnicode_CHECK_INTERNED(PyObject *op) {
291+
return _PyASCIIObject_CAST(op)->state.interned;
292+
}
293+
#define PyUnicode_CHECK_INTERNED(op) PyUnicode_CHECK_INTERNED(_PyObject_CAST(op))
294+
295+
/* Fast check to determine whether an object is ready. Equivalent to:
296+
PyUnicode_IS_COMPACT(op) || _PyUnicodeObject_CAST(op)->data.any */
297+
static inline unsigned int PyUnicode_IS_READY(PyObject *op) {
298+
return _PyASCIIObject_CAST(op)->state.ready;
299+
}
300+
#define PyUnicode_IS_READY(op) PyUnicode_IS_READY(_PyObject_CAST(op))
292301

293302
/* Return true if the string contains only ASCII characters, or 0 if not. The
294303
string may be compact (PyUnicode_IS_COMPACT_ASCII) or not, but must be
295304
ready. */
296-
#define PyUnicode_IS_ASCII(op) \
297-
(assert(PyUnicode_IS_READY(op)), \
298-
_PyASCIIObject_CAST(op)->state.ascii)
305+
static inline unsigned int PyUnicode_IS_ASCII(PyObject *op) {
306+
assert(PyUnicode_IS_READY(op));
307+
return _PyASCIIObject_CAST(op)->state.ascii;
308+
}
309+
#define PyUnicode_IS_ASCII(op) PyUnicode_IS_ASCII(_PyObject_CAST(op))
299310

300311
/* Return true if the string is compact or 0 if not.
301312
No type checks or Ready calls are performed. */
302-
#define PyUnicode_IS_COMPACT(op) \
303-
(_PyASCIIObject_CAST(op)->state.compact)
313+
static inline unsigned int PyUnicode_IS_COMPACT(PyObject *op) {
314+
return _PyASCIIObject_CAST(op)->state.compact;
315+
}
316+
#define PyUnicode_IS_COMPACT(op) PyUnicode_IS_COMPACT(_PyObject_CAST(op))
304317

305318
/* Return true if the string is a compact ASCII string (use PyASCIIObject
306319
structure), or 0 if not. No type checks or Ready calls are performed. */
307-
#define PyUnicode_IS_COMPACT_ASCII(op) \
308-
(_PyASCIIObject_CAST(op)->state.ascii && PyUnicode_IS_COMPACT(op))
320+
static inline int PyUnicode_IS_COMPACT_ASCII(PyObject *op) {
321+
return (_PyASCIIObject_CAST(op)->state.ascii && PyUnicode_IS_COMPACT(op));
322+
}
323+
#define PyUnicode_IS_COMPACT_ASCII(op) PyUnicode_IS_COMPACT_ASCII(_PyObject_CAST(op))
309324

310325
enum PyUnicode_Kind {
311326
/* String contains only wstr byte characters. This is only possible
312327
when the string was created with a legacy API and _PyUnicode_Ready()
313328
has not been called yet. */
314329
PyUnicode_WCHAR_KIND = 0,
315-
/* Return values of the PyUnicode_KIND() macro: */
330+
/* Return values of the PyUnicode_KIND() function: */
316331
PyUnicode_1BYTE_KIND = 1,
317332
PyUnicode_2BYTE_KIND = 2,
318333
PyUnicode_4BYTE_KIND = 4
319334
};
320335

336+
/* Return one of the PyUnicode_*_KIND values defined above. */
337+
#define PyUnicode_KIND(op) \
338+
(assert(PyUnicode_Check(op)), \
339+
assert(PyUnicode_IS_READY(op)), \
340+
((PyASCIIObject *)(op))->state.kind)
341+
342+
/* Return a void pointer to the raw unicode buffer. */
343+
static inline void* _PyUnicode_COMPACT_DATA(PyObject *op) {
344+
if (PyUnicode_IS_ASCII(op)) {
345+
return (void*)(_PyASCIIObject_CAST(op) + 1);
346+
}
347+
return (void*)(_PyCompactUnicodeObject_CAST(op) + 1);
348+
}
349+
350+
static inline void* _PyUnicode_NONCOMPACT_DATA(PyObject *op) {
351+
void *data = _PyUnicodeObject_CAST(op)->data.any;
352+
assert(data != NULL);
353+
return data;
354+
}
355+
356+
static inline void* PyUnicode_DATA(PyObject *op) {
357+
if (PyUnicode_IS_COMPACT(op)) {
358+
return _PyUnicode_COMPACT_DATA(op);
359+
}
360+
return _PyUnicode_NONCOMPACT_DATA(op);
361+
}
362+
#define PyUnicode_DATA(op) PyUnicode_DATA(_PyObject_CAST(op))
363+
321364
/* Return pointers to the canonical representation cast to unsigned char,
322365
Py_UCS2, or Py_UCS4 for direct character access.
323366
No checks are performed, use PyUnicode_KIND() before to ensure
@@ -327,24 +370,14 @@ enum PyUnicode_Kind {
327370
#define PyUnicode_2BYTE_DATA(op) ((Py_UCS2*)PyUnicode_DATA(op))
328371
#define PyUnicode_4BYTE_DATA(op) ((Py_UCS4*)PyUnicode_DATA(op))
329372

330-
/* Return one of the PyUnicode_*_KIND values defined above. */
331-
#define PyUnicode_KIND(op) \
332-
(assert(PyUnicode_IS_READY(op)), \
333-
_PyASCIIObject_CAST(op)->state.kind)
334-
335-
/* Return a void pointer to the raw unicode buffer. */
336-
#define _PyUnicode_COMPACT_DATA(op) \
337-
(PyUnicode_IS_ASCII(op) ? \
338-
((void*)(_PyASCIIObject_CAST(op) + 1)) : \
339-
((void*)(_PyCompactUnicodeObject_CAST(op) + 1)))
340-
341-
#define _PyUnicode_NONCOMPACT_DATA(op) \
342-
(assert(_PyUnicodeObject_CAST(op)->data.any), \
343-
(_PyUnicodeObject_CAST(op)->data.any))
344-
345-
#define PyUnicode_DATA(op) \
346-
(PyUnicode_IS_COMPACT(op) ? _PyUnicode_COMPACT_DATA(op) : \
347-
_PyUnicode_NONCOMPACT_DATA(op))
373+
/* Returns the length of the unicode string. The caller has to make sure that
374+
the string has it's canonical representation set before calling
375+
this function. Call PyUnicode_(FAST_)Ready to ensure that. */
376+
static inline Py_ssize_t PyUnicode_GET_LENGTH(PyObject *op) {
377+
assert(PyUnicode_IS_READY(op));
378+
return _PyASCIIObject_CAST(op)->length;
379+
}
380+
#define PyUnicode_GET_LENGTH(op) PyUnicode_GET_LENGTH(_PyObject_CAST(op))
348381

349382
/* In the access macros below, "kind" may be evaluated more than once.
350383
All other macro parameters are evaluated exactly once, so it is safe
@@ -400,19 +433,6 @@ enum PyUnicode_Kind {
400433
) \
401434
))
402435

403-
/* Returns the length of the unicode string. The caller has to make sure that
404-
the string has it's canonical representation set before calling
405-
this macro. Call PyUnicode_(FAST_)Ready to ensure that. */
406-
#define PyUnicode_GET_LENGTH(op) \
407-
(assert(PyUnicode_IS_READY(op)), \
408-
_PyASCIIObject_CAST(op)->length)
409-
410-
411-
/* Fast check to determine whether an object is ready. Equivalent to
412-
PyUnicode_IS_COMPACT(op) || _PyUnicodeObject_CAST(op)->data.any */
413-
414-
#define PyUnicode_IS_READY(op) (_PyASCIIObject_CAST(op)->state.ready)
415-
416436
/* PyUnicode_READY() does less work than _PyUnicode_Ready() in the best
417437
case. If the canonical representation is not yet set, it will still call
418438
_PyUnicode_Ready().

0 commit comments

Comments
 (0)