summaryrefslogtreecommitdiffstats
path: root/Include/object.h
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2018-11-22 01:57:29 (GMT)
committerGitHub <noreply@github.com>2018-11-22 01:57:29 (GMT)
commit2ff8fb7639a86757c00a7cbbe7da418fffec3870 (patch)
tree05ca8342faadc9cb406fc1b0c7ac6ae44543eec8 /Include/object.h
parent271753a27aca2e13275f0827080b915fb438107a (diff)
downloadcpython-2ff8fb7639a86757c00a7cbbe7da418fffec3870.zip
cpython-2ff8fb7639a86757c00a7cbbe7da418fffec3870.tar.gz
cpython-2ff8fb7639a86757c00a7cbbe7da418fffec3870.tar.bz2
bpo-35059: Add _PyObject_CAST() macro (GH-10645)
Add _PyObject_CAST() and _PyVarObject_CAST() macros to cast argument to PyObject* and PyVarObject* properly.
Diffstat (limited to 'Include/object.h')
-rw-r--r--Include/object.h28
1 files changed, 17 insertions, 11 deletions
diff --git a/Include/object.h b/Include/object.h
index 5947b79..cdcdca8 100644
--- a/Include/object.h
+++ b/Include/object.h
@@ -112,14 +112,20 @@ typedef struct _object {
struct _typeobject *ob_type;
} PyObject;
+/* Cast argument to PyObject* type. */
+#define _PyObject_CAST(op) ((PyObject*)(op))
+
typedef struct {
PyObject ob_base;
Py_ssize_t ob_size; /* Number of items in variable part */
} PyVarObject;
-#define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)
-#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
-#define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)
+/* Cast argument to PyVarObject* type. */
+#define _PyVarObject_CAST(op) ((PyVarObject*)(op))
+
+#define Py_REFCNT(ob) (_PyObject_CAST(ob)->ob_refcnt)
+#define Py_TYPE(ob) (_PyObject_CAST(ob)->ob_type)
+#define Py_SIZE(ob) (_PyVarObject_CAST(ob)->ob_size)
#ifndef Py_LIMITED_API
/********************* String Literals ****************************************/
@@ -814,7 +820,7 @@ static inline void _Py_INCREF(PyObject *op)
op->ob_refcnt++;
}
-#define Py_INCREF(op) _Py_INCREF((PyObject *)(op))
+#define Py_INCREF(op) _Py_INCREF(_PyObject_CAST(op))
static inline void _Py_DECREF(const char *filename, int lineno,
PyObject *op)
@@ -832,7 +838,7 @@ static inline void _Py_DECREF(const char *filename, int lineno,
}
}
-#define Py_DECREF(op) _Py_DECREF(__FILE__, __LINE__, (PyObject *)(op))
+#define Py_DECREF(op) _Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
/* 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,
*/
#define Py_CLEAR(op) \
do { \
- PyObject *_py_tmp = (PyObject *)(op); \
+ PyObject *_py_tmp = _PyObject_CAST(op); \
if (_py_tmp != NULL) { \
(op) = NULL; \
Py_DECREF(_py_tmp); \
@@ -886,7 +892,7 @@ static inline void _Py_XINCREF(PyObject *op)
}
}
-#define Py_XINCREF(op) _Py_XINCREF((PyObject *)(op))
+#define Py_XINCREF(op) _Py_XINCREF(_PyObject_CAST(op))
static inline void _Py_XDECREF(PyObject *op)
{
@@ -895,7 +901,7 @@ static inline void _Py_XDECREF(PyObject *op)
}
}
-#define Py_XDECREF(op) _Py_XDECREF((PyObject *)(op))
+#define Py_XDECREF(op) _Py_XDECREF(_PyObject_CAST(op))
#ifndef Py_LIMITED_API
/* Safely decref `op` and set `op` to `op2`.
@@ -919,14 +925,14 @@ static inline void _Py_XDECREF(PyObject *op)
#define Py_SETREF(op, op2) \
do { \
- PyObject *_py_tmp = (PyObject *)(op); \
+ PyObject *_py_tmp = _PyObject_CAST(op); \
(op) = (op2); \
Py_DECREF(_py_tmp); \
} while (0)
#define Py_XSETREF(op, op2) \
do { \
- PyObject *_py_tmp = (PyObject *)(op); \
+ PyObject *_py_tmp = _PyObject_CAST(op); \
(op) = (op2); \
Py_XDECREF(_py_tmp); \
} while (0)
@@ -1122,7 +1128,7 @@ PyAPI_FUNC(void) _PyTrash_thread_destroy_chain(void);
_PyTrash_thread_destroy_chain(); \
} \
else \
- _PyTrash_thread_deposit_object((PyObject*)op); \
+ _PyTrash_thread_deposit_object(_PyObject_CAST(op)); \
} while (0);
#ifndef Py_LIMITED_API