return NULL; } outlen = need; } memcpy( PyString_AS_STRING(result) + j, PyString_AS_STRING(item), reslen ); j += reslen; } } Py_DECREF(item); } if (j < outlen) _PyString_Resize(&result, j); return result; Fail_1: Py_DECREF(result); return NULL;} #ifdef Py_USING_UNICODE/* Helper for filter(): filter a Unicode object through a function */ static PyObject *filterunicode(PyObject *func, PyObject *strobj){ PyObject *result; register Py_ssize_t i, j; Py_ssize_t len = PyUnicode_GetSize(strobj); Py_ssize_t outlen = len; if (func == Py_None) { /* If it's a real string we can return the original, * as no character is ever false and __getitem__ * does return this character. If it's a subclass * we must go through the __getitem__ loop */ if (PyUnicode_CheckExact(strobj)) { Py_INCREF(strobj); return strobj; } } if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL) return NULL; for (i = j = 0; i < len; ++i) { PyObject *item, *arg, *good; int ok; item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i); if (item == NULL) goto Fail_1; if (func == Py_None) { ok = 1; } else { arg = PyTuple_Pack(1, item); if (arg == NULL) { Py_DECREF(item); goto Fail_1; } good = PyEval_CallObject(func, arg); Py_DECREF(arg); if (good == NULL) { Py_DECREF(item); goto Fail_1; } ok = PyObject_IsTrue(good); Py_DECREF(good); } if (ok) { Py_ssize_t reslen; if (!PyUnicode_Check(item)) { PyErr_SetString(PyExc_TypeError, "can't filter unicode to unicode:" " __getitem__ returned different type"); Py_DECREF(item); goto Fail_1; } reslen = PyUnicode_GET_SIZE(item); if (reslen == 1) PyUnicode_AS_UNICODE(result)[j++] = PyUnicode_AS_UNICODE(item)[0]; else { /* do we need more space? */ Py_ssize_t need = j + reslen + len - i - 1; if (need > outlen) { /* overallocate, to avoid reallocations */ if (need < 2 * outlen) need = 2 * outlen; if (PyUnicode_Resize( &result, need) < 0) { Py_DECREF(item); goto Fail_1; } outlen = need; } memcpy(PyUnicode_AS_UNICODE(result) + j, PyUnicode_AS_UNICODE(item), reslen*sizeof(Py_UNICODE)); j += reslen; } } Py_DECREF(item); } if (j < outlen) PyUnicode_Resize(&result, j); return result; Fail_1: Py_DECREF(result); return NULL;}#endifLines 2501–2620 of 2620. Previous lines
return NULL; } outlen = need; } memcpy( PyString_AS_STRING(result) + j, PyString_AS_STRING(item), reslen ); j += reslen; } } Py_DECREF(item); } if (j < outlen) _PyString_Resize(&result, j); return result; Fail_1: Py_DECREF(result); return NULL; } #ifdef Py_USING_UNICODE /* Helper for filter(): filter a Unicode object through a function */ static PyObject * filterunicode(PyObject *func, PyObject *strobj) { PyObject *result; register Py_ssize_t i, j; Py_ssize_t len = PyUnicode_GetSize(strobj); Py_ssize_t outlen = len; if (func == Py_None) { /* If it's a real string we can return the original, * as no character is ever false and __getitem__ * does return this character. If it's a subclass * we must go through the __getitem__ loop */ if (PyUnicode_CheckExact(strobj)) { Py_INCREF(strobj); return strobj; } } if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL) return NULL; for (i = j = 0; i < len; ++i) { PyObject *item, *arg, *good; int ok; item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i); if (item == NULL) goto Fail_1; if (func == Py_None) { ok = 1; } else { arg = PyTuple_Pack(1, item); if (arg == NULL) { Py_DECREF(item); goto Fail_1; } good = PyEval_CallObject(func, arg); Py_DECREF(arg); if (good == NULL) { Py_DECREF(item); goto Fail_1; } ok = PyObject_IsTrue(good); Py_DECREF(good); } if (ok) { Py_ssize_t reslen; if (!PyUnicode_Check(item)) { PyErr_SetString(PyExc_TypeError, "can't filter unicode to unicode:" " __getitem__ returned different type"); Py_DECREF(item); goto Fail_1; } reslen = PyUnicode_GET_SIZE(item); if (reslen == 1) PyUnicode_AS_UNICODE(result)[j++] = PyUnicode_AS_UNICODE(item)[0]; else { /* do we need more space? */ Py_ssize_t need = j + reslen + len - i - 1; if (need > outlen) { /* overallocate, to avoid reallocations */ if (need < 2 * outlen) need = 2 * outlen; if (PyUnicode_Resize( &result, need) < 0) { Py_DECREF(item); goto Fail_1; } outlen = need; } memcpy(PyUnicode_AS_UNICODE(result) + j, PyUnicode_AS_UNICODE(item), reslen*sizeof(Py_UNICODE)); j += reslen; } } Py_DECREF(item); } if (j < outlen) PyUnicode_Resize(&result, j); return result; Fail_1: Py_DECREF(result); return NULL; } #endif