diff options
author | Victor Stinner <vstinner@python.org> | 2022-11-28 16:40:08 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-28 16:40:08 +0100 |
commit | 02f72b8b938e301bbaaf0142547014e074bd564c (patch) | |
tree | 1ffd7f82d70652c6f480a67fe70aa8ae248fae18 /Include/internal/pycore_dict.h | |
parent | 53eef27133c1da395b3b4d7ce0ab1d5b743ffb41 (diff) | |
download | cpython-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/internal/pycore_dict.h')
-rw-r--r-- | Include/internal/pycore_dict.h | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/Include/internal/pycore_dict.h b/Include/internal/pycore_dict.h index 25bd3bffb2..04b7084901 100644 --- a/Include/internal/pycore_dict.h +++ b/Include/internal/pycore_dict.h @@ -128,12 +128,21 @@ struct _dictvalues { #else #define DK_SIZE(dk) (1<<DK_LOG_SIZE(dk)) #endif -#define DK_ENTRIES(dk) \ - (assert((dk)->dk_kind == DICT_KEYS_GENERAL), \ - (PyDictKeyEntry*)(&((int8_t*)((dk)->dk_indices))[(size_t)1 << (dk)->dk_log2_index_bytes])) -#define DK_UNICODE_ENTRIES(dk) \ - (assert((dk)->dk_kind != DICT_KEYS_GENERAL), \ - (PyDictUnicodeEntry*)(&((int8_t*)((dk)->dk_indices))[(size_t)1 << (dk)->dk_log2_index_bytes])) + +static inline void* _DK_ENTRIES(PyDictKeysObject *dk) { + int8_t *indices = (int8_t*)(dk->dk_indices); + size_t index = (size_t)1 << dk->dk_log2_index_bytes; + return (&indices[index]); +} +static inline PyDictKeyEntry* DK_ENTRIES(PyDictKeysObject *dk) { + assert(dk->dk_kind == DICT_KEYS_GENERAL); + return (PyDictKeyEntry*)_DK_ENTRIES(dk); +} +static inline PyDictUnicodeEntry* DK_UNICODE_ENTRIES(PyDictKeysObject *dk) { + assert(dk->dk_kind != DICT_KEYS_GENERAL); + return (PyDictUnicodeEntry*)_DK_ENTRIES(dk); +} + #define DK_IS_UNICODE(dk) ((dk)->dk_kind != DICT_KEYS_GENERAL) #define DICT_VERSION_INCREMENT (1 << DICT_MAX_WATCHERS) |