Skip to content

Commit 131801d

Browse files
authored
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).
1 parent 85dd6cb commit 131801d

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

Include/cpython/objimpl.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
# error "this header file must not be included directly"
33
#endif
44

5-
#define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
5+
static inline size_t _PyObject_SIZE(PyTypeObject *type) {
6+
return _Py_STATIC_CAST(size_t, type->tp_basicsize);
7+
}
68

79
/* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a
810
vrbl-size object with nitems items, exclusive of gc overhead (if any). The
@@ -18,10 +20,11 @@
1820
# error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2"
1921
#endif
2022

21-
#define _PyObject_VAR_SIZE(typeobj, nitems) \
22-
_Py_SIZE_ROUND_UP((typeobj)->tp_basicsize + \
23-
(nitems)*(typeobj)->tp_itemsize, \
24-
SIZEOF_VOID_P)
23+
static inline size_t _PyObject_VAR_SIZE(PyTypeObject *type, Py_ssize_t nitems) {
24+
size_t size = _Py_STATIC_CAST(size_t, type->tp_basicsize);
25+
size += _Py_STATIC_CAST(size_t, nitems) * _Py_STATIC_CAST(size_t, type->tp_itemsize);
26+
return _Py_SIZE_ROUND_UP(size, SIZEOF_VOID_P);
27+
}
2528

2629

2730
/* This example code implements an object constructor with a custom

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy