[issue44024] Use common TypeError message for built-in functions getattr and hasattr

2021-05-03 Thread Géry

Change by Géry :


--
keywords: +patch
pull_requests: +24544
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/25863

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44024] Use common TypeError message for built-in functions getattr and hasattr

2021-05-03 Thread Géry

New submission from Géry :

Problem
---

Actual behaviour:

```python
>>> getattr('foobar', 123)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: getattr(): attribute name must be string
>>> hasattr('foobar', 123)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: hasattr(): attribute name must be string
```

Expected behaviour:

```python
>>> getattr('foobar', 123)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: attribute name must be string, not 'int'
>>> hasattr('foobar', 123)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: attribute name must be string, not 'int'
```

Motivation:

```python
>>> setattr('foobar', 123, 'baz')
Traceback (most recent call last):
  File "", line 1, in 
TypeError: attribute name must be string, not 'int'
>>> delattr('foobar', 123)
Traceback (most recent call last):
  File "", line 1, in 
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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com