Index: Misc/NEWS =================================================================== RCS file: /cvsroot/python/python/dist/src/Misc/NEWS,v retrieving revision 1.541 diff -c -r1.541 NEWS *** Misc/NEWS 23 Nov 2002 22:08:15 -0000 1.541 --- Misc/NEWS 26 Nov 2002 09:58:26 -0000 *************** *** 268,273 **** --- 268,278 ---- an optional argument that specifies the characters to strip. For example, "Foo!!!?!?!?".rstrip("?!") -> "Foo". + - Dictionaries have a new class method, fromseq(sequence, value=None). + It constructs a new dictionary with keys from the sequence and a single + constant value. It is used for building membership tests and for + removing duplicates from sequences (or any iterable). + - Added a new dict method pop(key). This removes and returns the value corresponding to key. [SF patch #539949] Index: Objects/dictobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/dictobject.c,v retrieving revision 2.131 diff -c -r2.131 dictobject.c *** Objects/dictobject.c 23 Nov 2002 09:45:04 -0000 2.131 --- Objects/dictobject.c 26 Nov 2002 09:58:27 -0000 *************** *** 963,968 **** --- 963,1014 ---- } static PyObject * + dict_fromseq(PyObject *mp, PyObject *args) + { + PyObject *seq; + PyObject *value = Py_None; + PyObject *it; /* iter(seq) */ + PyObject *key; + PyObject *d; + PyObject *cls; + int status; + + if (!PyArg_ParseTuple(args, "OO|O:fromseq", &cls, &seq, &value)) + return NULL; + + d = PyObject_CallObject(cls, NULL); + if (d == NULL) + return NULL; + + it = PyObject_GetIter(seq); + if (it == NULL){ + Py_DECREF(d); + return NULL; + } + + for (;;) { + key = PyIter_Next(it); + if (key == NULL) { + if (PyErr_Occurred()) + goto Fail; + break; + } + status = PyDict_SetItem(d, key, value); + Py_DECREF(key); + if (status < 0) + goto Fail; + } + + Py_DECREF(it); + return d; + + Fail: + Py_DECREF(it); + Py_DECREF(d); + return NULL; + } + + static PyObject * dict_update(PyObject *mp, PyObject *other) { if (PyDict_Update(mp, other) < 0) *************** *** 1682,1687 **** --- 1728,1737 ---- PyDoc_STRVAR(update__doc__, "D.update(E) -> None. Update D from E: for k in E.keys(): D[k] = E[k]"); + PyDoc_STRVAR(fromseq__doc__, + "dict.fromseq(S[,v]) -> New dict with keys from S and values equal to v.\n\ + v defaults to None."); + PyDoc_STRVAR(clear__doc__, "D.clear() -> None. Remove all items from D."); *************** *** 1716,1721 **** --- 1766,1773 ---- values__doc__}, {"update", (PyCFunction)dict_update, METH_O, update__doc__}, + {"fromseq", (PyCFunction)dict_fromseq, METH_VARARGS | METH_CLASS, + fromseq__doc__}, {"clear", (PyCFunction)dict_clear, METH_NOARGS, clear__doc__}, {"copy", (PyCFunction)dict_copy, METH_NOARGS, Index: Doc/lib/libstdtypes.tex =================================================================== RCS file: /cvsroot/python/python/dist/src/Doc/lib/libstdtypes.tex,v retrieving revision 1.110 diff -c -r1.110 libstdtypes.tex *** Doc/lib/libstdtypes.tex 16 Nov 2002 00:44:00 -0000 1.110 --- Doc/lib/libstdtypes.tex 26 Nov 2002 09:58:28 -0000 *************** *** 1055,1062 **** {(3)} \lineiii{\var{a}.keys()}{a copy of \var{a}'s list of keys}{(3)} \lineiii{\var{a}.update(\var{b})} ! {\code{for k in \var{b}.keys(): \var{a}[k] = \var{b}[k]}} {} \lineiii{\var{a}.values()}{a copy of \var{a}'s list of values}{(3)} \lineiii{\var{a}.get(\var{k}\optional{, \var{x}})} {\code{\var{a}[\var{k}]} if \code{\var{k} in \var{a}}, --- 1055,1065 ---- {(3)} \lineiii{\var{a}.keys()}{a copy of \var{a}'s list of keys}{(3)} \lineiii{\var{a}.update(\var{b})} ! {\code{for \var{k} in \var{b}.keys(): \var{a}[\var{k}] = \var{b}[\var{k}]}} {} + \lineiii{\var{a}.fromseq(\var{seq}\optional{, \var{value}})} + {Creates a new dictionary with keys from \var{seq} and values from \var{value}} + {(7)} \lineiii{\var{a}.values()}{a copy of \var{a}'s list of values}{(3)} \lineiii{\var{a}.get(\var{k}\optional{, \var{x}})} {\code{\var{a}[\var{k}]} if \code{\var{k} in \var{a}}, *************** *** 1114,1119 **** --- 1117,1126 ---- \item[(6)] \function{popitem()} is useful to destructively iterate over a dictionary, as often used in set algorithms. + \end{description} + + \item[(7)] \function{fromseq()} is a class method that returns a + new dictionary. \var{value} defaults to \code{None}. \versionadded{2.3} \end{description} Index: Lib/test/test_types.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/test/test_types.py,v retrieving revision 1.40 diff -c -r1.40 test_types.py *** Lib/test/test_types.py 19 Nov 2002 20:49:13 -0000 1.40 --- Lib/test/test_types.py 26 Nov 2002 09:58:28 -0000 *************** *** 530,535 **** --- 530,559 ---- try: d.update(FailingUserDict()) except ValueError: pass else: raise TestFailed, 'dict.update(), __getitem__ expected ValueError' + # dict.fromseq() + if dict.fromseq('abc') != {'a':None, 'b':None, 'c':None}: + raise TestFailed, 'dict.fromseq did work as a class method' + d = {} + if d.fromseq('abc') is d: + raise TestFailed, 'dict.fromseq did not return new dict' + if d.fromseq('abc') != {'a':None, 'b':None, 'c':None}: + raise TestFailed, 'dict.fromseq failed with default value' + if d.fromseq((4,5),0) != {4:0, 5:0}: + raise TestFailed, 'dict.fromseq failed with specified value' + if d.fromseq([]) != {}: + raise TestFailed, 'dict.fromseq failed with null sequence' + def g(): + yield 1 + if d.fromseq(g()) != {1:None}: + raise TestFailed, 'dict.fromseq failed with a generator' + try: {}.fromseq(3) + except TypeError: pass + else: raise TestFailed, 'dict.fromseq failed to raise TypeError' + class dictlike(dict): pass + if dictlike.fromseq('a') != {'a':None}: + raise TestFailed, 'dictsubclass.fromseq did not inherit' + if type(dictlike.fromseq('a')) is not dictlike: + raise TestFailed, 'dictsubclass.fromseq created wrong type' # dict.copy() d = {1:1, 2:2, 3:3} if d.copy() != {1:1, 2:2, 3:3}: raise TestFailed, 'dict copy'