Skip to content

Commit 2d00190

Browse files
gh-92119: ctypes: Print exception class name instead of its representation (GH-98302)
(cherry picked from commit b9dedfe) Co-authored-by: Kamil Turek <kamil.turek@hotmail.com>
1 parent a6f6c3a commit 2d00190

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

Doc/library/ctypes.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ from within *IDLE* or *PythonWin*::
358358
>>> printf(b"%f bottles of beer\n", 42.5)
359359
Traceback (most recent call last):
360360
File "<stdin>", line 1, in <module>
361-
ArgumentError: argument 2: exceptions.TypeError: Don't know how to convert parameter 2
361+
ArgumentError: argument 2: TypeError: Don't know how to convert parameter 2
362362
>>>
363363

364364
As has been mentioned before, all Python types except integers, strings, and
@@ -421,7 +421,7 @@ prototype for a C function), and tries to convert the arguments to valid types::
421421
>>> printf(b"%d %d %d", 1, 2, 3)
422422
Traceback (most recent call last):
423423
File "<stdin>", line 1, in <module>
424-
ArgumentError: argument 2: exceptions.TypeError: wrong type
424+
ArgumentError: argument 2: TypeError: wrong type
425425
>>> printf(b"%s %d %f\n", b"X", 2, 3)
426426
X 2 3.000000
427427
13
@@ -471,7 +471,7 @@ single character Python bytes object into a C char::
471471
>>> strchr(b"abcdef", b"def")
472472
Traceback (most recent call last):
473473
File "<stdin>", line 1, in <module>
474-
ArgumentError: argument 2: exceptions.TypeError: one character string expected
474+
ArgumentError: argument 2: TypeError: one character string expected
475475
>>> print(strchr(b"abcdef", b"x"))
476476
None
477477
>>> strchr(b"abcdef", b"d")

Lib/ctypes/test/test_structures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,13 @@ class Person(Structure):
332332
cls, msg = self.get_except(Person, b"Someone", (1, 2))
333333
self.assertEqual(cls, RuntimeError)
334334
self.assertEqual(msg,
335-
"(Phone) <class 'TypeError'>: "
335+
"(Phone) TypeError: "
336336
"expected bytes, int found")
337337

338338
cls, msg = self.get_except(Person, b"Someone", (b"a", b"b", b"c"))
339339
self.assertEqual(cls, RuntimeError)
340340
self.assertEqual(msg,
341-
"(Phone) <class 'TypeError'>: too many initializers")
341+
"(Phone) TypeError: too many initializers")
342342

343343
def test_huge_field_name(self):
344344
# issue12881: segfault with large structure field names
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Print exception class name instead of its string representation when raising
2+
errors from :mod:`ctypes` calls.

Modules/_ctypes/callproc.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,10 @@ void _ctypes_extend_error(PyObject *exc_class, const char *fmt, ...)
10161016

10171017
PyErr_Fetch(&tp, &v, &tb);
10181018
PyErr_NormalizeException(&tp, &v, &tb);
1019-
cls_str = PyObject_Str(tp);
1019+
if (PyType_Check(tp))
1020+
cls_str = PyType_GetName((PyTypeObject *)tp);
1021+
else
1022+
cls_str = PyObject_Str(tp);
10201023
if (cls_str) {
10211024
PyUnicode_AppendAndDel(&s, cls_str);
10221025
PyUnicode_AppendAndDel(&s, PyUnicode_FromString(": "));

0 commit comments

Comments
 (0)