@@ -314,16 +314,15 @@ enum PyUnicode_Kind {
314
314
315
315
/* Return one of the PyUnicode_*_KIND values defined above. */
316
316
#define PyUnicode_KIND (op ) \
317
- (assert(PyUnicode_Check(op)), \
318
- assert (PyUnicode_IS_READY(op)), \
319
- ((PyASCIIObject *)(op))->state.kind)
317
+ (assert(PyUnicode_IS_READY(op)), \
318
+ _PyASCIIObject_CAST (op)->state.kind)
320
319
321
320
/* Return a void pointer to the raw unicode buffer. */
322
321
static inline void* _PyUnicode_COMPACT_DATA(PyObject *op) {
323
322
if (PyUnicode_IS_ASCII (op)) {
324
- return (void *) (_PyASCIIObject_CAST (op) + 1 );
323
+ return _Py_STATIC_CAST (void *, (_PyASCIIObject_CAST (op) + 1 ) );
325
324
}
326
- return (void *) (_PyCompactUnicodeObject_CAST (op) + 1 );
325
+ return _Py_STATIC_CAST (void *, (_PyCompactUnicodeObject_CAST (op) + 1 ) );
327
326
}
328
327
329
328
static inline void * _PyUnicode_NONCOMPACT_DATA (PyObject *op) {
@@ -348,9 +347,9 @@ static inline void* PyUnicode_DATA(PyObject *op) {
348
347
No checks are performed, use PyUnicode_KIND() before to ensure
349
348
these will work correctly. */
350
349
351
- #define PyUnicode_1BYTE_DATA (op ) (( Py_UCS1*) PyUnicode_DATA(op))
352
- #define PyUnicode_2BYTE_DATA (op ) (( Py_UCS2*) PyUnicode_DATA(op))
353
- #define PyUnicode_4BYTE_DATA (op ) (( Py_UCS4*) PyUnicode_DATA(op))
350
+ #define PyUnicode_1BYTE_DATA (op ) _Py_STATIC_CAST( Py_UCS1*, PyUnicode_DATA(op))
351
+ #define PyUnicode_2BYTE_DATA (op ) _Py_STATIC_CAST( Py_UCS2*, PyUnicode_DATA(op))
352
+ #define PyUnicode_4BYTE_DATA (op ) _Py_STATIC_CAST( Py_UCS4*, PyUnicode_DATA(op))
354
353
355
354
/* Returns the length of the unicode string. The caller has to make sure that
356
355
the string has it's canonical representation set before calling
@@ -373,16 +372,16 @@ static inline void PyUnicode_WRITE(unsigned int kind, void *data,
373
372
{
374
373
if (kind == PyUnicode_1BYTE_KIND) {
375
374
assert (value <= 0xffU );
376
- (( Py_UCS1 *) data)[index ] = (Py_UCS1) value;
375
+ _Py_STATIC_CAST ( Py_UCS1*, data)[index ] = _Py_STATIC_CAST (Py_UCS1, value) ;
377
376
}
378
377
else if (kind == PyUnicode_2BYTE_KIND) {
379
378
assert (value <= 0xffffU );
380
- (( Py_UCS2 *) data)[index ] = (Py_UCS2) value;
379
+ _Py_STATIC_CAST ( Py_UCS2*, data)[index ] = _Py_STATIC_CAST (Py_UCS2, value) ;
381
380
}
382
381
else {
383
382
assert (kind == PyUnicode_4BYTE_KIND);
384
383
assert (value <= 0x10ffffU );
385
- (( Py_UCS4 *) data)[index ] = value;
384
+ _Py_STATIC_CAST ( Py_UCS4*, data)[index ] = value;
386
385
}
387
386
}
388
387
#define PyUnicode_WRITE (kind, data, index, value ) \
@@ -394,13 +393,13 @@ static inline Py_UCS4 PyUnicode_READ(unsigned int kind,
394
393
const void *data, Py_ssize_t index)
395
394
{
396
395
if (kind == PyUnicode_1BYTE_KIND) {
397
- return (( const Py_UCS1 *) data)[index ];
396
+ return _Py_STATIC_CAST ( const Py_UCS1*, data)[index ];
398
397
}
399
398
if (kind == PyUnicode_2BYTE_KIND) {
400
- return (( const Py_UCS2 *) data)[index ];
399
+ return _Py_STATIC_CAST ( const Py_UCS2*, data)[index ];
401
400
}
402
401
assert (kind == PyUnicode_4BYTE_KIND);
403
- return (( const Py_UCS4 *) data)[index ];
402
+ return _Py_STATIC_CAST ( const Py_UCS4*, data)[index ];
404
403
}
405
404
#define PyUnicode_READ (kind, data, index ) \
406
405
PyUnicode_READ ((unsigned int )(kind), (const void *)(data), (index))
@@ -693,7 +692,9 @@ static inline const char* PyUnicode_AS_DATA(PyObject *op)
693
692
{
694
693
_Py_COMP_DIAG_PUSH
695
694
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
696
- return (const char *)PyUnicode_AS_UNICODE (op);
695
+ Py_UNICODE *data = PyUnicode_AS_UNICODE (op);
696
+ // In C++, casting directly PyUnicode* to const char* is not valid
697
+ return _Py_STATIC_CAST (const char *, _Py_STATIC_CAST (const void *, data));
697
698
_Py_COMP_DIAG_POP
698
699
}
699
700
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
0 commit comments