[Python-bugs-list] [ python-Bugs-829458 ] setattr(obj, BADNAME, value) does not raises exception

SourceForge.net noreply at sourceforge.net
Sun Nov 2 05:00:29 EST 2003


Bugs item #829458, was opened at 2003-10-24 02:53
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=829458&group_id=5470

Category: Python Library
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Dmitry Vasiliev (hdima)
Assigned to: Nobody/Anonymous (nobody)
Summary: setattr(obj, BADNAME, value) does not raises exception

Initial Comment:
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']

----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2003-11-02 05:00

Message:
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".

----------------------------------------------------------------------

Comment By: Dmitry Vasiliev (hdima)
Date: 2003-10-28 09:22

Message:
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?

----------------------------------------------------------------------

Comment By: Brett Cannon (bcannon)
Date: 2003-10-27 21:29

Message:
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?

----------------------------------------------------------------------

Comment By: Christos Georgiou (tzot)
Date: 2003-10-27 20:00

Message:
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.

----------------------------------------------------------------------

Comment By: Dmitry Vasiliev (hdima)
Date: 2003-10-26 04:09

Message:
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.


----------------------------------------------------------------------

Comment By: Thomas Heller (theller)
Date: 2003-10-25 08:50

Message:
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.

----------------------------------------------------------------------

Comment By: Dmitry Vasiliev (hdima)
Date: 2003-10-25 06:54

Message:
Logged In: YES 
user_id=388573

Ok. I'll start working on it on the next week.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2003-10-24 15:13

Message:
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?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=829458&group_id=5470



More information about the Python-bugs-list mailing list