summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2022-11-30 17:17:50 (GMT)
committerGitHub <noreply@github.com>2022-11-30 17:17:50 (GMT)
commit131801d14dfc4f0b2b79103612c88e2e282ff158 (patch)
treeeb23947c6e423613b59a161d8fa9a3530362783c
parent85dd6cb6df996b1197266d1a50ecc9187a91e481 (diff)
downloadcpython-131801d14dfc4f0b2b79103612c88e2e282ff158.zip
cpython-131801d14dfc4f0b2b79103612c88e2e282ff158.tar.gz
cpython-131801d14dfc4f0b2b79103612c88e2e282ff158.tar.bz2
gh-99845: PEP 670: Convert PyObject macros to functions (#99850)
Convert macros to static inline functions to avoid macro pitfalls, like duplication of side effects: * _PyObject_SIZE() * _PyObject_VAR_SIZE() The result type is size_t (unsigned).
-rw-r--r--Include/cpython/objimpl.h13
1 files changed, 8 insertions, 5 deletions
diff --git a/Include/cpython/objimpl.h b/Include/cpython/objimpl.h
index d7c76ea..0b038d3 100644
--- a/Include/cpython/objimpl.h
+++ b/Include/cpython/objimpl.h
@@ -2,7 +2,9 @@
# error "this header file must not be included directly"
#endif
-#define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
+static inline size_t _PyObject_SIZE(PyTypeObject *type) {
+ return _Py_STATIC_CAST(size_t, type->tp_basicsize);
+}
/* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a
vrbl-size object with nitems items, exclusive of gc overhead (if any). The
@@ -18,10 +20,11 @@
# error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2"
#endif
-#define _PyObject_VAR_SIZE(typeobj, nitems) \
- _Py_SIZE_ROUND_UP((typeobj)->tp_basicsize + \
- (nitems)*(typeobj)->tp_itemsize, \
- SIZEOF_VOID_P)
+static inline size_t _PyObject_VAR_SIZE(PyTypeObject *type, Py_ssize_t nitems) {
+ size_t size = _Py_STATIC_CAST(size_t, type->tp_basicsize);
+ size += _Py_STATIC_CAST(size_t, nitems) * _Py_STATIC_CAST(size_t, type->tp_itemsize);
+ return _Py_SIZE_ROUND_UP(size, SIZEOF_VOID_P);
+}
/* This example code implements an object constructor with a custom