Skip to content

Remove usage of APIs outside the limited API #692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 28, 2025

Conversation

vyasr
Copy link
Contributor

@vyasr vyasr commented Apr 10, 2025

I hadn't revisited kvikio's usage of CPython's functions outside the limited API since #504, and while reviving rapidsai/devcontainers#278 I found that these usages persist and are easy enough to remove.

@vyasr vyasr added improvement Improves an existing functionality non-breaking Introduces a non-breaking change labels Apr 10, 2025
@vyasr vyasr self-assigned this Apr 10, 2025
@vyasr vyasr requested a review from a team as a code owner April 10, 2025 19:21
@vyasr vyasr force-pushed the fix/limited_api_usage branch 2 times, most recently from 40b3df0 to 52c153d Compare April 10, 2025 19:23
@vyasr vyasr requested a review from a team as a code owner April 10, 2025 19:23
@vyasr vyasr requested a review from KyleFromNVIDIA April 10, 2025 19:23
@vyasr vyasr marked this pull request as draft April 10, 2025 19:24
Copy link

copy-pr-bot bot commented Apr 10, 2025

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@vyasr vyasr force-pushed the fix/limited_api_usage branch from 52c153d to e0b7c07 Compare April 10, 2025 19:31
@vyasr vyasr marked this pull request as ready for review April 10, 2025 19:32
Copy link
Member

@jakirkham jakirkham left a comment

Choose a reason for hiding this comment

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

Thank Vyas! 🙏

So my understanding is these are all macros. As a result by definition they are not part of the limited API. However they are eliminated as part of the build

So the real question is what do they do under-the-hood. Usually the answer is attribute access or assignment

Given the struct types referenced here are part of stable and limited API, I suspect whatever issue we are seeing is a tooling bug and not actually something we need to change. Would be interested in hearing more about how you are checking this and what results the tool is presenting

@@ -125,8 +129,7 @@ cdef class Array:
self.shape_mv = None
self.strides_mv = None
else:
mv = PyMemoryView_FromObject(obj)
pybuf = PyMemoryView_GET_BUFFER(mv)
PyObject_GetBuffer(obj, &pybuf, PyBUF_FULL_RO)
Copy link
Member

Choose a reason for hiding this comment

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

If we are going to do this, it needs to be part of a try...finally... block

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What would that do? This is a C API function that does not push exceptions onto the stack. If it fails, it will return -1, so we could check the error value, but I chose not to do that in this PR since the same is true of the old functions (they could return NULL references, in which case we would have to pop the last exception).

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for the slow follow up here

Have filed a fix for this in PR: #728

Hopefully that makes it clearer what I meant

Comment on lines 209 to +210
Py_INCREF(o)
PyTuple_SET_ITEM(shape, i, o)
PyTuple_SetItem(shape, i, o)
Copy link
Member

Choose a reason for hiding this comment

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

Is the incref still needed here?

Same question occurs with similar lines below

Copy link
Contributor Author

@vyasr vyasr Apr 15, 2025

Choose a reason for hiding this comment

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

It looks like both PyTuple_SET_ITEM and PyTuple_SetItem both steal the reference. That means that 1) nothing has changed relative to the old behavior in this PR, and 2) this incref is indeed necessary since we are retaining local references to o here and without the incref we will wind up undercounting the number of references alive.

Copy link
Member

Choose a reason for hiding this comment

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

Ok great. Thank you for checking! 🙏

Remember there being subtle behavior around stealing with these functions

Agree this seems reasonable then

@vyasr
Copy link
Contributor Author

vyasr commented Apr 11, 2025

I'm not sure I follow your logic. Just because something is a macro it doesn't mean that it will always be available in a limited api build. Many macros in cpython are conditionally defined based on whether we are compiling under the limited api or not.

In this case, my test doesn't involve any tooling, I am simply instructing python to be compiled with the limited api. You can see the test in https://github.com/rapidsai/devcontainers/actions/runs/14388179703/job/40348643586. The result is a compilation failure indicating that these functions/macros are not defined when the limited api define is set.

@vyasr vyasr requested a review from jakirkham April 15, 2025 23:26
Copy link
Member

@madsbk madsbk left a comment

Choose a reason for hiding this comment

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

LGTM

@vyasr vyasr dismissed jakirkham’s stale review April 28, 2025 20:38

Comments were addressed or responded to.

@vyasr
Copy link
Contributor Author

vyasr commented Apr 28, 2025

/merge

@rapids-bot rapids-bot bot merged commit d06f360 into rapidsai:branch-25.06 Apr 28, 2025
59 checks passed
@vyasr vyasr deleted the fix/limited_api_usage branch May 22, 2025 18:13
rapids-bot bot pushed a commit that referenced this pull request May 22, 2025
Follow up on this discussion: #692 (comment)

Ensure that the acquired buffer is properly released even if an exception is raised. Given we may raise an exception, this is important to check.

cc @vyasr

Authors:
  - https://github.com/jakirkham

Approvers:
  - Vyas Ramasubramani (https://github.com/vyasr)

URL: #728
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement Improves an existing functionality non-breaking Introduces a non-breaking change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants