cinap_lenrekadd hg and python4bb9e2215 years agoHistory
2550 lines · 57.3 KB · C
RawDownload
	PyObject_GC_Track(newto);	return (PyObject *)newto;} PyDoc_STRVAR(teecopy_doc, "Returns an independent iterator."); static PyObject *tee_fromiterable(PyObject *iterable){	teeobject *to;	PyObject *it = NULL; 	it = PyObject_GetIter(iterable);	if (it == NULL)		return NULL;	if (PyObject_TypeCheck(it, &tee_type)) {		to = (teeobject *)tee_copy((teeobject *)it);		goto done;	} 	to = PyObject_GC_New(teeobject, &tee_type);	if (to == NULL) 		goto done;	to->dataobj = (teedataobject *)teedataobject_new(it);	if (!to->dataobj) {		PyObject_GC_Del(to);		to = NULL;		goto done;	} 	to->index = 0;	to->weakreflist = NULL;	PyObject_GC_Track(to);done:	Py_XDECREF(it);	return (PyObject *)to;} static PyObject *tee_new(PyTypeObject *type, PyObject *args, PyObject *kw){	PyObject *iterable; 	if (!PyArg_UnpackTuple(args, "tee", 1, 1, &iterable))		return NULL;	return tee_fromiterable(iterable);} static inttee_clear(teeobject *to){	if (to->weakreflist != NULL)		PyObject_ClearWeakRefs((PyObject *) to);	Py_CLEAR(to->dataobj);	return 0;} static voidtee_dealloc(teeobject *to){	PyObject_GC_UnTrack(to);	tee_clear(to);	PyObject_GC_Del(to);} PyDoc_STRVAR(teeobject_doc,"Iterator wrapped to make it copyable"); static PyMethodDef tee_methods[] = {	{"__copy__",	(PyCFunction)tee_copy,	METH_NOARGS, teecopy_doc}, 	{NULL,		NULL}		/* sentinel */}; static PyTypeObject tee_type = {	PyObject_HEAD_INIT(NULL)	0,				/* ob_size */	"itertools.tee",		/* tp_name */	sizeof(teeobject),		/* tp_basicsize */	0,				/* tp_itemsize */	/* methods */	(destructor)tee_dealloc,	/* tp_dealloc */	0,				/* tp_print */	0,				/* tp_getattr */	0,				/* tp_setattr */	0,				/* tp_compare */	0,				/* tp_repr */	0,				/* tp_as_number */	0,				/* tp_as_sequence */	0,				/* tp_as_mapping */	0,				/* tp_hash */	0,				/* tp_call */	0,				/* tp_str */	0,				/* tp_getattro */	0,				/* tp_setattro */	0,				/* tp_as_buffer */	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,	/* tp_flags */	teeobject_doc,			/* tp_doc */	(traverseproc)tee_traverse,	/* tp_traverse */	(inquiry)tee_clear,		/* tp_clear */	0,				/* tp_richcompare */	offsetof(teeobject, weakreflist),	/* tp_weaklistoffset */	PyObject_SelfIter,		/* tp_iter */	(iternextfunc)tee_next,		/* tp_iternext */	tee_methods,			/* tp_methods */	0,				/* tp_members */	0,				/* tp_getset */	0,				/* tp_base */	0,				/* tp_dict */	0,				/* tp_descr_get */	0,				/* tp_descr_set */	0,				/* tp_dictoffset */	0,				/* tp_init */	0,				/* tp_alloc */	tee_new,			/* tp_new */	PyObject_GC_Del,		/* tp_free */}; static PyObject *tee(PyObject *self, PyObject *args){	Py_ssize_t i, n=2;	PyObject *it, *iterable, *copyable, *result; 	if (!PyArg_ParseTuple(args, "O|n", &iterable, &n))		return NULL;	if (n < 0) {		PyErr_SetString(PyExc_ValueError, "n must be >= 0");		return NULL;	}	result = PyTuple_New(n);	if (result == NULL)		return NULL;	if (n == 0)		return result;	it = PyObject_GetIter(iterable);	if (it == NULL) {		Py_DECREF(result);		return NULL;	}	if (!PyObject_HasAttrString(it, "__copy__")) {		copyable = tee_fromiterable(it);		Py_DECREF(it);		if (copyable == NULL) {			Py_DECREF(result);			return NULL;		}	} else		copyable = it;	PyTuple_SET_ITEM(result, 0, copyable);	for (i=1 ; i<n ; i++) {		copyable = PyObject_CallMethod(copyable, "__copy__", NULL);		if (copyable == NULL) {			Py_DECREF(result);			return NULL;		}		PyTuple_SET_ITEM(result, i, copyable);	}	return result;} PyDoc_STRVAR(tee_doc,"tee(iterable, n=2) --> tuple of n independent iterators.");  /* cycle object **********************************************************/ typedef struct {	PyObject_HEAD	PyObject *it;	PyObject *saved;	int firstpass;} cycleobject; static PyTypeObject cycle_type; static PyObject *cycle_new(PyTypeObject *type, PyObject *args, PyObject *kwds){	PyObject *it;	PyObject *iterable;	PyObject *saved;	cycleobject *lz; 	if (type == &cycle_type && !_PyArg_NoKeywords("cycle()", kwds))		return NULL; 	if (!PyArg_UnpackTuple(args, "cycle", 1, 1, &iterable))		return NULL; 	/* Get iterator. */	it = PyObject_GetIter(iterable);	if (it == NULL)		return NULL; 	saved = PyList_New(0);	if (saved == NULL) {		Py_DECREF(it);		return NULL;	} 	/* create cycleobject structure */	lz = (cycleobject *)type->tp_alloc(type, 0);	if (lz == NULL) {		Py_DECREF(it);		Py_DECREF(saved);		return NULL;	}	lz->it = it;	lz->saved = saved;	lz->firstpass = 0; 	return (PyObject *)lz;} static voidcycle_dealloc(cycleobject *lz){	PyObject_GC_UnTrack(lz);	Py_XDECREF(lz->saved);	Py_XDECREF(lz->it);	lz->ob_type->tp_free(lz);} static intcycle_traverse(cycleobject *lz, visitproc visit, void *arg){	Py_VISIT(lz->it);	Py_VISIT(lz->saved);	return 0;} static PyObject *cycle_next(cycleobject *lz){	PyObject *item;	PyObject *it;	PyObject *tmp; 	while (1) {		item = PyIter_Next(lz->it);		if (item != NULL) {			if (!lz->firstpass)				PyList_Append(lz->saved, item);			return item;		}		if (PyErr_Occurred()) {			if (PyErr_ExceptionMatches(PyExc_StopIteration))				PyErr_Clear();			else				return NULL;		}		if (PyList_Size(lz->saved) == 0) 			return NULL;		it = PyObject_GetIter(lz->saved);		if (it == NULL)			return NULL;		tmp = lz->it;		lz->it = it;		lz->firstpass = 1;		Py_DECREF(tmp);	}} PyDoc_STRVAR(cycle_doc,"cycle(iterable) --> cycle object\n\\n\Return elements from the iterable until it is exhausted.\n\Then repeat the sequence indefinitely."); static PyTypeObject cycle_type = {	PyObject_HEAD_INIT(NULL)	0,				/* ob_size */	"itertools.cycle",		/* tp_name */	sizeof(cycleobject),		/* tp_basicsize */	0,				/* tp_itemsize */	/* methods */	(destructor)cycle_dealloc,	/* tp_dealloc */	0,				/* tp_print */	0,				/* tp_getattr */	0,				/* tp_setattr */	0,				/* tp_compare */	0,				/* tp_repr */	0,				/* tp_as_number */	0,				/* tp_as_sequence */	0,				/* tp_as_mapping */	0,				/* tp_hash */	0,				/* tp_call */	0,				/* tp_str */	PyObject_GenericGetAttr,	/* tp_getattro */	0,				/* tp_setattro */	0,				/* tp_as_buffer */	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |		Py_TPFLAGS_BASETYPE,	/* tp_flags */	cycle_doc,			/* tp_doc */	(traverseproc)cycle_traverse,	/* tp_traverse */	0,				/* tp_clear */	0,				/* tp_richcompare */	0,				/* tp_weaklistoffset */	PyObject_SelfIter,		/* tp_iter */	(iternextfunc)cycle_next,	/* tp_iternext */	0,				/* tp_methods */	0,				/* tp_members */	0,				/* tp_getset */	0,				/* tp_base */	0,				/* tp_dict */	0,				/* tp_descr_get */	0,				/* tp_descr_set */	0,				/* tp_dictoffset */	0,				/* tp_init */	0,				/* tp_alloc */	cycle_new,			/* tp_new */	PyObject_GC_Del,		/* tp_free */};  /* dropwhile object **********************************************************/ typedef struct {	PyObject_HEAD	PyObject *func;	PyObject *it;	long	 start;} dropwhileobject; static PyTypeObject dropwhile_type; static PyObject *dropwhile_new(PyTypeObject *type, PyObject *args, PyObject *kwds){	PyObject *func, *seq;	PyObject *it;	dropwhileobject *lz; 	if (type == &dropwhile_type && !_PyArg_NoKeywords("dropwhile()", kwds))		return NULL; 	if (!PyArg_UnpackTuple(args, "dropwhile", 2, 2, &func, &seq))		return NULL; 	/* Get iterator. */	it = PyObject_GetIter(seq);	if (it == NULL)		return NULL; 	/* create dropwhileobject structure */	lz = (dropwhileobject *)type->tp_alloc(type, 0);	if (lz == NULL) {		Py_DECREF(it);		return NULL;	}	Py_INCREF(func);	lz->func = func;	lz->it = it;	lz->start = 0; 	return (PyObject *)lz;} static voiddropwhile_dealloc(dropwhileobject *lz){	PyObject_GC_UnTrack(lz);	Py_XDECREF(lz->func);	Py_XDECREF(lz->it);	lz->ob_type->tp_free(lz);} static intdropwhile_traverse(dropwhileobject *lz, visitproc visit, void *arg){	Py_VISIT(lz->it);	Py_VISIT(lz->func);	return 0;} static PyObject *dropwhile_next(dropwhileobject *lz){	PyObject *item, *good;	PyObject *it = lz->it;	long ok;	PyObject *(*iternext)(PyObject *); 	assert(PyIter_Check(it));	iternext = *it->ob_type->tp_iternext;	for (;;) {		item = iternext(it);		if (item == NULL)			return NULL;		if (lz->start == 1)			return item; 		good = PyObject_CallFunctionObjArgs(lz->func, item, NULL);		if (good == NULL) {			Py_DECREF(item);			return NULL;		}		ok = PyObject_IsTrue(good);		Py_DECREF(good);		if (!ok) {			lz->start = 1;			return item;		}		Py_DECREF(item);	}} PyDoc_STRVAR(dropwhile_doc,"dropwhile(predicate, iterable) --> dropwhile object\n\\n\Drop items from the iterable while predicate(item) is true.\n\Afterwards, return every element until the iterable is exhausted."); static PyTypeObject dropwhile_type = {	PyObject_HEAD_INIT(NULL)	0,				/* ob_size */	"itertools.dropwhile",		/* tp_name */	sizeof(dropwhileobject),	/* tp_basicsize */	0,				/* tp_itemsize */	/* methods */	(destructor)dropwhile_dealloc,	/* tp_dealloc */	0,				/* tp_print */	0,				/* tp_getattr */	0,				/* tp_setattr */	0,				/* tp_compare */	0,				/* tp_repr */	0,				/* tp_as_number */	0,				/* tp_as_sequence */	0,				/* tp_as_mapping */	0,				/* tp_hash */	0,				/* tp_call */	0,				/* tp_str */	PyObject_GenericGetAttr,	/* tp_getattro */	0,				/* tp_setattro */	0,				/* tp_as_buffer */	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |		Py_TPFLAGS_BASETYPE,	/* tp_flags */	dropwhile_doc,			/* tp_doc */	(traverseproc)dropwhile_traverse,    /* tp_traverse */	0,				/* tp_clear */	0,				/* tp_richcompare */	0,				/* tp_weaklistoffset */	PyObject_SelfIter,		/* tp_iter */	(iternextfunc)dropwhile_next,	/* tp_iternext */	0,				/* tp_methods */	0,				/* tp_members */	0,				/* tp_getset */	0,				/* tp_base */	0,				/* tp_dict */	0,				/* tp_descr_get */	0,				/* tp_descr_set */	0,				/* tp_dictoffset */	0,				/* tp_init */	0,				/* tp_alloc */	dropwhile_new,			/* tp_new */	PyObject_GC_Del,		/* tp_free */};  /* takewhile object **********************************************************/ typedef struct {	PyObject_HEAD	PyObject *func;	PyObject *it;	long	 stop;} takewhileobject; static PyTypeObject takewhile_type; static PyObject *takewhile_new(PyTypeObject *type, PyObject *args, PyObject *kwds){	PyObject *func, *seq;	PyObject *it;	takewhileobject *lz; 	if (type == &takewhile_type && !_PyArg_NoKeywords("takewhile()", kwds))		return NULL; 	if (!PyArg_UnpackTuple(args, "takewhile", 2, 2, &func, &seq))		return NULL; 	/* Get iterator. */	it = PyObject_GetIter(seq);	if (it == NULL)		return NULL; 	/* create takewhileobject structure */	lz = (takewhileobject *)type->tp_alloc(type, 0);	if (lz == NULL) {		Py_DECREF(it);		return NULL;	}	Py_INCREF(func);	lz->func = func;	lz->it = it;	lz->stop = 0; 	return (PyObject *)lz;

Lines 501–1000 of 2550. Previous lines