This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mwh
Recipients
Date 2001-05-04.11:11:18
SpamBayes Score
Marked as misclassified
Message-id
In-reply-to
Content
Logged In: YES 
user_id=6656

But consider this:

>>> getattr({},1)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: attribute name must be string
>>> getattr({},1,"e")
'e'

or

>>> getattr(None,u"\343","e")
'e'
>>> getattr(None,u"\343")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeError: ASCII encoding error: ordinal not in range(128)

Here's a possible patch:

Index: bltinmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/bltinmodule.c,v
retrieving revision 2.200
diff -c -r2.200 bltinmodule.c
*** bltinmodule.c       2001/05/02 07:39:38     2.200
--- bltinmodule.c       2001/05/04 11:09:35
***************
*** 845,850 ****
--- 845,854 ----
  The globals and locals are dictionaries, defaulting to the
current\n\
  globals and locals.  If only globals is given, locals
defaults to it.";
  
+ /* Internal API needed by builtin_getattr(): */
+ extern 
+ PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
+                                 const char *errors);
  
  static PyObject *
  builtin_getattr(PyObject *self, PyObject *args)
***************
*** 854,859 ****
--- 858,874 ----
  
        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;
+       } 
+       else if (!PyString_Check(name)) {
+               PyErr_Format(PyExc_TypeError,
+            "getattr: attribute name must be string, not
\"%.500s\"",
+                            name->ob_type->tp_name);
+               return NULL;
+       }
        result = PyObject_GetAttr(v, name);
        if (result == NULL && dflt != NULL) {
                PyErr_Clear();

... though by the time you've done all that, there's not
much point in calling PyObject_GetAttr at all...

OTOH, this changes behaviour, so maybe one should just
clarify this in the docs.
History
Date User Action Args
2007-08-23 13:54:25adminlinkissue420304 messages
2007-08-23 13:54:25admincreate