Skip to content

Commit

Permalink
gh-92119: ctypes: Print exception class name instead of its represent…
Browse files Browse the repository at this point in the history
…ation (#98302)
  • Loading branch information
kamilturek committed Nov 8, 2022
1 parent a309ad9 commit b9dedfe
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Doc/library/ctypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ from within *IDLE* or *PythonWin*::
>>> printf(b"%f bottles of beer\n", 42.5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ArgumentError: argument 2: exceptions.TypeError: Don't know how to convert parameter 2
ArgumentError: argument 2: TypeError: Don't know how to convert parameter 2
>>>

As has been mentioned before, all Python types except integers, strings, and
Expand Down Expand Up @@ -422,7 +422,7 @@ prototype for a C function), and tries to convert the arguments to valid types::
>>> printf(b"%d %d %d", 1, 2, 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ArgumentError: argument 2: exceptions.TypeError: wrong type
ArgumentError: argument 2: TypeError: wrong type
>>> printf(b"%s %d %f\n", b"X", 2, 3)
X 2 3.000000
13
Expand Down Expand Up @@ -487,7 +487,7 @@ single character Python bytes object into a C char::
>>> strchr(b"abcdef", b"def")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ArgumentError: argument 2: exceptions.TypeError: one character string expected
ArgumentError: argument 2: TypeError: one character string expected
>>> print(strchr(b"abcdef", b"x"))
None
>>> strchr(b"abcdef", b"d")
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_ctypes/test_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,13 @@ class Person(Structure):
cls, msg = self.get_except(Person, b"Someone", (1, 2))
self.assertEqual(cls, RuntimeError)
self.assertEqual(msg,
"(Phone) <class 'TypeError'>: "
"(Phone) TypeError: "
"expected bytes, int found")

cls, msg = self.get_except(Person, b"Someone", (b"a", b"b", b"c"))
self.assertEqual(cls, RuntimeError)
self.assertEqual(msg,
"(Phone) <class 'TypeError'>: too many initializers")
"(Phone) TypeError: too many initializers")

def test_huge_field_name(self):
# issue12881: segfault with large structure field names
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Print exception class name instead of its string representation when raising
errors from :mod:`ctypes` calls.
5 changes: 4 additions & 1 deletion Modules/_ctypes/callproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,10 @@ void _ctypes_extend_error(PyObject *exc_class, const char *fmt, ...)

PyErr_Fetch(&tp, &v, &tb);
PyErr_NormalizeException(&tp, &v, &tb);
cls_str = PyObject_Str(tp);
if (PyType_Check(tp))
cls_str = PyType_GetName((PyTypeObject *)tp);
else
cls_str = PyObject_Str(tp);
if (cls_str) {
PyUnicode_AppendAndDel(&s, cls_str);
PyUnicode_AppendAndDel(&s, PyUnicode_FromString(": "));
Expand Down

0 comments on commit b9dedfe

Please sign in to comment.