@@ -24,6 +24,9 @@ const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
2424
2525/* Forward */
2626static PyObject * filterstring (PyObject * , PyObject * );
27+ #ifdef Py_USING_UNICODE
28+ static PyObject * filterunicode (PyObject * , PyObject * );
29+ #endif
2730static PyObject * filtertuple (PyObject * , PyObject * );
2831
2932static PyObject *
@@ -132,6 +135,10 @@ builtin_filter(PyObject *self, PyObject *args)
132135 /* Strings and tuples return a result of the same type. */
133136 if (PyString_Check (seq ))
134137 return filterstring (func , seq );
138+ #ifdef Py_USING_UNICODE
139+ if (PyUnicode_Check (seq ))
140+ return filterunicode (func , seq );
141+ #endif
135142 if (PyTuple_Check (seq ))
136143 return filtertuple (func , seq );
137144
@@ -1926,3 +1933,58 @@ filterstring(PyObject *func, PyObject *strobj)
19261933 Py_DECREF (result );
19271934 return NULL ;
19281935}
1936+
1937+ #ifdef Py_USING_UNICODE
1938+ /* Helper for filter(): filter a Unicode object through a function */
1939+
1940+ static PyObject *
1941+ filterunicode (PyObject * func , PyObject * strobj )
1942+ {
1943+ PyObject * result ;
1944+ register int i , j ;
1945+ int len = PyUnicode_GetSize (strobj );
1946+
1947+ if (func == Py_None ) {
1948+ /* No character is ever false -- share input string */
1949+ Py_INCREF (strobj );
1950+ return strobj ;
1951+ }
1952+ if ((result = PyUnicode_FromUnicode (NULL , len )) == NULL )
1953+ return NULL ;
1954+
1955+ for (i = j = 0 ; i < len ; ++ i ) {
1956+ PyObject * item , * arg , * good ;
1957+ int ok ;
1958+
1959+ item = (* strobj -> ob_type -> tp_as_sequence -> sq_item )(strobj , i );
1960+ if (item == NULL )
1961+ goto Fail_1 ;
1962+ arg = Py_BuildValue ("(O)" , item );
1963+ if (arg == NULL ) {
1964+ Py_DECREF (item );
1965+ goto Fail_1 ;
1966+ }
1967+ good = PyEval_CallObject (func , arg );
1968+ Py_DECREF (arg );
1969+ if (good == NULL ) {
1970+ Py_DECREF (item );
1971+ goto Fail_1 ;
1972+ }
1973+ ok = PyObject_IsTrue (good );
1974+ Py_DECREF (good );
1975+ if (ok )
1976+ PyUnicode_AS_UNICODE ((PyStringObject * )result )[j ++ ] =
1977+ PyUnicode_AS_UNICODE ((PyStringObject * )item )[0 ];
1978+ Py_DECREF (item );
1979+ }
1980+
1981+ if (j < len )
1982+ PyUnicode_Resize (& result , j );
1983+
1984+ return result ;
1985+
1986+ Fail_1 :
1987+ Py_DECREF (result );
1988+ return NULL ;
1989+ }
1990+ #endif
0 commit comments