Remove assumption that cls is a subclass of dict.

Simplifies the code and gets Just van Rossum's example to work.
This commit is contained in:
Raymond Hettinger 2002-12-07 08:10:51 +00:00
parent 4e52ca82ae
commit e03e5b1f91
2 changed files with 4 additions and 10 deletions

View File

@ -566,9 +566,9 @@ from UserDict import UserDict
class mydict(dict): class mydict(dict):
def __new__(cls, *args, **kwargs): def __new__(cls, *args, **kwargs):
return UserDict(*args, **kwargs) return UserDict(*args, **kwargs)
try: mydict.fromkeys('a b c'.split()) ud = mydict.fromkeys('ab')
except TypeError: pass if ud != {'a':None, 'b':None} or not isinstance(ud,UserDict):
else: raise TestFailed, 'dict.fromkeys() failed to detect non-dict class.' raise TestFailed, 'fromkeys did not instantiate using __new__'
# dict.copy() # dict.copy()
d = {1:1, 2:2, 3:3} d = {1:1, 2:2, 3:3}
if d.copy() != {1:1, 2:2, 3:3}: raise TestFailed, 'dict copy' if d.copy() != {1:1, 2:2, 3:3}: raise TestFailed, 'dict copy'

View File

@ -979,12 +979,6 @@ dict_fromkeys(PyObject *mp, PyObject *args)
d = PyObject_CallObject(cls, NULL); d = PyObject_CallObject(cls, NULL);
if (d == NULL) if (d == NULL)
return 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); it = PyObject_GetIter(seq);
if (it == NULL){ if (it == NULL){
@ -999,7 +993,7 @@ dict_fromkeys(PyObject *mp, PyObject *args)
goto Fail; goto Fail;
break; break;
} }
status = PyDict_SetItem(d, key, value); status = PyObject_SetItem(d, key, value);
Py_DECREF(key); Py_DECREF(key);
if (status < 0) if (status < 0)
goto Fail; goto Fail;