Generalize dictionary() to accept a sequence of 2-sequences. At the
outer level, the iterator protocol is used for memory-efficiency (the
outer sequence may be very large if fully materialized); at the inner
level, PySequence_Fast() is used for time-efficiency (these should
always be sequences of length 2).
dictobject.c, new functions PyDict_{Merge,Update}FromSeq2. These are
wholly analogous to PyDict_{Merge,Update}, but process a sequence-of-2-
sequences argument instead of a mapping object. For now, I left these
functions file static, so no corresponding doc changes. It's tempting
to change dict.update() to allow a sequence-of-2-seqs argument too.
Also changed the name of dictionary's keyword argument from "mapping"
to "x". Got a better name? "mapping_or_sequence_of_pairs" isn't
attractive, although more so than "mosop" <wink>.
abstract.h, abstract.tex: Added new PySequence_Fast_GET_SIZE function,
much faster than going thru the all-purpose PySequence_Size.
libfuncs.tex:
- Document dictionary().
- Fiddle tuple() and list() to admit that their argument is optional.
- The long-winded repetitions of "a sequence, a container that supports
iteration, or an iterator object" is getting to be a PITA. Many
months ago I suggested factoring this out into "iterable object",
where the definition of that could include being explicit about
generators too (as is, I'm not sure a reader outside of PythonLabs
could guess that "an iterator object" includes a generator call).
- Please check my curly braces -- I'm going blind <0.9 wink>.
abstract.c, PySequence_Tuple(): When PyObject_GetIter() fails, leave
its error msg alone now (the msg it produces has improved since
PySequence_Tuple was generalized to accept iterable objects, and
PySequence_Tuple was also stomping on the msg in cases it shouldn't
have even before PyObject_GetIter grew a better msg).
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 829f76d..f901499 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -993,7 +993,89 @@
/* Update unconditionally replaces existing items.
Merge has a 3rd argument 'override'; if set, it acts like Update,
- otherwise it leaves existing items unchanged. */
+ otherwise it leaves existing items unchanged.
+
+ PyDict_{Update,Merge} update/merge from a mapping object.
+
+ PyDict_{Update,Merge}FromSeq2 update/merge from any iterable object
+ producing iterable objects of length 2.
+*/
+
+static int
+PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
+{
+ PyObject *it; /* iter(seq2) */
+ int i; /* index into seq2 of current element */
+ PyObject *item; /* seq2[i] */
+ PyObject *fast; /* item as a 2-tuple or 2-list */
+
+ assert(d != NULL);
+ assert(PyDict_Check(d));
+ assert(seq2 != NULL);
+
+ it = PyObject_GetIter(seq2);
+ if (it == NULL)
+ return -1;
+
+ for (i = 0; ; ++i) {
+ PyObject *key, *value;
+ int n;
+
+ fast = NULL;
+ item = PyIter_Next(it);
+ if (item == NULL) {
+ if (PyErr_Occurred())
+ goto Fail;
+ break;
+ }
+
+ /* Convert item to sequence, and verify length 2. */
+ fast = PySequence_Fast(item, "");
+ if (fast == NULL) {
+ if (PyErr_ExceptionMatches(PyExc_TypeError))
+ PyErr_Format(PyExc_TypeError,
+ "cannot convert dictionary update "
+ "sequence element #%d to a sequence",
+ i);
+ goto Fail;
+ }
+ n = PySequence_Fast_GET_SIZE(fast);
+ if (n != 2) {
+ PyErr_Format(PyExc_ValueError,
+ "dictionary update sequence element #%d "
+ "has length %d; 2 is required",
+ i, n);
+ goto Fail;
+ }
+
+ /* Update/merge with this (key, value) pair. */
+ key = PySequence_Fast_GET_ITEM(fast, 0);
+ value = PySequence_Fast_GET_ITEM(fast, 1);
+ if (override || PyDict_GetItem(d, key) == NULL) {
+ int status = PyDict_SetItem(d, key, value);
+ if (status < 0)
+ goto Fail;
+ }
+ Py_DECREF(fast);
+ Py_DECREF(item);
+ }
+
+ i = 0;
+ goto Return;
+Fail:
+ Py_XDECREF(item);
+ Py_XDECREF(fast);
+ i = -1;
+Return:
+ Py_DECREF(it);
+ return i;
+}
+
+static int
+PyDict_UpdateFromSeq2(PyObject *d, PyObject *seq2)
+{
+ return PyDict_MergeFromSeq2(d, seq2, 1);
+}
int
PyDict_Update(PyObject *a, PyObject *b)
@@ -1699,23 +1781,20 @@
dict_init(PyObject *self, PyObject *args, PyObject *kwds)
{
PyObject *arg = NULL;
- static char *kwlist[] = {"mapping", 0};
+ static char *kwlist[] = {"x", 0};
+ int result = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:dictionary",
kwlist, &arg))
- return -1;
- if (arg != NULL) {
- if (PyDict_Merge(self, arg, 1) < 0) {
- /* An error like "AttributeError: keys" is too
- cryptic in this context. */
- if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
- PyErr_SetString(PyExc_TypeError,
- "argument must be of a mapping type");
- }
- return -1;
- }
+ result = -1;
+
+ else if (arg != NULL) {
+ if (PyObject_HasAttrString(arg, "keys"))
+ result = PyDict_Merge(self, arg, 1);
+ else
+ result = PyDict_MergeFromSeq2(self, arg, 1);
}
- return 0;
+ return result;
}
static PyObject *
@@ -1725,8 +1804,15 @@
}
static char dictionary_doc[] =
-"dictionary() -> new empty dictionary\n"
-"dictionary(mapping) -> new dict initialized from mapping's key+value pairs";
+"dictionary() -> new empty dictionary.\n"
+"dictionary(mapping) -> new dict initialized from a mapping object's\n"
+" (key, value) pairs.\n"
+"dictionary(seq) -> new dict initialized from the 2-element elements of\n"
+" a sequence; for example, from mapping.items(). seq must be an\n"
+" iterable object, producing iterable objects each producing exactly\n"
+" two objects, the first of which is used as a key and the second as\n"
+" its value. If a given key is seen more than once, the dict retains\n"
+" the last value associated with it.";
PyTypeObject PyDict_Type = {
PyObject_HEAD_INIT(&PyType_Type)