-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
gh-92119: Print exception class name instead of its representation #98302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Doc/library/ctypes.rst
Outdated
@@ -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: wrong type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the actual error message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably best kept for a separate PR, but I wonder why we don't run these tests as part of the doctest suite.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I will create a separate PR for this. Reverted the change for now.
Doc/library/ctypes.rst
Outdated
@@ -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: wrong type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably best kept for a separate PR, but I wonder why we don't run these tests as part of the doctest suite.
Modules/_ctypes/callproc.c
Outdated
@@ -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 = PyType_GetName((PyTypeObject *)tp); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure it's guaranteed that tp
is actually a type here, although of course it should be. Perhaps we can execute this block only if (PyType_Check(tb))
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://docs.python.org/3/c-api/exceptions.html?highlight=pyerr_fetch#c.PyErr_Fetch, tp is guaranteed to be a typeobject here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no explicit statement that says that, and there is no check in the C code in errors.c
that ensures that the type is really a type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough. Corrected 👍
Thanks @kamilturek for the PR, and @JelleZijlstra for merging it 🌮🎉.. I'm working now to backport this PR to: 3.10, 3.11. |
…resentation (pythonGH-98302) (cherry picked from commit b9dedfe) Co-authored-by: Kamil Turek <[email protected]>
GH-99234 is a backport of this pull request to the 3.11 branch. |
GH-99235 is a backport of this pull request to the 3.10 branch. |
…resentation (pythonGH-98302) (cherry picked from commit b9dedfe) Co-authored-by: Kamil Turek <[email protected]>
…ation (GH-98302) (cherry picked from commit b9dedfe) Co-authored-by: Kamil Turek <[email protected]>
GH-99452 is a backport of this pull request to the 3.10 branch. |
…resentation (python#98302) (cherry picked from commit b9dedfe)
This PR updates the exception messages raised from
ctypes
calls to print the exception class name instead of its string representation. Also, it removes obsoleteexceptions.
prefixes from documentation examples.Closes #92119.