]> granicus.if.org Git - python/commitdiff
Fix for SF byg [ #420304 ] getattr function w/ default
authorJeremy Hylton <jeremy@alum.mit.edu>
Mon, 30 Jul 2001 22:39:31 +0000 (22:39 +0000)
committerJeremy Hylton <jeremy@alum.mit.edu>
Mon, 30 Jul 2001 22:39:31 +0000 (22:39 +0000)
Fix suggested by Michael Hudson: Raise TypeError if attribute name
passed to getattr() is not a string or Unicode.  There is some
unfortunate duplication of code between builtin_getattr() and
PyObject_GetAttr(), but it appears to be unavoidable.

Python/bltinmodule.c

index 362cdaa2787dd234a09754d095647a73d03d056a..571cfe2c8a108d6d800040365723aeb33f514d52 100644 (file)
@@ -890,6 +890,17 @@ builtin_getattr(PyObject *self, PyObject *args)
 
        if (!PyArg_ParseTuple(args, "OO|O:getattr", &v, &name, &dflt))
                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;
+       }
        result = PyObject_GetAttr(v, name);
        if (result == NULL && dflt != NULL) {
                PyErr_Clear();