]> granicus.if.org Git - python/commitdiff
Do for hasattr() what was done for getattr()
authorJeremy Hylton <jeremy@alum.mit.edu>
Mon, 30 Jul 2001 22:45:19 +0000 (22:45 +0000)
committerJeremy Hylton <jeremy@alum.mit.edu>
Mon, 30 Jul 2001 22:45:19 +0000 (22:45 +0000)
Namely, an exception is raised if the second arg to hasattr() is not a
string or Unicode.

Python/bltinmodule.c

index 571cfe2c8a108d6d800040365723aeb33f514d52..ec55928cccc1efb17be3a5f2208330f348b7b7e8 100644 (file)
@@ -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();