gh-89653: PEP 670: Convert macros to functions by vstinner · Pull Request #99843 · python/cpython (original) (raw)
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:
- Add also assertions in some functions using "CAST" macro to check
the type of the arguments when Python is built with assertions
(debug build). - Remove an outdated comment in unicodeobject.h.
- PyCode_GetNumFree() doesn't cast its argument: the replaced macro
already required the exact type PyCodeObject*. - 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. - Issue: [C API] PEP 670: Convert macros to functions in the Python C API #89653
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:
- Add also assertions in some functions using "CAST" macro to check the type of the arguments when Python is built with assertions (debug build).
- Remove an outdated comment in unicodeobject.h.
- PyCode_GetNumFree() doesn't cast its argument: the replaced macro already required the exact type PyCodeObject*.
- 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.
I changed PyCode_GetNumFree() return type to Py_ssize_t.