mirror of
				https://github.com/python/cpython.git
				synced 2025-10-28 20:25:04 +00:00 
			
		
		
		
	Add two new functions, any() and all().
This commit is contained in:
		
							parent
							
								
									26e512a04f
								
							
						
					
					
						commit
						96229b1918
					
				
					 4 changed files with 129 additions and 0 deletions
				
			
		|  | @ -68,6 +68,69 @@ PyDoc_STRVAR(abs_doc, | |||
| \n\ | ||||
| Return the absolute value of the argument."); | ||||
| 
 | ||||
| static PyObject * | ||||
| builtin_all(PyObject *self, PyObject *v) | ||||
| { | ||||
| 	PyObject *it, *item; | ||||
| 
 | ||||
| 	it = PyObject_GetIter(v); | ||||
| 	if (it == NULL) | ||||
| 		return NULL; | ||||
| 
 | ||||
| 	while ((item = PyIter_Next(it)) != NULL) { | ||||
| 		int cmp = PyObject_IsTrue(item); | ||||
| 		Py_DECREF(item); | ||||
| 		if (cmp < 0) { | ||||
| 			Py_DECREF(it); | ||||
| 			return NULL; | ||||
| 		} | ||||
| 		if (cmp == 0) { | ||||
| 			Py_DECREF(it); | ||||
| 			Py_RETURN_FALSE; | ||||
| 		} | ||||
| 	} | ||||
| 	Py_DECREF(it); | ||||
| 	if (PyErr_Occurred()) | ||||
| 		return NULL; | ||||
| 	Py_RETURN_TRUE; | ||||
| } | ||||
| 
 | ||||
| PyDoc_STRVAR(all_doc, | ||||
| "all(iterable) -> bool\n\
 | ||||
| \n\ | ||||
| Return True if bool(x) is True for all values x in the iterable."); | ||||
| 
 | ||||
| static PyObject * | ||||
| builtin_any(PyObject *self, PyObject *v) | ||||
| { | ||||
| 	PyObject *it, *item; | ||||
| 
 | ||||
| 	it = PyObject_GetIter(v); | ||||
| 	if (it == NULL) | ||||
| 		return NULL; | ||||
| 
 | ||||
| 	while ((item = PyIter_Next(it)) != NULL) { | ||||
| 		int cmp = PyObject_IsTrue(item); | ||||
| 		Py_DECREF(item); | ||||
| 		if (cmp < 0) { | ||||
| 			Py_DECREF(it); | ||||
| 			return NULL; | ||||
| 		} | ||||
| 		if (cmp == 1) { | ||||
| 			Py_DECREF(it); | ||||
| 			Py_RETURN_TRUE; | ||||
| 		} | ||||
| 	} | ||||
| 	Py_DECREF(it); | ||||
| 	if (PyErr_Occurred()) | ||||
| 		return NULL; | ||||
| 	Py_RETURN_FALSE; | ||||
| } | ||||
| 
 | ||||
| PyDoc_STRVAR(any_doc, | ||||
| "any(iterable) -> bool\n\
 | ||||
| \n\ | ||||
| Return True if bool(x) is True for any x in the iterable."); | ||||
| 
 | ||||
| static PyObject * | ||||
| builtin_apply(PyObject *self, PyObject *args) | ||||
|  | @ -2125,6 +2188,8 @@ in length to the length of the shortest argument sequence."); | |||
| static PyMethodDef builtin_methods[] = { | ||||
|  	{"__import__",	builtin___import__, METH_VARARGS, import_doc}, | ||||
|  	{"abs",		builtin_abs,        METH_O, abs_doc}, | ||||
|  	{"all",		builtin_all,        METH_O, all_doc}, | ||||
|  	{"any",		builtin_any,        METH_O, any_doc}, | ||||
|  	{"apply",	builtin_apply,      METH_VARARGS, apply_doc}, | ||||
|  	{"callable",	builtin_callable,   METH_O, callable_doc}, | ||||
|  	{"chr",		builtin_chr,        METH_VARARGS, chr_doc}, | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Raymond Hettinger
						Raymond Hettinger