@@ -234,6 +234,15 @@ PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
234
234
PyObject *op,
235
235
int check_content);
236
236
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
+
237
246
/* Fast access macros */
238
247
239
248
/* Returns the deprecated Py_UNICODE representation's size in code units
@@ -243,11 +252,10 @@ PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
243
252
244
253
/* Py_DEPRECATED(3.3) */
245
254
#define PyUnicode_GET_SIZE (op ) \
246
- (assert(PyUnicode_Check(op)), \
247
- (((PyASCIIObject *)(op))->wstr) ? \
255
+ (_PyASCIIObject_CAST(op)->wstr ? \
248
256
PyUnicode_WSTR_LENGTH (op) : \
249
257
((void )PyUnicode_AsUnicode(_PyObject_CAST(op)),\
250
- assert(((PyASCIIObject *)(op) )->wstr), \
258
+ assert(_PyASCIIObject_CAST(op )->wstr), \
251
259
PyUnicode_WSTR_LENGTH(op)))
252
260
253
261
/* Py_DEPRECATED(3.3) */
@@ -261,9 +269,8 @@ PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
261
269
262
270
/* Py_DEPRECATED(3.3) */
263
271
#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)))
267
274
268
275
/* Py_DEPRECATED(3.3) */
269
276
#define PyUnicode_AS_DATA (op ) \
@@ -281,25 +288,24 @@ PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
281
288
282
289
/* Use only if you know it's a string */
283
290
#define PyUnicode_CHECK_INTERNED (op ) \
284
- (((PyASCIIObject *)(op) )->state.interned)
291
+ (_PyASCIIObject_CAST(op )->state.interned)
285
292
286
293
/* Return true if the string contains only ASCII characters, or 0 if not. The
287
294
string may be compact (PyUnicode_IS_COMPACT_ASCII) or not, but must be
288
295
ready. */
289
296
#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)
293
299
294
300
/* Return true if the string is compact or 0 if not.
295
301
No type checks or Ready calls are performed. */
296
302
#define PyUnicode_IS_COMPACT (op ) \
297
- (((PyASCIIObject*)(op) )->state.compact)
303
+ (_PyASCIIObject_CAST(op )->state.compact)
298
304
299
305
/* Return true if the string is a compact ASCII string (use PyASCIIObject
300
306
structure), or 0 if not. No type checks or Ready calls are performed. */
301
307
#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))
303
309
304
310
enum PyUnicode_Kind {
305
311
/* String contains only wstr byte characters. This is only possible
@@ -323,23 +329,21 @@ enum PyUnicode_Kind {
323
329
324
330
/* Return one of the PyUnicode_*_KIND values defined above. */
325
331
#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)
329
334
330
335
/* Return a void pointer to the raw unicode buffer. */
331
336
#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 )))
335
340
336
341
#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))
339
344
340
345
#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) : \
343
347
_PyUnicode_NONCOMPACT_DATA (op))
344
348
345
349
/* In the access macros below, "kind" may be evaluated more than once.
@@ -386,8 +390,7 @@ enum PyUnicode_Kind {
386
390
PyUnicode_READ_CHAR, for multiple consecutive reads callers should
387
391
cache kind and use PyUnicode_READ instead. */
388
392
#define PyUnicode_READ_CHAR (unicode, index ) \
389
- (assert(PyUnicode_Check(unicode)), \
390
- assert (PyUnicode_IS_READY(unicode)), \
393
+ (assert(PyUnicode_IS_READY(unicode)), \
391
394
(Py_UCS4) \
392
395
(PyUnicode_KIND((unicode)) == PyUnicode_1BYTE_KIND ? \
393
396
((const Py_UCS1 *)(PyUnicode_DATA((unicode))))[(index)] : \
@@ -401,23 +404,21 @@ enum PyUnicode_Kind {
401
404
the string has it's canonical representation set before calling
402
405
this macro. Call PyUnicode_(FAST_)Ready to ensure that. */
403
406
#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)
407
409
408
410
409
411
/* 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 */
411
413
412
- #define PyUnicode_IS_READY (op ) (((PyASCIIObject*) op)->state.ready)
414
+ #define PyUnicode_IS_READY (op ) (_PyASCIIObject_CAST( op)->state.ready)
413
415
414
416
/* PyUnicode_READY() does less work than _PyUnicode_Ready() in the best
415
417
case. If the canonical representation is not yet set, it will still call
416
418
_PyUnicode_Ready().
417
419
Returns 0 on success and -1 on errors. */
418
420
#define PyUnicode_READY (op ) \
419
- (assert(PyUnicode_Check(op)), \
420
- (PyUnicode_IS_READY(op) ? \
421
+ ((PyUnicode_IS_READY(op) ? \
421
422
0 : _PyUnicode_Ready(_PyObject_CAST(op))))
422
423
423
424
/* Return a maximum character value which is suitable for creating another
@@ -436,8 +437,8 @@ enum PyUnicode_Kind {
436
437
Py_DEPRECATED (3.3 )
437
438
static inline Py_ssize_t PyUnicode_WSTR_LENGTH(PyObject *op) {
438
439
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 ;
441
442
}
442
443
#define PyUnicode_WSTR_LENGTH (op ) PyUnicode_WSTR_LENGTH(_PyObject_CAST(op))
443
444
0 commit comments