Skip to content

gh-131974: Fix usages of locked_deref in ctypes #131975

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

Conversation

ZeroIntensity
Copy link
Member

@ZeroIntensity ZeroIntensity commented Apr 1, 2025

We need to hold the lock as long as we plan on using the pointer, not just when we dereference it.

{
CDataObject *self = _CDataObject_CAST(myself);
Py_ssize_t size;
Py_ssize_t offset;
PyObject *proto;
void *deref = locked_deref(self);
void *deref = *(void **)self->b_ptr;
Copy link
Member

Choose a reason for hiding this comment

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

You may add a deref_lock_held() function for this operation.

Copy link
Member Author

Choose a reason for hiding this comment

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

Eh, I don't think its necessary. A dereference is easy enough to write.

Co-authored-by: Victor Stinner <vstinner@python.org>
Py_ssize_t i;
size_t cur;
for (cur = start, i = 0; i < len; cur += step, i++) {
for (size_t cur = start, Py_ssize_t i = 0; i < len; cur += step, i++) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm very sorry, my suggestion was not valid C code :-(

You should write it differently, example:

    size_t cur = start;
    for (Py_ssize_t i = 0; i < len; cur += step, i++) { 

Copy link
Member Author

Choose a reason for hiding this comment

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

No worries, I didn't catch it either. I'll just revert it and leave the formatting as-is.

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

@vstinner vstinner merged commit 6e91d1f into python:main Apr 2, 2025
46 checks passed
@vstinner
Copy link
Member

vstinner commented Apr 2, 2025

Merged, thank you. I don't think that this change needs to be backported to 3.13.

@ZeroIntensity
Copy link
Member Author

It can't; we haven't been backporting ctypes thread safety changes.

@ZeroIntensity ZeroIntensity deleted the gh-131974-ctypes-get-contents-lock branch April 2, 2025 21:03
seehwan pushed a commit to seehwan/cpython that referenced this pull request Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants