@@ -234,6 +234,15 @@ PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
234234 PyObject *op,
235235 int check_content);
236236
237+
238+ #define _PyASCIIObject_CAST (op ) \
239+ (assert (PyUnicode_Check(op)), (PyASCIIObject*)(op))
240+ #define _PyCompactUnicodeObject_CAST (op ) \
241+ (assert (PyUnicode_Check(op)), (PyCompactUnicodeObject*)(op))
242+ #define _PyUnicodeObject_CAST (op ) \
243+ (assert (PyUnicode_Check(op)), (PyUnicodeObject*)(op))
244+
245+
237246/* Fast access macros */
238247
239248/* Returns the deprecated Py_UNICODE representation's size in code units
@@ -243,11 +252,10 @@ PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
243252
244253/* Py_DEPRECATED(3.3) */
245254#define PyUnicode_GET_SIZE (op ) \
246- (assert (PyUnicode_Check(op)), \
247- (((PyASCIIObject *)(op))->wstr) ? \
255+ (_PyASCIIObject_CAST(op)->wstr ? \
248256 PyUnicode_WSTR_LENGTH (op) : \
249257 ((void )PyUnicode_AsUnicode(_PyObject_CAST(op)),\
250- assert(((PyASCIIObject *)(op) )->wstr), \
258+ assert(_PyASCIIObject_CAST(op )->wstr), \
251259 PyUnicode_WSTR_LENGTH(op)))
252260
253261/* Py_DEPRECATED(3.3) */
@@ -261,9 +269,8 @@ PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
261269
262270/* Py_DEPRECATED(3.3) */
263271#define PyUnicode_AS_UNICODE (op ) \
264- (assert (PyUnicode_Check(op)), \
265- (((PyASCIIObject *)(op))->wstr) ? (((PyASCIIObject *)(op))->wstr) : \
266- PyUnicode_AsUnicode (_PyObject_CAST(op)))
272+ (_PyASCIIObject_CAST(op)->wstr ? _PyASCIIObject_CAST(op)->wstr : \
273+ PyUnicode_AsUnicode (_PyObject_CAST(op)))
267274
268275/* Py_DEPRECATED(3.3) */
269276#define PyUnicode_AS_DATA (op ) \
@@ -281,25 +288,24 @@ PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
281288
282289/* Use only if you know it's a string */
283290#define PyUnicode_CHECK_INTERNED (op ) \
284- (((PyASCIIObject *)(op) )->state.interned)
291+ (_PyASCIIObject_CAST(op )->state.interned)
285292
286293/* Return true if the string contains only ASCII characters, or 0 if not. The
287294 string may be compact (PyUnicode_IS_COMPACT_ASCII) or not, but must be
288295 ready. */
289296#define PyUnicode_IS_ASCII (op ) \
290- (assert (PyUnicode_Check(op)), \
291- assert (PyUnicode_IS_READY(op)), \
292- ((PyASCIIObject*)op)->state.ascii)
297+ (assert (PyUnicode_IS_READY(op)), \
298+ _PyASCIIObject_CAST (op)->state.ascii)
293299
294300/* Return true if the string is compact or 0 if not.
295301 No type checks or Ready calls are performed. */
296302#define PyUnicode_IS_COMPACT (op ) \
297- (((PyASCIIObject*)(op) )->state.compact)
303+ (_PyASCIIObject_CAST(op )->state.compact)
298304
299305/* Return true if the string is a compact ASCII string (use PyASCIIObject
300306 structure), or 0 if not. No type checks or Ready calls are performed. */
301307#define PyUnicode_IS_COMPACT_ASCII (op ) \
302- (((PyASCIIObject*) op)->state.ascii && PyUnicode_IS_COMPACT(op))
308+ (_PyASCIIObject_CAST( op)->state.ascii && PyUnicode_IS_COMPACT(op))
303309
304310enum PyUnicode_Kind {
305311/* String contains only wstr byte characters. This is only possible
@@ -323,23 +329,21 @@ enum PyUnicode_Kind {
323329
324330/* Return one of the PyUnicode_*_KIND values defined above. */
325331#define PyUnicode_KIND (op ) \
326- (assert (PyUnicode_Check(op)), \
327- assert (PyUnicode_IS_READY(op)), \
328- ((PyASCIIObject *)(op))->state.kind)
332+ (assert (PyUnicode_IS_READY(op)), \
333+ _PyASCIIObject_CAST (op)->state.kind)
329334
330335/* Return a void pointer to the raw unicode buffer. */
331336#define _PyUnicode_COMPACT_DATA (op ) \
332- (PyUnicode_IS_ASCII(op) ? \
333- ((void *)((PyASCIIObject*)( op) + 1 )) : \
334- ((void *)((PyCompactUnicodeObject*) (op) + 1 )))
337+ (PyUnicode_IS_ASCII(op) ? \
338+ ((void *)(_PyASCIIObject_CAST( op) + 1 )) : \
339+ ((void *)(_PyCompactUnicodeObject_CAST (op) + 1 )))
335340
336341#define _PyUnicode_NONCOMPACT_DATA (op ) \
337- (assert (((PyUnicodeObject*)( op)) ->data.any), \
338- ((((PyUnicodeObject *)( op)) ->data.any) ))
342+ (assert (_PyUnicodeObject_CAST( op)->data.any), \
343+ (_PyUnicodeObject_CAST( op)->data.any))
339344
340345#define PyUnicode_DATA (op ) \
341- (assert (PyUnicode_Check(op)), \
342- PyUnicode_IS_COMPACT (op) ? _PyUnicode_COMPACT_DATA(op) : \
346+ (PyUnicode_IS_COMPACT(op) ? _PyUnicode_COMPACT_DATA(op) : \
343347 _PyUnicode_NONCOMPACT_DATA (op))
344348
345349/* In the access macros below, "kind" may be evaluated more than once.
@@ -386,8 +390,7 @@ enum PyUnicode_Kind {
386390 PyUnicode_READ_CHAR, for multiple consecutive reads callers should
387391 cache kind and use PyUnicode_READ instead. */
388392#define PyUnicode_READ_CHAR (unicode, index ) \
389- (assert (PyUnicode_Check(unicode)), \
390- assert (PyUnicode_IS_READY(unicode)), \
393+ (assert (PyUnicode_IS_READY(unicode)), \
391394 (Py_UCS4) \
392395 (PyUnicode_KIND((unicode)) == PyUnicode_1BYTE_KIND ? \
393396 ((const Py_UCS1 *)(PyUnicode_DATA((unicode))))[(index)] : \
@@ -401,23 +404,21 @@ enum PyUnicode_Kind {
401404 the string has it's canonical representation set before calling
402405 this macro. Call PyUnicode_(FAST_)Ready to ensure that. */
403406#define PyUnicode_GET_LENGTH (op ) \
404- (assert (PyUnicode_Check(op)), \
405- assert (PyUnicode_IS_READY(op)), \
406- ((PyASCIIObject *)(op))->length)
407+ (assert (PyUnicode_IS_READY(op)), \
408+ _PyASCIIObject_CAST (op)->length)
407409
408410
409411/* Fast check to determine whether an object is ready. Equivalent to
410- PyUnicode_IS_COMPACT(op) || ((PyUnicodeObject*)(op) )->data.any */
412+ PyUnicode_IS_COMPACT(op) || _PyUnicodeObject_CAST(op )->data.any */
411413
412- #define PyUnicode_IS_READY (op ) (((PyASCIIObject*) op)->state.ready)
414+ #define PyUnicode_IS_READY (op ) (_PyASCIIObject_CAST( op)->state.ready)
413415
414416/* PyUnicode_READY() does less work than _PyUnicode_Ready() in the best
415417 case. If the canonical representation is not yet set, it will still call
416418 _PyUnicode_Ready().
417419 Returns 0 on success and -1 on errors. */
418420#define PyUnicode_READY (op ) \
419- (assert (PyUnicode_Check(op)), \
420- (PyUnicode_IS_READY(op) ? \
421+ ((PyUnicode_IS_READY(op) ? \
421422 0 : _PyUnicode_Ready(_PyObject_CAST(op))))
422423
423424/* Return a maximum character value which is suitable for creating another
@@ -436,8 +437,8 @@ enum PyUnicode_Kind {
436437Py_DEPRECATED (3.3 )
437438static inline Py_ssize_t PyUnicode_WSTR_LENGTH(PyObject *op) {
438439 return PyUnicode_IS_COMPACT_ASCII (op) ?
439- ((PyASCIIObject*) op)->length :
440- ((PyCompactUnicodeObject*) op)->wstr_length ;
440+ _PyASCIIObject_CAST ( op)->length :
441+ _PyCompactUnicodeObject_CAST ( op)->wstr_length ;
441442}
442443#define PyUnicode_WSTR_LENGTH (op ) PyUnicode_WSTR_LENGTH(_PyObject_CAST(op))
443444
0 commit comments