aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-11-07 21:20:06 -0800
committerGitHub <noreply@github.com>2022-11-07 21:20:06 -0800
commit2d00190591922750a04742ec1b5cf5466daff059 (patch)
tree1d48759100d83465ea5ff18bf524df053529ae2e
parent[3.11] gh-98433: Fix quadratic time idna decoding. (GH-99092) (#99222) (diff)
downloadcpython-2d00190591922750a04742ec1b5cf5466daff059.tar.gz
cpython-2d00190591922750a04742ec1b5cf5466daff059.tar.bz2
cpython-2d00190591922750a04742ec1b5cf5466daff059.zip
gh-92119: ctypes: Print exception class name instead of its representation (GH-98302)
(cherry picked from commit b9dedfe61dce2997e3e6be318d8c50b0c19c9394) Co-authored-by: Kamil Turek <kamil.turek@hotmail.com>
-rw-r--r--Doc/library/ctypes.rst6
-rw-r--r--Lib/ctypes/test/test_structures.py4
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2022-10-15-23-15-14.gh-issue-92119.PMSwwG.rst2
-rw-r--r--Modules/_ctypes/callproc.c5
4 files changed, 11 insertions, 6 deletions
diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst
index 2900f77589e..a8d19a9d1a2 100644
--- a/Doc/library/ctypes.rst
+++ b/Doc/library/ctypes.rst
@@ -358,7 +358,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
@@ -421,7 +421,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
@@ -471,7 +471,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")
diff --git a/Lib/ctypes/test/test_structures.py b/Lib/ctypes/test/test_structures.py
index 97ad2b8ed8a..f95d5a99a3a 100644
--- a/Lib/ctypes/test/test_structures.py
+++ b/Lib/ctypes/test/test_structures.py
@@ -332,13 +332,13 @@ class StructureTestCase(unittest.TestCase):
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
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 00000000000..7142fc61976
--- /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.
diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
index 3fab9ad0c1e..7875640ce2b 100644
--- a/Modules/_ctypes/callproc.c
+++ b/Modules/_ctypes/callproc.c
@@ -1016,7 +1016,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(": "));