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.

classification
Title: setattr(obj, BADNAME, value) does not raises exception
Type: Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, hdima, loewis, rhettinger, theller, tzot
Priority: normal Keywords:

Created on 2003-10-24 07:53 by hdima, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (9)
msg18741 - (view) Author: Dmitry Vasiliev (hdima) Date: 2003-10-24 07:53
Now I just realize that setattr() does not check value
of the  name argument but simply insert the named value
in __dict__ so setattr(obj, BADNAME, value) not equal
to obj.BADNAME = value. It's true for new and old style
classes and all python versions which I have tried
(1.5, 2.1, 2.2, 2.3). Should not setattr(obj, BADNAME,
value) raise appropriate exception (AttributeError or
maybe ValueError)?

For example:

>>> class Test: pass
...
>>> o = Test()
>>> setattr(o, "test.test", 100)
>>> setattr(o, "12345", 200)
>>> dir(o)
['12345', '__doc__', '__module__', 'test.test']
msg18742 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2003-10-24 20:13
Logged In: YES 
user_id=21627

I don't see it as a big problem, but I would not object much
to a change. Would you like to work on a patch?
msg18743 - (view) Author: Dmitry Vasiliev (hdima) Date: 2003-10-25 11:54
Logged In: YES 
user_id=388573

Ok. I'll start working on it on the next week.
msg18744 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2003-10-25 13:50
Logged In: YES 
user_id=11105

Hm, you mean setattr() should only accept strings containing
valid Python identifiers as the second argument, and choke
on something like "123" or "a.b.c.d"?

I would strongly object to this change, in fact I'm using it
quite often.
msg18745 - (view) Author: Dmitry Vasiliev (hdima) Date: 2003-10-26 09:09
Logged In: YES 
user_id=388573

Give me some use case examples. Maybe you use object's
__dict__ like general purpose dictionary? If so, it seems
ugly for me.
msg18746 - (view) Author: Χρήστος Γεωργίου (Christos Georgiou) (tzot) * Date: 2003-10-28 01:00
Logged In: YES 
user_id=539787

Note: there is a valid_identifier function in 
Objects/typeobject.c which is being used by the __slots__ 
mechanism and I believe is quite handy; however, it is 
declared as static.
msg18747 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2003-10-28 02:29
Logged In: YES 
user_id=357491

I am going to argue for "no" on this one.  The major point of 
setattr is that it allows you to set an attribute for *any* name 
after compile time.  This should go for even attribute names that 
would not normally work.  It is a rather powerful ability that I 
think should stay.

Anyone else care to weigh in on this?
msg18748 - (view) Author: Dmitry Vasiliev (hdima) Date: 2003-10-28 14:22
Logged In: YES 
user_id=388573

I don't know why setattr() should allows *any* attribute
name, but if so shouldn't this ability will be documented at
least?
msg18749 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-11-02 10:00
Logged In: YES 
user_id=80475

I think it's fine as is.
Also, it is certain that some existing code relies on it.
Other that bugging the OP, I see no harm from it.
Recommend closing this as "not a bug".
History
Date User Action Args
2022-04-11 14:56:00adminsetgithub: 39453
2003-10-24 07:53:39hdimacreate