]> granicus.if.org Git - python/commitdiff
Remove assumption that cls is a subclass of dict.
authorRaymond Hettinger <python@rcn.com>
Sat, 7 Dec 2002 08:10:51 +0000 (08:10 +0000)
committerRaymond Hettinger <python@rcn.com>
Sat, 7 Dec 2002 08:10:51 +0000 (08:10 +0000)
Simplifies the code and gets Just van Rossum's example to work.

Lib/test/test_types.py
Objects/dictobject.c

index 106d0a5b3b76ea7a4714e577333430c49d09f70b..1b6f3acd8f2127a7e8cfa5f2fe2496298549c667 100644 (file)
@@ -566,9 +566,9 @@ from UserDict import UserDict
 class mydict(dict):
     def __new__(cls, *args, **kwargs):
         return UserDict(*args, **kwargs)
-try: mydict.fromkeys('a b c'.split())
-except TypeError: pass
-else: raise TestFailed, 'dict.fromkeys() failed to detect non-dict class.'
+ud = mydict.fromkeys('ab')
+if ud != {'a':None, 'b':None} or not isinstance(ud,UserDict):
+    raise TestFailed, 'fromkeys did not instantiate using  __new__'
 # dict.copy()
 d = {1:1, 2:2, 3:3}
 if d.copy() != {1:1, 2:2, 3:3}: raise TestFailed, 'dict copy'
index d3603f00458b8763978ecf8db05c09e1051ac9b2..69adc50301908ddaedc4cbfb388e36e7f84634b1 100644 (file)
@@ -979,12 +979,6 @@ dict_fromkeys(PyObject *mp, PyObject *args)
        d = PyObject_CallObject(cls, NULL);
        if (d == NULL)
                return NULL;
-       if (!PyDict_Check(d)) {
-               Py_DECREF(d);
-               PyErr_SetString(PyExc_TypeError,
-                       "class constructor must return a subclass of dict");
-               return NULL;
-       }
 
        it = PyObject_GetIter(seq);
        if (it == NULL){
@@ -999,7 +993,7 @@ dict_fromkeys(PyObject *mp, PyObject *args)
                                goto Fail;
                        break;
                }
-               status = PyDict_SetItem(d, key, value);
+               status = PyObject_SetItem(d, key, value);
                Py_DECREF(key);
                if (status < 0)
                        goto Fail;