8000 bpo-35059: Add _PyObject_CAST() macro (GH-10645) · python/cpython@2ff8fb7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2ff8fb7

Browse files
authored
bpo-35059: Add _PyObject_CAST() macro (GH-10645)
Add _PyObject_CAST() and _PyVarObject_CAST() macros to cast argument to PyObject* and PyVarObject* properly.
1 parent 271753a commit 2ff8fb7

File tree

5 files changed

+29
-23
lines changed

5 files changed

+29
-23
lines changed

Include/internal/pycore_object.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static inline void _PyObject_GC_TRACK_impl(const char *filename, int lineno,
4242
}
4343

4444
#define _PyObject_GC_TRACK(op) \
45-
_PyObject_GC_TRACK_impl(__FILE__, __LINE__, (PyObject *)(op))
45+
_PyObject_GC_TRACK_impl(__FILE__, __LINE__, _PyObject_CAST(op))
4646

4747
/* Tell the GC to stop tracking this object.
4848
*
@@ -70,7 +70,7 @@ static inline void _PyObject_GC_UNTRACK_impl(const char *filename, int lineno,
7070
}
7171

7272
#define _PyObject_GC_UNTRACK(op) \
73-
_PyObject_GC_UNTRACK_impl(__FILE__, __LINE__, (PyObject *)(op))
73+
_PyObject_GC_UNTRACK_impl(__FILE__, __LINE__, _PyObject_CAST(op))
7474

7575
#ifdef __cplusplus
7676
}

Include/object.h

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,20 @@ typedef struct _object {
112112
struct _typeobject *ob_type;
113113
} PyObject;
114114

115+
/* Cast argument to PyObject* type. */
116+
#define _PyObject_CAST(op) ((PyObject*)(op))
117+
115118
typedef struct {
116119
PyObject ob_base;
117120
Py_ssize_t ob_size; /* Number of items in variable part */
118121
} PyVarObject;
119122

120-
#define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)
121-
#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
122-
#define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)
123+
/* Cast argument to PyVarObject* type. */
124+
#define _PyVarObject_CAST(op) ((PyVarObject*)(op))
125+
126+
#define Py_REFCNT(ob) (_PyObject_CAST(ob)->ob_refcnt)
127+
#define Py_TYPE(ob) (_PyObject_CAST(ob)->ob_type)
128+
#define Py_SIZE(ob) (_PyVarObject_CAST(ob)->ob_size)
123129

124130
#ifndef Py_LIMITED_API
125131
/********************* String Literals ****************************************/
@@ -814,7 +820,7 @@ static inline void _Py_INCREF(PyObject *op)
814820
op->ob_refcnt++;
815821
}
816822

817-
#define Py_INCREF(op) _Py_INCREF((PyObject *)(op))
823+
#define Py_INCREF(op) _Py_INCREF(_PyObject_CAST(op))
818824

819825
static inline void _Py_DECREF(const char *filename, int lineno,
820826
PyObject *op)
@@ -832,7 +838,7 @@ static inline void _Py_DECREF(const char *filename, int lineno,
832838
}
833839
}
834840

835-
#define Py_DECREF(op) _Py_DECREF(__FILE__, __LINE__, (PyObject *)(op))
841+
#define Py_DECREF(op) _Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
836842

837843

838844
/* Safely decref `op` and set `op` to NULL, especially useful in tp_clear
@@ -871,7 +877,7 @@ static inline void _Py_DECREF(const char *filename, int lineno,
871877
*/
872878
#define Py_CLEAR(op) \
873879
do { \
874-
PyObject *_py_tmp = (PyObject *)(op); \
880+
PyObject *_py_tmp = _PyObject_CAST(op); \
875881
if (_py_tmp != NULL) { \
876882
(op) = NULL; \
877883
Py_DECREF(_py_tmp); \
@@ -886,7 +892,7 @@ static inline void _Py_XINCREF(PyObject *op)
886892
}
887893
}
888894

889-
#define Py_XINCREF(op) _Py_XINCREF((PyObject *)(op))
895+
#define Py_XINCREF(op) _Py_XINCREF(_PyObject_CAST(op))
890896

891897
static inline void _Py_XDECREF(PyObject *op)
892898
{
@@ -895,7 +901,7 @@ static inline void _Py_XDECREF(PyObject *op)
895901
}
896902
}
897903

898-
#define Py_XDECREF(op) _Py_XDECREF((PyObject *)(op))
904+
#define Py_XDECREF(op) _Py_XDECREF(_PyObject_CAST(op))
899905

900906
#ifndef Py_LIMITED_API
901907
/* Safely decref `op` and set `op` to `op2`.
@@ -919,14 +925,14 @@ static inline void _Py_XDECREF(PyObject *op)
919925

920926
#define Py_SETREF(op, op2) \
921927
do { \
922-
PyObject *_py_tmp = (PyObject *)(op); \
928+
PyObject *_py_tmp = _PyObject_CAST(op); \
923929
(op) = (op2); \
924930
Py_DECREF(_py_tmp); \
925931
} while (0)
926932

927933
#define Py_XSETREF(op, op2) \
928934
do { \
929-
PyObject *_py_tmp = (PyObject *)(op); \
935+
PyObject *_py_tmp = _PyObject_CAST(op); \
930936
(op) = (op2); \
931937
Py_XDECREF(_py_tmp); \
932938
} while (0)
@@ -1122,7 +1128,7 @@ PyAPI_FUNC(void) _PyTrash_thread_destroy_chain(void);
11221128
_PyTrash_thread_destroy_chain(); \
11231129
} \
11241130
else \
1125-
_PyTrash_thread_deposit_object((PyObject*)op); \
1131+
_PyTrash_thread_deposit_object(_PyObject_CAST(op)); \
11261132
} while (0);
11271133

11281134
#ifndef Py_LIMITED_API

Include/objimpl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ PyAPI_FUNC(Py_ssize_t) _PyGC_CollectIfEnabled(void);
258258

259259
PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t);
260260
#define PyObject_GC_Resize(type, op, n) \
261-
( (type *) _PyObject_GC_Resize((PyVarObject *)(op), (n)) )
261+
( (type *) _PyObject_GC_Resize(_PyVarObject_CAST(op), (n)) )
262262

263263

264264
#ifndef Py_LIMITED_API
@@ -356,7 +356,7 @@ PyAPI_FUNC(void) PyObject_GC_Del(void *);
356356
#define Py_VISIT(op) \
357357
do { \
358358
if (op) { \
359-
int vret = visit((PyObject *)(op), arg); \
359+
int vret = visit(_PyObject_CAST(op), arg); \
360360
if (vret) \
361361
return vret; \
362362
} \

Include/odictobject.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ PyAPI_FUNC(int) PyODict_SetItem(PyObject *od, PyObject *key, PyObject *item);
2727
PyAPI_FUNC(int) PyODict_DelItem(PyObject *od, PyObject *key);
2828

2929
/* wrappers around PyDict* functions */
30-
#define PyODict_GetItem(od, key) PyDict_GetItem((PyObject *)od, key)
30+
#define PyODict_GetItem(od, key) PyDict_GetItem(_PyObject_CAST(od), key)
3131
#define PyODict_GetItemWithError(od, key) \
32-
PyDict_GetItemWithError((PyObject *)od, key)
33-
#define PyODict_Contains(od, key) PyDict_Contains((PyObject *)od, key)
34-
#define PyODict_Size(od) PyDict_Size((PyObject *)od)
32+
PyDict_GetItemWithError(_PyObject_CAST(od), key)
33+
#define PyODict_Contains(od, key) PyDict_Contains(_PyObject_CAST(od), key)
34+
#define PyODict_Size(od) PyDict_Size(_PyObject_CAST(od))
3535
#define PyODict_GetItemString(od, key) \
36-
PyDict_GetItemString((PyObject *)od, key)
36+
PyDict_GetItemString(_PyObject_CAST(od), key)
3737

3838
#endif
3939

Include/unicodeobject.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ PyAPI_DATA(PyTypeObject) PyUnicodeIter_Type;
380380
(assert(PyUnicode_Check(op)), \
381381
(((PyASCIIObject *)(op))->wstr) ? \
382382
PyUnicode_WSTR_LENGTH(op) : \
383-
((void)PyUnicode_AsUnicode((PyObject *)(op)), \
383+
((void)PyUnicode_AsUnicode(_PyObject_CAST(op)),\
384384
assert(((PyASCIIObject *)(op))->wstr), \
385385
PyUnicode_WSTR_LENGTH(op)))
386386
/* Py_DEPRECATED(3.3) */
@@ -397,7 +397,7 @@ PyAPI_DATA(PyTypeObject) PyUnicodeIter_Type;
397397
#define PyUnicode_AS_UNICODE(op) \
398398
(assert(PyUnicode_Check(op)), \
399399
(((PyASCIIObject *)(op))->wstr) ? (((PyASCIIObject *)(op))->wstr) : \
400-
PyUnicode_AsUnicode((PyObject *)(op)))
400+
PyUnicode_AsUnicode(_PyObject_CAST(op)))
401401
/* Py_DEPRECATED(3.3) */
402402

403403
#define PyUnicode_AS_DATA(op) \
@@ -549,7 +549,7 @@ enum PyUnicode_Kind {
549549
#define PyUnicode_READY(op) \
550550
(assert(PyUnicode_Check(op)), \
551551
(PyUnicode_IS_READY(op) ? \
552-
0 : _PyUnicode_Ready((PyObject *)(op))))
552+
0 : _PyUnicode_Ready(_PyObject_CAST(op))))
553553

554554
/* Return a maximum character value which is suitable for creating another
555555
string based on op. This is always an approximation but more efficient

0 commit comments

Comments
 (0)
0