[Python-Dev] Py_SIZE vs PyXXX_GET_SIZE (original) (raw)

Victor Stinner victor.stinner at gmail.com
Tue Mar 21 02:48:06 EDT 2017


We may modify PyXXX_GET_SIZE() to add assert(PyXXX_Check()) to help to detect bugs and misuses of these macros in debug mode.

The problem is that I expect a compilation error on PyXXX_GET_SIZE()=size.

The new PyDict_GET_SIZE() macro has the assertion. Use Py_SIZE() to set the size.

Victor

Le 20 mars 2017 12:28, "Serhiy Storchaka" <storchaka at gmail.com> a écrit :

What is the preferable way of getting the size of tuple, list, bytes, bytearray: PySIZE or PyTupleGETSIZE, PyListGETSIZE, PyBytesGETSIZE, PyByteArrayGETSIZE? Are macros for concrete types more preferable or they are outdated?

On one hand concrete type macros are longer than PySIZE, and since concrete type macros are defined not for all PyVarObject types we need to use PySIZE 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 PySIZE with concrete type macros I found two cases of misusing PySIZE 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.


Python-Dev mailing list Python-Dev at python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/victor. stinner%40gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20170321/b73cdc86/attachment.html>



More information about the Python-Dev mailing list