Skip to content

Conversation

@Yashp002
Copy link
Contributor

@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 <[email protected]>
Co-authored-by: Stan Ulbrych <[email protected]>
Co-authored-by: Stan Ulbrych <[email protected]>
@Yashp002
Copy link
Contributor 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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
@Yashp002
Copy link
Contributor 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.

Yashp002 and others added 3 commits November 20, 2025 23:50
I realize that VLAs also don't have a known size at compile-time.
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.

LGTM.

@ZeroIntensity ZeroIntensity enabled auto-merge (squash) November 24, 2025 13:30
@ZeroIntensity ZeroIntensity added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Nov 24, 2025
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM

@ZeroIntensity ZeroIntensity merged commit e0773ea into python:main Nov 24, 2025
36 checks passed
@github-project-automation github-project-automation bot moved this from Todo to Done in Docs PRs Nov 24, 2025
@miss-islington-app
Copy link

Thanks @Yashp002 for the PR, and @ZeroIntensity for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 24, 2025
(cherry picked from commit e0773ea)

Co-authored-by: Yashraj <[email protected]>
Co-authored-by: Stan Ulbrych <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Nov 24, 2025

GH-141895 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Nov 24, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 24, 2025
(cherry picked from commit e0773ea)

Co-authored-by: Yashraj <[email protected]>
Co-authored-by: Stan Ulbrych <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Nov 24, 2025

GH-141896 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Nov 24, 2025
@ZeroIntensity
Copy link
Member

Congrats on your first contribution @Yashp002 :)

ZeroIntensity added a commit that referenced this pull request Nov 24, 2025
gh-141004: Document `Py_ARRAY_LENGTH` (GH-141601)
(cherry picked from commit e0773ea)

Co-authored-by: Yashraj <[email protected]>
Co-authored-by: Stan Ulbrych <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>
ZeroIntensity added a commit that referenced this pull request Nov 24, 2025
gh-141004: Document `Py_ARRAY_LENGTH` (GH-141601)
(cherry picked from commit e0773ea)

Co-authored-by: Yashraj <[email protected]>
Co-authored-by: Stan Ulbrych <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>
StanFromIreland added a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
Co-authored-by: Stan Ulbrych <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>
ashm-dev pushed a commit to ashm-dev/cpython that referenced this pull request Dec 8, 2025
Co-authored-by: Stan Ulbrych <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants