Skip to content

Commit b9dedfe

Browse files
authored
gh-92119: ctypes: Print exception class name instead of its representation (#98302)
1 parent a309ad9 commit b9dedfe

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

Doc/library/ctypes.rst

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

365365
As has been mentioned before, all Python types except integers, strings, and
@@ -422,7 +422,7 @@ prototype for a C function), and tries to convert the arguments to valid types::
422422
>>> printf(b"%d %d %d", 1, 2, 3)
423423
Traceback (most recent call last):
424424
File "<stdin>", line 1, in <module>
425-
ArgumentError: argument 2: exceptions.TypeError: wrong type
425+
ArgumentError: argument 2: TypeError: wrong type
426426
>>> printf(b"%s %d %f\n", b"X", 2, 3)
427427
X 2 3.000000
428428
13
@@ -487,7 +487,7 @@ single character Python bytes object into a C char::
487487
>>> strchr(b"abcdef", b"def")
488488
Traceback (most recent call last):
489489
File "<stdin>", line 1, in <module>
490-
ArgumentError: argument 2: exceptions.TypeError: one character string expected
490+
ArgumentError: argument 2: TypeError: one character string expected
491491
>>> print(strchr(b"abcdef", b"x"))
492492
None
493493
>>> strchr(b"abcdef", b"d")

Lib/test/test_ctypes/test_structures.py

+2-2
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
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

+4-1
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,10 @@ void _ctypes_extend_error(PyObject *exc_class, const char *fmt, ...)
10191019

10201020
PyErr_Fetch(&tp, &v, &tb);
10211021
PyErr_NormalizeException(&tp, &v, &tb);
1022-
cls_str = PyObject_Str(tp);
1022+
if (PyType_Check(tp))
1023+
cls_str = PyType_GetName((PyTypeObject *)tp);
1024+
else
1025+
cls_str = PyObject_Str(tp);
10231026
if (cls_str) {
10241027
PyUnicode_AppendAndDel(&s, cls_str);
10251028
PyUnicode_AppendAndDel(&s, PyUnicode_FromString(": "));

0 commit comments

Comments
 (0)