-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
8617d76
bda7921
1685958
5587067
e948a1a
8a05d9e
138e52d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |||||||||||||||||||||
| import gc | ||||||||||||||||||||||
| import sys | ||||||||||||||||||||||
| import unittest | ||||||||||||||||||||||
| import os | ||||||||||||||||||||||
| from ctypes import (CDLL, CFUNCTYPE, Structure, | ||||||||||||||||||||||
| POINTER, pointer, _Pointer, | ||||||||||||||||||||||
| byref, sizeof, | ||||||||||||||||||||||
|
|
@@ -472,6 +473,23 @@ class C(Structure): | |||||||||||||||||||||
| ptr.set_type(c_int) | ||||||||||||||||||||||
| self.assertIs(ptr._type_, c_int) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| class TestPointerStringProto(unittest.TestCase): | ||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||||||||||||||||||||||
| def test_pointer_string_proto_argtypes_error(self): | ||||||||||||||||||||||
| with self.assertWarns(DeprecationWarning): | ||||||||||||||||||||||
| BadType = ctypes.POINTER("BugTrigger") | ||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if os.name == "nt": | ||||||||||||||||||||||
| libc = ctypes.WinDLL("kernel32.dll") | ||||||||||||||||||||||
| func = libc.GetCurrentProcessId | ||||||||||||||||||||||
| else: | ||||||||||||||||||||||
| libc = ctypes.CDLL(None) | ||||||||||||||||||||||
| func = libc.getpid | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func.argtypes = (BadType,) | ||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use the Python C API:
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| with self.assertRaises(ctypes.ArgumentError): | ||||||||||||||||||||||
| func(ctypes.byref(ctypes.c_int(0))) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if __name__ == '__main__': | ||||||||||||||||||||||
| unittest.main() | ||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Fixed a crash in ctypes when using a deprecated ``POINTER(str)`` type in | ||
| ``argtypes``. Instead of aborting, ctypes now raises a proper Python | ||
| exception when the pointer target type is unresolved. |
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.