Skip to content
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

incompatible behaviour between 3.12.0rc1 and previous versions in ctypes #107940

Closed
2 tasks done
mattip opened this issue Aug 14, 2023 · 9 comments
Closed
2 tasks done

incompatible behaviour between 3.12.0rc1 and previous versions in ctypes #107940

mattip opened this issue Aug 14, 2023 · 9 comments
Assignees
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes release-blocker topic-ctypes type-bug An unexpected behavior, bug, or error

Comments

@mattip
Copy link
Contributor

mattip commented Aug 14, 2023

Bug report

Testing out 3.12.0rc1 on NumPy, we came across numpy/numpy#24399. I have distilled this down to a reproducer that fails on 3.12.0rc1, but passes on previous versions.

Checklist

  • I am confident this is a bug in CPython, not a bug in a third-party project
  • I have searched the CPython issue tracker, and am confident this bug has not been reported before

A clear and concise description of the bug

from ctypes import Structure, pointer, c_int16, cast, POINTER

class Struct(Structure):
    _fields_ = [('a', c_int16)]

Struct3 = 3 * Struct

c_array = (2 * Struct3)(
    Struct3(Struct(a=1), Struct(a=2), Struct(a=3)),
    Struct3(Struct(a=4), Struct(a=5), Struct(a=6))
)

assert c_array[0][0].a == 1

p_obj = cast(pointer(c_array), POINTER(pointer(c_array)._type_))
obj = p_obj.contents
# Works on 3.12, obj is 2d
assert obj[0][0].a == 1

p_obj = cast(pointer(c_array[0]), POINTER(pointer(c_array)._type_))
obj = p_obj.contents
# Fails on 3.12.0rc1, but works on 3.11 and earlier: obj is not 2d
assert obj[0][0].a == 1

Your environment

  • CPython versions tested on: 3.12rc1
  • Operating system and architecture: ubuntu 22.04

Edit: 3.12rc1 -> 3.12.0rc1

Linked PRs

@mattip mattip added the type-bug An unexpected behavior, bug, or error label Aug 14, 2023
@mattip mattip changed the title incompatible behaviour between 3.12rc1 and previous versions in ctypes incompatible behaviour between 3.12.0rc1 and previous versions in ctypes Aug 14, 2023
@mattip
Copy link
Contributor Author

mattip commented Aug 14, 2023

I see #107131 touched Pointer_get_contents to solve #46376. Perhaps that PR did not take cast into account when checking the equivalency of pointers?

@kumaraditya303 kumaraditya303 added 3.12 bugs and security fixes 3.13 bugs and security fixes labels Aug 20, 2023
ambv added a commit to ambv/cpython that referenced this issue Aug 21, 2023
…tainer

Closes python#107940. Also, solves a related yet undiscovered issue where an array of
pointers reuses the array's memory for the pointer objects.
ambv added a commit to ambv/cpython that referenced this issue Aug 24, 2023
…tainer

Closes python#107940. Also, solves a related yet undiscovered issue where an array of
pointers reuses the array's memory for the pointer objects.
ambv added a commit that referenced this issue Aug 24, 2023
…pes (GH-107131) (GH-107488)" (#108412)

This reverts commit 57f27e4.

The fix caused gh-107940. Until we have a bulletproof fix for that, the 3.11 backport needs to be reverted to make way for 3.11.5.
@vstinner
Copy link
Member

In the 3.12 branch, I confirm that the commit 54aaaad introduced the regression: issue #46376 of PR #107487 (backport of PR #107131).

@vstinner
Copy link
Member

It seems like the PCP project is affected by this regression, see the Fedora issue: https://bugzilla.redhat.com/show_bug.cgi?id=2231751

@vstinner
Copy link
Member

I proposed a revert until the regression is understood and fixed: #108688

ambv added a commit to ambv/cpython that referenced this issue Sep 4, 2023
…tainer

Closes python#107940. Also, solves a related yet undiscovered issue where an array of
pointers reuses the array's memory for the pointer objects.
@ambv
Copy link
Contributor

ambv commented Sep 4, 2023

Revert for main merged. Revert for 3.12 up: #108864

@ambv
Copy link
Contributor

ambv commented Sep 4, 2023

3.12 revert landed as well.

@ambv
Copy link
Contributor

ambv commented Sep 4, 2023

3.11 was reverted before 3.11.5 in GH-108412.

@ambv
Copy link
Contributor

ambv commented Sep 4, 2023

We'll close this and continue search for a good solution in the original issue.

@ambv ambv closed this as completed Sep 4, 2023
@vstinner
Copy link
Member

vstinner commented Sep 4, 2023

We'll close this and continue search for a good solution in the original issue.

Thanks. IMO it's a reasonable practical solution to this complicated problem.

If someone comes up with a new fix, please ping me, I will try to review it! But I'm not available to investigate the issue and attempt writing a better ctypes fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes release-blocker topic-ctypes type-bug An unexpected behavior, bug, or error
Projects
Development

Successfully merging a pull request may close this issue.

6 participants