diff -r a62072cf50a2 Modules/_collectionsmodule.c --- a/Modules/_collectionsmodule.c Wed Jan 22 12:26:01 2014 +0100 +++ b/Modules/_collectionsmodule.c Wed Jan 22 15:23:06 2014 +0200 @@ -7,6 +7,22 @@ All rights reserved. */ +/*[clinic input] +module collections +module _collections +class collections.deque +class collections.Counter +class _collections.dequeiter +class _collections.dequereviter +[clinic start generated code]*/ +/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + +/*[python input] +class dequeobject_converter(self_converter): + type = "dequeobject *" +[python start generated code]*/ +/*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + /* The block length may be set to any number over 1. Larger numbers * reduce the number of calls to the memory allocator, give faster * indexing and rotation, and reduce the link::data overhead ratio. @@ -1026,29 +1042,57 @@ return NULL; } +/*[clinic input] +collections.deque.__init__ as deque_init + + self: self(type="dequeobject *") + iterable: object = NULL + maxlen: Py_ssize_t(nullable=True) = None + +Build an ordered collection with optimized access from its endpoints. +[clinic start generated code]*/ + +PyDoc_STRVAR(deque_init__doc__, +"deque(iterable=None, maxlen=None)\n" +"Build an ordered collection with optimized access from its endpoints."); + static int -deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs) +deque_init_impl(dequeobject *self, PyObject *iterable, nullable_Py_ssize_t *maxlen); + +static int +deque_init(PyObject *self, PyObject *args, PyObject *kwargs) { + int return_value = -1; + static char *_keywords[] = {"iterable", "maxlen", NULL}; PyObject *iterable = NULL; - PyObject *maxlenobj = NULL; - Py_ssize_t maxlen = -1; - char *kwlist[] = {"iterable", "maxlen", 0}; + nullable_Py_ssize_t maxlen = NULLABLE_PY_SSIZE_T_INITIALIZE(0, 1); - if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj)) + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "|OO&:__init__", _keywords, + &iterable, _PyLong_NullablePy_ssize_tConverter, &maxlen)) + goto exit; + return_value = deque_init_impl((dequeobject *)self, iterable, &maxlen); + +exit: + return return_value; +} + +static int +deque_init_impl(dequeobject *self, PyObject *iterable, nullable_Py_ssize_t *maxlen) +/*[clinic end generated code: checksum=3b1ffeeee9a70ec1573dfebf813d3d9b13b68c68]*/ +{ + if (!maxlen->is_null && maxlen->i < 0) { + PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative"); return -1; - if (maxlenobj != NULL && maxlenobj != Py_None) { - maxlen = PyLong_AsSsize_t(maxlenobj); - if (maxlen == -1 && PyErr_Occurred()) - return -1; - if (maxlen < 0) { - PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative"); - return -1; - } } - deque->maxlen = maxlen; - deque_clear(deque); + + if (maxlen->is_null) + self->maxlen = -1; + else + self->maxlen = maxlen->i; + deque_clear(self); if (iterable != NULL) { - PyObject *rv = deque_extend(deque, iterable); + PyObject *rv = deque_extend(self, iterable); if (rv == NULL) return -1; Py_DECREF(rv); @@ -1142,11 +1186,6 @@ {NULL, NULL} /* sentinel */ }; -PyDoc_STRVAR(deque_doc, -"deque([iterable[, maxlen]]) --> deque object\n\ -\n\ -Build an ordered collection with optimized access from its endpoints."); - static PyTypeObject deque_type = { PyVarObject_HEAD_INIT(NULL, 0) "collections.deque", /* tp_name */ @@ -1170,7 +1209,7 @@ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */ - deque_doc, /* tp_doc */ + deque_init__doc__, /* tp_doc */ (traverseproc)deque_traverse, /* tp_traverse */ (inquiry)deque_clear, /* tp_clear */ (richcmpfunc)deque_richcompare, /* tp_richcompare */