[Python-checkins] cpython (merge 3.3 -> default): merge for issue #16160: Subclass support now works for types.SimpleNamespace.

eric.snow python-checkins at python.org
Wed Oct 17 07:57:20 CEST 2012


http://hg.python.org/cpython/rev/47b9732696eb
changeset:   79777:47b9732696eb
parent:      79775:055d3ff891ad
parent:      79776:c5124145e79e
user:        Eric Snow <ericsnowcurrently at gmail.com>
date:        Tue Oct 16 22:45:49 2012 -0700
summary:
  merge for issue #16160: Subclass support now works for types.SimpleNamespace.

files:
  Lib/test/test_types.py    |   9 +++++++++
  Misc/NEWS                 |   2 ++
  Objects/namespaceobject.c |  22 +++++++++++-----------
  3 files changed, 22 insertions(+), 11 deletions(-)


diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -1135,6 +1135,15 @@
         with self.assertRaises(TypeError):
             ns['spam']
 
+    def test_subclass(self):
+        class Spam(types.SimpleNamespace):
+            pass
+
+        spam = Spam(ham=8, eggs=9)
+
+        self.assertIs(type(spam), Spam)
+        self.assertEqual(vars(spam), {'ham': 8, 'eggs': 9})
+
 
 def test_main():
     run_unittest(TypesTests, MappingProxyTests, ClassCreationTests,
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -16,6 +16,8 @@
 - Issue #14783: Improve int() docstring and switch docstrings for str(),
   range(), and slice() to use multi-line signatures.
 
+- Issue #16160: Subclass support now works for types.SimpleNamespace.
+
 - Upgrade Unicode data (UCD) to version 6.2.
 
 - Issue #15379: Fix passing of non-BMP characters as integers for the charmap
diff --git a/Objects/namespaceobject.c b/Objects/namespaceobject.c
--- a/Objects/namespaceobject.c
+++ b/Objects/namespaceobject.c
@@ -21,19 +21,19 @@
 static PyObject *
 namespace_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
-    _PyNamespaceObject *ns;
-    ns = PyObject_GC_New(_PyNamespaceObject, &_PyNamespace_Type);
-    if (ns == NULL)
-        return NULL;
+    PyObject *self;
 
-    ns->ns_dict = PyDict_New();
-    if (ns->ns_dict == NULL) {
-        Py_DECREF(ns);
-        return NULL;
+    assert(type != NULL && type->tp_alloc != NULL);
+    self = type->tp_alloc(type, 0);
+    if (self != NULL) {
+        _PyNamespaceObject *ns = (_PyNamespaceObject *)self;
+        ns->ns_dict = PyDict_New();
+        if (ns->ns_dict == NULL) {
+            Py_DECREF(ns);
+            return NULL;
+        }
     }
-
-    PyObject_GC_Track(ns);
-    return (PyObject *)ns;
+    return self;
 }
 
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list