[issue44024] Use common TypeError message for built-in functions getattr and hasattr
Géry
report at bugs.python.org
Mon May 3 18:50:44 EDT 2021
New submission from Géry <gery.ogam at gmail.com>:
Problem
-------
Actual behaviour:
```python
>>> 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:
```python
>>> 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:
```python
>>> 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:
```c
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:
```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:
```c
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.
----------
components: Interpreter Core
messages: 392843
nosy: maggyero
priority: normal
severity: normal
status: open
title: Use common TypeError message for built-in functions getattr and hasattr
type: behavior
versions: Python 3.10, Python 3.11, Python 3.6, Python 3.7, Python 3.8, Python 3.9
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44024>
_______________________________________
More information about the Python-bugs-list
mailing list