◐ Shell
clean mode source ↗

gh-141004: Document Py_ARRAY_LENGTH macro by Yashp002 · Pull Request #141598 · python/cpython

@Yashp002

@Yashp002 Yashp002 commented

Nov 15, 2025

edited by github-actions Bot

Loading

Fixes part of #141004

Added documentation for the Py_ARRAY_LENGTH macro in Doc/c-api/structures.rst. This macro computes the length of a statically allocated C array at compile time.

The documentation includes:

  • Description of the macro's purpose
  • Explanation of what it works with (static arrays only)
  • Warning about pointer incompatibility
  • Code definition

📚 Documentation preview 📚: https://cpython-previews--141598.org.readthedocs.build/

@python-cla-bot

All commit authors signed the Contributor License Agreement.

CLA signed

StanFromIreland

Comment on lines +741 to +743

This macro is defined as::

#define Py_ARRAY_LENGTH(array) (sizeof(array) / sizeof((array)[0]))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, this is wrong since depending on the GCC version it varies.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, let's just say something like "This is generally equivalent to ..."

StanFromIreland


#define Py_ARRAY_LENGTH(array) (sizeof(array) / sizeof((array)[0]))

.. versionadded:: 3.13

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong, it has been around for many years (2.7 & before).

StanFromIreland

Comment on lines 717 to 727

In previous versions, the macros were only available with
``#include "structmember.h"`` and were named without the ``Py_`` prefix
(e.g. as ``T_INT``).
The header is still available and contains the old names, along with
the following deprecated types:

.. c:macro:: T_OBJECT

Like ``Py_T_OBJECT_EX``, but ``NULL`` is converted to ``None``.
This results in surprising behavior in Python: deleting the attribute
effectively sets it to ``None``.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it in this section? Useful macros seems to be a better place IMO.

@Yashp002

Thanks @StanFromIreland for the review, Fixing all three issues now:

Moving to Useful macros section

Removing versionadded tag (my mistake on the version)

Changing to "generally equivalent to" for the definition

@StanFromIreland

Something odd happened and the CI broke :-(

StanFromIreland

@@ -1 +1 @@
../../Lib/_colorize.py No newline at end of file
../../Lib/_colorize.py

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I take that back, this happened and the CI broke, please revert unrelated changes.

StanFromIreland

Comment on lines +733 to +734

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yashp002

right sorry gimme a minute, it's my first PR actually 😭

@StanFromIreland

The mypy failures appear unrelated to the documentation changes

Indeed, but you have made changes to their configuration files too, see my review above, please revert the unrelated changes.

@Yashp002

I'm closing this to submit clean version in a couple of minutes and actively awaiting review.