summaryrefslogtreecommitdiff
path: root/Include/cpython/memoryobject.h
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2022-11-28 16:40:08 +0100
committerGitHub <noreply@github.com>2022-11-28 16:40:08 +0100
commit02f72b8b938e301bbaaf0142547014e074bd564c (patch)
tree1ffd7f82d70652c6f480a67fe70aa8ae248fae18 /Include/cpython/memoryobject.h
parent53eef27133c1da395b3b4d7ce0ab1d5b743ffb41 (diff)
downloadcpython-git-02f72b8b938e301bbaaf0142547014e074bd564c.tar.gz
gh-89653: PEP 670: Convert macros to functions (#99843)
Convert macros to static inline functions to avoid macro pitfalls, like duplication of side effects: * DK_ENTRIES() * DK_UNICODE_ENTRIES() * PyCode_GetNumFree() * PyFloat_AS_DOUBLE() * PyInstanceMethod_GET_FUNCTION() * PyMemoryView_GET_BASE() * PyMemoryView_GET_BUFFER() * PyMethod_GET_FUNCTION() * PyMethod_GET_SELF() * PySet_GET_SIZE() * _PyHeapType_GET_MEMBERS() Changes: * PyCode_GetNumFree() casts PyCode_GetNumFree.co_nfreevars from int to Py_ssize_t to be future proof, and because Py_ssize_t is commonly used in the C API. * PyCode_GetNumFree() doesn't cast its argument: the replaced macro already required the exact type PyCodeObject*. * Add assertions in some functions using "CAST" macros to check the arguments type when Python is built with assertions (debug build). * Remove an outdated comment in unicodeobject.h.
Diffstat (limited to 'Include/cpython/memoryobject.h')
-rw-r--r--Include/cpython/memoryobject.h13
1 files changed, 11 insertions, 2 deletions
diff --git a/Include/cpython/memoryobject.h b/Include/cpython/memoryobject.h
index e2a1e168e4..deab3cc89f 100644
--- a/Include/cpython/memoryobject.h
+++ b/Include/cpython/memoryobject.h
@@ -36,7 +36,16 @@ typedef struct {
Py_ssize_t ob_array[1]; /* shape, strides, suboffsets */
} PyMemoryViewObject;
+#define _PyMemoryView_CAST(op) _Py_CAST(PyMemoryViewObject*, op)
+
/* Get a pointer to the memoryview's private copy of the exporter's buffer. */
-#define PyMemoryView_GET_BUFFER(op) (&((PyMemoryViewObject *)(op))->view)
+static inline Py_buffer* PyMemoryView_GET_BUFFER(PyObject *op) {
+ return (&_PyMemoryView_CAST(op)->view);
+}
+#define PyMemoryView_GET_BUFFER(op) PyMemoryView_GET_BUFFER(_PyObject_CAST(op))
+
/* Get a pointer to the exporting object (this may be NULL!). */
-#define PyMemoryView_GET_BASE(op) (((PyMemoryViewObject *)(op))->view.obj)
+static inline PyObject* PyMemoryView_GET_BASE(PyObject *op) {
+ return _PyMemoryView_CAST(op)->view.obj;
+}
+#define PyMemoryView_GET_BASE(op) PyMemoryView_GET_BASE(_PyObject_CAST(op))