gh-144100: Fix crash for POINTER(str) used in ctypes argtypes#144108
gh-144100: Fix crash for POINTER(str) used in ctypes argtypes#144108vstinner merged 7 commits intopython:mainfrom
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
|
@vstinner This is my proposed solution if you have time please review it. |
| with self.assertWarns(DeprecationWarning): | ||
| BadType = ctypes.POINTER("BugTrigger") |
There was a problem hiding this comment.
I would prefer to avoid deprecated code path. You should be able to use:
| with self.assertWarns(DeprecationWarning): | |
| BadType = ctypes.POINTER("BugTrigger") | |
| class BadType(ctypes._Pointer): | |
| # _type_ is not defined on purpose | |
| pass |
| if os.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.
You can use the Python C API:
| if os.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) | ||
|
|
||
| class TestPointerStringProto(unittest.TestCase): |
There was a problem hiding this comment.
I would prefer to add the test to the end of PointersTestCase, instead of adding a new test case.
| import gc | ||
| import sys | ||
| import unittest | ||
| import os |
There was a problem hiding this comment.
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>
|
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 commit 8f45925) 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 the 3.14 branch. |
|
GH-144245 is a backport of this pull request to the 3.13 branch. |
|
Merged. Thanks for the fix. |
|
Thanks for guidance and review |
…ython#144108) Co-authored-by: Victor Stinner <vstinner@python.org>
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.