Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34k
gh-144100: Fix crash for POINTER(str) used in ctypes argtypes#144108
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
gh-144100: Fix crash for POINTER(str) used in ctypes argtypes#144108
Uh oh!
There was an error while loading.Please reload this page.
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
VanshAgarwal24036 commentedJan 23, 2026
@vstinner This is my proposed solution if you have time please review it. |
| withself.assertWarns(DeprecationWarning): | ||
| BadType=ctypes.POINTER("BugTrigger") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I would prefer to avoid deprecated code path. You should be able to use:
| withself.assertWarns(DeprecationWarning): | |
| BadType=ctypes.POINTER("BugTrigger") | |
| classBadType(ctypes._Pointer): | |
| # _type_ is not defined on purpose | |
| pass |
| ifos.name=="nt": | ||
| libc=ctypes.WinDLL("kernel32.dll") | ||
| func=libc.GetCurrentProcessId | ||
| else: | ||
| libc=ctypes.CDLL(None) | ||
| func=libc.getpid | ||
| func.argtypes= (BadType,) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
You can use the Python C API:
| ifos.name=="nt": | |
| libc=ctypes.WinDLL("kernel32.dll") | |
| func=libc.GetCurrentProcessId | |
| else: | |
| libc=ctypes.CDLL(None) | |
| func=libc.getpid | |
| func.argtypes= (BadType,) | |
| func=ctypes.pythonapi.Py_GetVersion | |
| func.argtypes= (BadType,) |
| ptr.set_type(c_int) | ||
| self.assertIs(ptr._type_,c_int) | ||
| classTestPointerStringProto(unittest.TestCase): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I would prefer to add the test to the end of PointersTestCase, instead of adding a new test case.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
| importgc | ||
| importsys | ||
| importunittest | ||
| importos |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This import can be removed with my proposed code.
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
vstinner left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
LGTM
8f45925 intopython:mainUh oh!
There was an error while loading.Please reload this page.
Thanks@VanshAgarwal24036 for the PR, and@vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14. |
…ythonGH-144108)(cherry picked from commit8f45925)Co-authored-by: VanshAgarwal24036 <148854295+VanshAgarwal24036@users.noreply.github.com>Co-authored-by: Victor Stinner <vstinner@python.org>
Sorry,@VanshAgarwal24036 and@vstinner, I could not cleanly backport this to |
GH-144244 is a backport of this pull request to the3.14 branch. |
GH-144245 is a backport of this pull request to the3.13 branch. |
vstinner commentedJan 26, 2026
Merged. Thanks for the fix. |
VanshAgarwal24036 commentedJan 26, 2026
Thanks for guidance and review |
Uh oh!
There was an error while loading.Please reload this page.
It fixes a crash in
ctypesthat occur when a deprecatedPOINTER(str)type was used inargtypes.When such a pointer type was encountered during argument conversion,
ctypeswould hit an internal assertion and abort the interpreter in debug builds.This PR replaces the assertion with proper error handling and raises a Python exception instead.