[Python-Dev] Py_SIZE vs PyXXX_GET_SIZE

Serhiy Storchaka storchaka at gmail.com
Mon Mar 20 07:26:34 EDT 2017


What is the preferable way of getting the size of tuple, list, bytes, 
bytearray: Py_SIZE or PyTuple_GET_SIZE, PyList_GET_SIZE, 
PyBytes_GET_SIZE, PyByteArray_GET_SIZE? Are macros for concrete types 
more preferable or they are outdated?

On one hand concrete type macros are longer than Py_SIZE, and since 
concrete type macros are defined not for all PyVarObject types we need 
to use Py_SIZE for them in any case (for example for PyLongObject and 
PyTypeObject).

On other hand we can add asserts for checking that concrete type macros 
are used with correct types. When I wrote a patch that replaces Py_SIZE 
with concrete type macros I found two cases of misusing Py_SIZE with 
dict object: one in _json.c (already fixed in 
3023ebb43f7607584c3e123aff56e867cb04a418) and other in dictobject.c 
(still not fixed). If prefer using concrete type macros this would 
unlikely happen.



More information about the Python-Dev mailing list