mirror of
https://github.com/python/cpython.git
synced 2025-08-29 05:05:03 +00:00
Do for hasattr() what was done for getattr()
Namely, an exception is raised if the second arg to hasattr() is not a string or Unicode.
This commit is contained in:
parent
c974bf4dc2
commit
302b54acd9
1 changed files with 11 additions and 0 deletions
|
@ -944,6 +944,17 @@ builtin_hasattr(PyObject *self, PyObject *args)
|
|||
|
||||
if (!PyArg_ParseTuple(args, "OO:hasattr", &v, &name))
|
||||
return NULL;
|
||||
if (PyUnicode_Check(name)) {
|
||||
name = _PyUnicode_AsDefaultEncodedString(name, NULL);
|
||||
if (name == NULL)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!PyString_Check(name)) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"attribute name must be string");
|
||||
return NULL;
|
||||
}
|
||||
v = PyObject_GetAttr(v, name);
|
||||
if (v == NULL) {
|
||||
PyErr_Clear();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue