msg172355 - (view) |
Author: R. David Murray (r.david.murray) *  |
Date: 2012-10-08 01:10 |
Python 3.3.0+ (3.3:152d85b2da3a, Oct 6 2012, 13:17:54) [GCC 4.6.3] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from types import SimpleNamespace >>> class Foo(SimpleNamespace): ... pass ... >>> y = Foo(bar=8, foo=9) >>> y namespace() It doesn't work to define an __init__ method, either, which is what I really wanted to do. (I was subclassing to get the nice repr). |
|
|
msg172357 - (view) |
Author: Christian Heimes (christian.heimes) *  |
Date: 2012-10-08 02:26 |
I have verified that the __init__ function isn't executed when SimpleNamespace is subclasses. I guess that's happening: http://docs.python.org/py3k/c-api/typeobj.html?highlight=tp_init#PyTypeObject.tp_init "If the tp_new function returns an instance of some other type that is not a subtype of the original type, no tp_init function is called; if tp_new returns an instance of a subtype of the original type, the subtype’s tp_init is called." namespace_new always returns a namespace object no matter what. As namespace is not a subclass of the Foo (the other way around), the type check in type_call() fails and __init__ isn't called. The tp_new method needs a fix. That looks all wrong to me: >>> import types >>> class SubNS(types.SimpleNamespace): ... pass ... >>> SubNS.__new__(SubNS) namespace() That's about right: >>> class SubStr(str): ... pass ... >>> type(SubStr.__new__(SubStr)) <class '__main__.SubStr'> |
|
|
msg172510 - (view) |
Author: Eric Snow (eric.snow) *  |
Date: 2012-10-09 19:44 |
Yikes. I'll get a patch up tonight. |
|
|
msg172552 - (view) |
Author: Eric Snow (eric.snow) *  |
Date: 2012-10-10 05:04 |
Here's a patch that fixes subclass support. It was supposed to work in the first place. Guess I had tunnel vision at the time. The fix is essentially a copy of the code in dict_new() in Objects/dictobject.c. I left out the part about releasing GC if the type is not a subclass. Note: see #15022, #15004, and #15003 for other things SimpleNamespace is missing. I can get those done if it would be meaningful to you. |
|
|
msg172565 - (view) |
Author: R. David Murray (r.david.murray) *  |
Date: 2012-10-10 11:09 |
Thanks. I don't have a need for those in my current application at the current time. |
|
|
msg172617 - (view) |
Author: Eric Snow (eric.snow) *  |
Date: 2012-10-11 04:25 |
Here's an updated patch that addresses Éric's review comments. |
|
|
msg172827 - (view) |
Author: Eric Snow (eric.snow) *  |
Date: 2012-10-13 20:24 |
Am I good to commit this? |
|
|
msg173133 - (view) |
Author: Roundup Robot (python-dev)  |
Date: 2012-10-17 05:57 |
New changeset c5124145e79e by Eric Snow in branch '3.3': Close #16160: Subclass support now works for types.SimpleNamespace. Thanks to RDM for noticing. http://hg.python.org/cpython/rev/c5124145e79e New changeset 47b9732696eb by Eric Snow in branch 'default': merge for issue #16160: Subclass support now works for types.SimpleNamespace. http://hg.python.org/cpython/rev/47b9732696eb |
|
|