GeezerVison is showing github.com — bpo-44024: Improve the TypeError message for non-string second arguments passed to the built-in functions getattr and hasattr by geryogam · Pull Request #25863 · python/cpython
Conversation
geryogam
commented
May 3, 2021
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Problem
Actual behaviour:
>>> getattr('foobar', 123)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: getattr(): attribute name must be string
>>> hasattr('foobar', 123)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: hasattr(): attribute name must be string
Expected behaviour:
>>> getattr('foobar', 123)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: attribute name must be string, not 'int'
>>> hasattr('foobar', 123)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: attribute name must be string, not 'int'
Motivation:
>>> setattr('foobar', 123, 'baz')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: attribute name must be string, not 'int'
>>> delattr('foobar', 123)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: attribute name must be string, not 'int'
Solution
In the function builtin_getattr defined in Python/bltinmodule.c, we remove the following lines:
if (!PyUnicode_Check(name)) {
PyErr_SetString(PyExc_TypeError,
"getattr(): attribute name must be string");
return NULL;
}
because the expected TypeError message is already implemented in the subsequent call to the functions _PyObject_LookupAttr and PyObject_GetAttr defined in Objects/object.c:
PyObject *
PyObject_GetAttr(PyObject *v, PyObject *name)
{
PyTypeObject *tp = Py_TYPE(v);
if (!PyUnicode_Check(name)) {
PyErr_Format(PyExc_TypeError,
"attribute name must be string, not '%.200s'",
Py_TYPE(name)->tp_name);
return NULL;
}
[…]
int
_PyObject_LookupAttr(PyObject *v, PyObject *name, PyObject **result)
{
PyTypeObject *tp = Py_TYPE(v);
if (!PyUnicode_Check(name)) {
PyErr_Format(PyExc_TypeError,
"attribute name must be string, not '%.200s'",
Py_TYPE(name)->tp_name);
*result = NULL;
return -1;
}
[…]
Likewise, in the function builtin_hasattr_impl defined in Python/bltinmodule.c, we remove the following lines:
if (!PyUnicode_Check(name)) {
PyErr_SetString(PyExc_TypeError,
"hasattr(): attribute name must be string");
return NULL;
}
because the expected TypeError message is already implemented in the subsequent call to the function _PyObject_LookupAttr defined in Objects/object.c.
https://bugs.python.org/issue44024
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Could you add a test for the error message and a news entry for the change?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
geryogam
changed the title
bpo-44024: Use common TypeError message for built-in functions getattr and hasattr
bpo-44024: Use the common TypeError message for non-string second arguments passed to the built-in functions getattr and hasattr
May 4, 2021
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
geryogam
changed the title
bpo-44024: Use the common TypeError message for non-string second arguments passed to the built-in functions getattr and hasattr
bpo-44024: Improve the error message for non-string second arguments passed to the built-in functions getattr and hasattr
May 4, 2021
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
geryogam
changed the title
bpo-44024: Improve the error message for non-string second arguments passed to the built-in functions getattr and hasattr
bpo-44024: Improve the TypeError message for non-string second arguments passed to the built-in functions getattr and hasattr
May 4, 2021
|
This PR is stale because it has been open for 30 days with no activity.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Looks good, thanks! Could you pull and merge upstream in your branch?
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
The change LGTM, but tests are added in wrong place.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|