
In the current Python, __getattribute__ can be called directly with a non-string, causing much weirdness: >>> print ''.__getattribute__(1) Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: 'str' object has no attribute ' �*' >>> To avoid this, i've added a simple check to Objects/object.c: diff -c -r2.161 object.c *** object.c 2001/11/04 07:29:31 2.161 --- object.c 2001/12/04 12:46:13 *************** *** 1210,1215 **** --- 1210,1221 ---- descrgetfunc f; PyObject **dictptr; + if (!PyString_Check(name)) { + PyErr_SetString(PyExc_TypeError, + "attribute name must be string"); + return NULL; + } + if (tp->tp_dict == NULL) { if (PyType_Ready(tp) < 0) return NULL; This produces the better behaviour: >>> print ''.__getattribute__(1) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: attribute name must be string >>> I was about to check in this simple fix -- but then i discovered that it is currently possible to assign attributes with non-string keys: >>> def f(): pass ... >>> f.__getattribute__(1) Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: 'function' object has no attribute ' �*' >>> f.__setattr__(1, 1) >>> f.__getattribute__(1) 1 >>> Adding the above check prevents this usage. Is there any reason why people should be allowed to assign and retrieve attributes with non-string names? -- ?!ng

Is there any reason why people should be allowed to assign and retrieve attributes with non-string names?
Not really, but why should we actively try to prevent it when it's easy to do self.__dict__[1] = 'a'? This means you can't rely on all attribute names being strings, and I'm not going to prevent *that*. The proper fix IMO for the bug you found is to be more careful when formatting the error message. --Guido van Rossum (home page: http://www.python.org/~guido/)

I take it back. All the other functions in object.c require the getattr/setattr argument to be a string or Unicode object. I'll fix object.__getattribute__ and object.__setattr__ (which are really PyObject_Generic{Get,Set}Attr) to do the same. --Guido van Rossum (home page: http://www.python.org/~guido/)

Is there any reason why people should be allowed to assign and retrieve attributes with non-string names?
Not really, but why should we actively try to prevent it when it's easy to do self.__dict__[1] = 'a'? This means you can't rely on all attribute names being strings, and I'm not going to prevent *that*. The proper fix IMO for the bug you found is to be more careful when formatting the error message. --Guido van Rossum (home page: http://www.python.org/~guido/)

I take it back. All the other functions in object.c require the getattr/setattr argument to be a string or Unicode object. I'll fix object.__getattribute__ and object.__setattr__ (which are really PyObject_Generic{Get,Set}Attr) to do the same. --Guido van Rossum (home page: http://www.python.org/~guido/)
participants (2)
-
Guido van Rossum
-
Ka-Ping Yee