From 99790860001d24fc02e922e39c78cd10d83fb994 Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Sat, 15 Oct 2022 22:57:26 +0200 Subject: [PATCH 1/6] Print exception type name instead of its representation --- Lib/test/test_ctypes/test_structures.py | 4 ++-- Modules/_ctypes/callproc.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_ctypes/test_structures.py b/Lib/test/test_ctypes/test_structures.py index 13c0470ba2238c..df39dc7f50d3f7 100644 --- a/Lib/test/test_ctypes/test_structures.py +++ b/Lib/test/test_ctypes/test_structures.py @@ -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) : " + "(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) : too many initializers") + "(Phone) TypeError: too many initializers") def test_huge_field_name(self): # issue12881: segfault with large structure field names diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c index fa1dfac6c7d946..bc10fbfc429af9 100644 --- a/Modules/_ctypes/callproc.c +++ b/Modules/_ctypes/callproc.c @@ -1019,7 +1019,7 @@ 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); + cls_str = PyUnicode_FromString(((PyTypeObject *)tp)->tp_name); if (cls_str) { PyUnicode_AppendAndDel(&s, cls_str); PyUnicode_AppendAndDel(&s, PyUnicode_FromString(": ")); From 261f7e6f4969f476e2d9f400647244453a14be2e Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Sat, 15 Oct 2022 22:57:59 +0200 Subject: [PATCH 2/6] Remove outdated exceptions prefix from doc examples --- Doc/library/ctypes.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 0351ec970be0b8..51ee44266f8463 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -359,7 +359,7 @@ from within *IDLE* or *PythonWin*:: >>> printf(b"%f bottles of beer\n", 42.5) Traceback (most recent call last): File "", line 1, in - 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 @@ -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 "", line 1, in - 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 @@ -487,7 +487,7 @@ single character Python bytes object into a C char:: >>> strchr(b"abcdef", b"def") Traceback (most recent call last): File "", line 1, in - ArgumentError: argument 2: exceptions.TypeError: one character string expected + ArgumentError: argument 2: TypeError: wrong type >>> print(strchr(b"abcdef", b"x")) None >>> strchr(b"abcdef", b"d") From afa4abe6e16808ee2869bfb6a39526cd310c5bde Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Sat, 15 Oct 2022 23:16:19 +0200 Subject: [PATCH 3/6] Add a NEWS entry --- .../2022-10-15-23-15-14.gh-issue-92119.PMSwwG.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-10-15-23-15-14.gh-issue-92119.PMSwwG.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-10-15-23-15-14.gh-issue-92119.PMSwwG.rst b/Misc/NEWS.d/next/Core and Builtins/2022-10-15-23-15-14.gh-issue-92119.PMSwwG.rst new file mode 100644 index 00000000000000..7142fc61976560 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-10-15-23-15-14.gh-issue-92119.PMSwwG.rst @@ -0,0 +1,2 @@ +Print exception class name instead of its string representation when raising +errors from :mod:`ctypes` calls. From 5123aaa9905ef0a9851871d1d6f1bba33c193a9a Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Fri, 28 Oct 2022 22:19:53 +0200 Subject: [PATCH 4/6] Use PyType_GetName --- Modules/_ctypes/callproc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c index bc10fbfc429af9..26ed301d4244e5 100644 --- a/Modules/_ctypes/callproc.c +++ b/Modules/_ctypes/callproc.c @@ -1019,7 +1019,7 @@ void _ctypes_extend_error(PyObject *exc_class, const char *fmt, ...) PyErr_Fetch(&tp, &v, &tb); PyErr_NormalizeException(&tp, &v, &tb); - cls_str = PyUnicode_FromString(((PyTypeObject *)tp)->tp_name); + cls_str = PyType_GetName((PyTypeObject *)tp); if (cls_str) { PyUnicode_AppendAndDel(&s, cls_str); PyUnicode_AppendAndDel(&s, PyUnicode_FromString(": ")); From 87bb82c920a84140308d4f4965152ce991ddd35f Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Thu, 3 Nov 2022 10:10:01 +0100 Subject: [PATCH 5/6] Bring back the original error message in docs --- Doc/library/ctypes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 51ee44266f8463..971adb4611fd50 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -487,7 +487,7 @@ single character Python bytes object into a C char:: >>> strchr(b"abcdef", b"def") Traceback (most recent call last): File "", line 1, in - ArgumentError: argument 2: TypeError: wrong type + ArgumentError: argument 2: TypeError: one character string expected >>> print(strchr(b"abcdef", b"x")) None >>> strchr(b"abcdef", b"d") From 2eada1dc818c4b2a72e4dcb7ba3555683d27f1c8 Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Thu, 3 Nov 2022 10:18:25 +0100 Subject: [PATCH 6/6] Get type name only if it is a type object --- Modules/_ctypes/callproc.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c index 26ed301d4244e5..60bedc02b2c2f5 100644 --- a/Modules/_ctypes/callproc.c +++ b/Modules/_ctypes/callproc.c @@ -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 = PyType_GetName((PyTypeObject *)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(": "));