diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2003-01-25 22:46:11 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2003-01-25 22:46:11 (GMT) |
commit | 8afd7571a18593bef57d98ccd68865fd1d400643 (patch) | |
tree | ef091620849b0a497568b274dcf59bdbf266ed3b /Python | |
parent | fc03a94aaccc9ae606ee3438ca21d65a335d43e9 (diff) | |
download | cpython-8afd7571a18593bef57d98ccd68865fd1d400643.zip cpython-8afd7571a18593bef57d98ccd68865fd1d400643.tar.gz cpython-8afd7571a18593bef57d98ccd68865fd1d400643.tar.bz2 |
Patch #636005: Filter unicode into unicode.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/bltinmodule.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 45ea4dc..54a9afd 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -24,6 +24,9 @@ const char *Py_FileSystemDefaultEncoding = NULL; /* use default */ /* Forward */ static PyObject *filterstring(PyObject *, PyObject *); +#ifdef Py_USING_UNICODE +static PyObject *filterunicode(PyObject *, PyObject *); +#endif static PyObject *filtertuple (PyObject *, PyObject *); static PyObject * @@ -132,6 +135,10 @@ builtin_filter(PyObject *self, PyObject *args) /* Strings and tuples return a result of the same type. */ if (PyString_Check(seq)) return filterstring(func, seq); +#ifdef Py_USING_UNICODE + if (PyUnicode_Check(seq)) + return filterunicode(func, seq); +#endif if (PyTuple_Check(seq)) return filtertuple(func, seq); @@ -1926,3 +1933,58 @@ 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 int i, j; + int len = PyUnicode_GetSize(strobj); + + if (func == Py_None) { + /* No character is ever false -- share input string */ + 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; + arg = Py_BuildValue("(O)", 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) + PyUnicode_AS_UNICODE((PyStringObject *)result)[j++] = + PyUnicode_AS_UNICODE((PyStringObject *)item)[0]; + Py_DECREF(item); + } + + if (j < len) + PyUnicode_Resize(&result, j); + + return result; + +Fail_1: + Py_DECREF(result); + return NULL; +} +#endif |