[Python-checkins] CVS: python/dist/src/Objects abstract.c,2.86,2.87 dictobject.c,2.114,2.115
Tim Peters
tim_one@users.sourceforge.net
Thu, 25 Oct 2001 22:06:52 -0700
Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv13745/python/Objects
Modified Files:
abstract.c dictobject.c
Log Message:
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).
Index: abstract.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/abstract.c,v
retrieving revision 2.86
retrieving revision 2.87
diff -C2 -d -r2.86 -r2.87
*** abstract.c 2001/10/22 04:12:44 2.86
--- abstract.c 2001/10/26 05:06:50 2.87
***************
*** 1279,1283 ****
it = PyObject_GetIter(v);
if (it == NULL)
! return type_error("tuple() argument must support iteration");
/* Guess result size and allocate space. */
--- 1279,1283 ----
it = PyObject_GetIter(v);
if (it == NULL)
! return NULL;
/* Guess result size and allocate space. */
Index: dictobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/dictobject.c,v
retrieving revision 2.114
retrieving revision 2.115
diff -C2 -d -r2.114 -r2.115
*** dictobject.c 2001/10/05 20:51:38 2.114
--- dictobject.c 2001/10/26 05:06:50 2.115
***************
*** 994,999 ****
/* Update unconditionally replaces existing items.
Merge has a 3rd argument 'override'; if set, it acts like Update,
! otherwise it leaves existing items unchanged. */
int
PyDict_Update(PyObject *a, PyObject *b)
--- 994,1081 ----
/* Update unconditionally replaces existing items.
Merge has a 3rd argument 'override'; if set, it acts like Update,
! 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)
***************
*** 1700,1720 ****
{
PyObject *arg = NULL;
! static char *kwlist[] = {"mapping", 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;
! }
}
! return 0;
}
--- 1782,1799 ----
{
PyObject *arg = NULL;
! static char *kwlist[] = {"x", 0};
! int result = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:dictionary",
kwlist, &arg))
! 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 result;
}
***************
*** 1726,1731 ****
static char dictionary_doc[] =
! "dictionary() -> new empty dictionary\n"
! "dictionary(mapping) -> new dict initialized from mapping's key+value pairs";
PyTypeObject PyDict_Type = {
--- 1805,1817 ----
static char dictionary_doc[] =
! "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 = {