Skip to content
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

bpo-44024: Improve the TypeError message for non-string second arguments passed to the built-in functions getattr and hasattr #25863

Merged
merged 8 commits into from
Jan 18, 2022
30 changes: 30 additions & 0 deletions Lib/test/test_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,36 @@ def test_oldargs1_2_kw(self):
msg = r"count\(\) takes no keyword arguments"
self.assertRaisesRegex(TypeError, msg, [].count, x=2, y=2)

def test_argtype0(self):
geryogam marked this conversation as resolved.
Show resolved Hide resolved
geryogam marked this conversation as resolved.
Show resolved Hide resolved
msg = r"^attribute name must be string, not 'int'$"
self.assertRaisesRegex(TypeError, msg, getattr, 'foo', 123)

def test_argtype1(self):
msg = r"^attribute name must be string, not 'int'$"
self.assertRaisesRegex(TypeError, msg, hasattr, 'foo', 123)

def test_argtype2(self):
msg = r"^attribute name must be string, not 'int'$"
self.assertRaisesRegex(TypeError, msg, setattr, 'foo', 123, 'bar')

def test_argtype3(self):
msg = r"^attribute name must be string, not 'int'$"
self.assertRaisesRegex(TypeError, msg, delattr, 'foo', 123)

def test_argtype4(self):
msg = r"^attribute name must be string, not 'int'$"
with self.assertRaisesRegex(TypeError, msg):
object.__getattribute__('foo', 123)

def test_argtype5(self):
msg = r"^attribute name must be string, not 'int'$"
with self.assertRaisesRegex(TypeError, msg):
object.__setattr__('foo', 123, 'bar')

def test_argtype6(self):
msg = r"^attribute name must be string, not 'int'$"
with self.assertRaisesRegex(TypeError, msg):
object.__delattr__('foo', 123)


class TestCallingConventions(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve the exc:`TypeError` message for non-string second arguments passed to
the built-in functions :func:`getattr` and :func:`hasattr`. Patch by Géry Ogam.
10 changes: 0 additions & 10 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1099,11 +1099,6 @@ builtin_getattr(PyObject *self, PyObject *const *args, Py_ssize_t nargs)

v = args[0];
name = args[1];
if (!PyUnicode_Check(name)) {
PyErr_SetString(PyExc_TypeError,
"getattr(): attribute name must be string");
return NULL;
}
if (nargs > 2) {
if (_PyObject_LookupAttr(v, name, &result) == 0) {
PyObject *dflt = args[2];
Expand Down Expand Up @@ -1164,11 +1159,6 @@ builtin_hasattr_impl(PyObject *module, PyObject *obj, PyObject *name)
{
PyObject *v;

if (!PyUnicode_Check(name)) {
PyErr_SetString(PyExc_TypeError,
"hasattr(): attribute name must be string");
return NULL;
}
if (_PyObject_LookupAttr(obj, name, &v) < 0) {
return NULL;
}
Expand Down