Skip to content

Conversation

@Yashp002
Copy link

@Yashp002 Yashp002 commented Nov 15, 2025

This PR adds documentation for the Py_ARRAY_LENGTH macro to the C API documentation, addressing issue #141004.

The documentation is placed in the "Useful macros" section of Doc/c-api/intro.rst and includes:

  • A description of the macro's purpose (computing array length at compile time)
  • A warning that it must be used with C arrays, not pointers
  • An explanation that it's equivalent to sizeof(array) / sizeof((array)[0])

This macro has existed since Python 2.7 but was previously undocumented in the official C API documentation.


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

Added documentation for the Py_ARRAY_LENGTH macro, explaining its usage and requirements.
@StanFromIreland StanFromIreland changed the title Document Py_ARRAY_LENGTH macro in intro.rst gh-141004: Document Py_ARRAY_LENGTH macro in intro.rst Nov 15, 2025
@StanFromIreland
Copy link
Member

Our CI does not like it when the branch the PR is opened from is called "main" IIRC and that is why some extra tests are running.

Yashp002 and others added 3 commits November 16, 2025 02:09
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
@Yashp002
Copy link
Author

Also, is it a generally good practice to break off a branch from the main while working on it myself then?

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
compilation error, otherwise, using this macro with a pointer will
produce incorrect results.

This is generally equivalent to::
Copy link
Member

Choose a reason for hiding this comment

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

I don't like the use of "generally" equivalent because it may indicate that it's equivalent to something else. So I would remove the "This is generally equivalent to:: + code"

Copy link
Member

Choose a reason for hiding this comment

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

I disagree, it's helpful to have something rather than nothing, even if it's not 100% true. This is a good example of a lie-to-children.

If you're against "generally", how about saying "this is semantically equivalent to ..."?

Copy link
Member

@picnixz picnixz Nov 19, 2025

Choose a reason for hiding this comment

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

"generally" is inaccurate; if you want to lie a bit, you should say "roughly equivalent to" (which is what we do in academia or in itertools btw). "Generally" means that this would always be the case (at least to me when used like that), but the macro expansion is different.

  • If you want to use "generally" as a synonym of "usually", "roughly" is better.
  • "semantically equivalent" is too verbose IMO.

Copy link
Member

Choose a reason for hiding this comment

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

"roughly equivalent to" is fine with me.

@picnixz picnixz changed the title gh-141004: Document Py_ARRAY_LENGTH macro in intro.rst gh-141004: Document Py_ARRAY_LENGTH macro Nov 16, 2025
Removed redundant explanation of array size calculation.
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

Happy to see a new contributor :)


.. c:macro:: Py_ARRAY_LENGTH(array)
Compute the length (number of elements) of a statically allocated C array at
Copy link
Member

Choose a reason for hiding this comment

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

I think it's generally well known that the length is the number of elements.

Comment on lines +311 to +312
The *array* argument must be a C array with a size known at compile time,
not a pointer. On supported compilers, passing a pointer will result in a
Copy link
Member

Choose a reason for hiding this comment

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

All arrays can be represented as a pointer, statically allocated or not. Let's instead say something like "heap-allocated array" instead of "pointer".

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
@Yashp002
Copy link
Author

Happy to see a new contributor :)

Thanks, I've been hoping to contribute to python since the longest time tbh 😄

@ZeroIntensity
Copy link
Member

Please address my comments and then I'll merge this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

4 participants