mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 07:31:38 +00:00 
			
		
		
		
	Close #16160: Subclass support now works for types.SimpleNamespace. Thanks to RDM for noticing.
This commit is contained in:
		
							parent
							
								
									e54c718572
								
							
						
					
					
						commit
						547298c94c
					
				
					 3 changed files with 22 additions and 11 deletions
				
			
		| 
						 | 
				
			
			@ -1135,6 +1135,15 @@ def test_as_dict(self):
 | 
			
		|||
        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,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,6 +15,8 @@ Core and Builtins
 | 
			
		|||
- 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.
 | 
			
		||||
 | 
			
		||||
- Issue #15379: Fix passing of non-BMP characters as integers for the charmap
 | 
			
		||||
  decoder (already working as unicode strings).  Patch by Serhiy Storchaka.
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -21,19 +21,19 @@ static PyMemberDef namespace_members[] = {
 | 
			
		|||
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;
 | 
			
		||||
 | 
			
		||||
    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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue