Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
2fa9d99b
Commit
2fa9d99b
authored
Jan 25, 2003
by
Martin v. Löwis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Patch #636005: Filter unicode into unicode.
parent
b13b5ec9
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
70 additions
and
0 deletions
+70
-0
Lib/test/test_builtin.py
Lib/test/test_builtin.py
+6
-0
Misc/NEWS
Misc/NEWS
+2
-0
Python/bltinmodule.c
Python/bltinmodule.c
+62
-0
No files found.
Lib/test/test_builtin.py
View file @
2fa9d99b
...
...
@@ -365,6 +365,12 @@ class BuiltinTest(unittest.TestCase):
def
__getitem__
(
self
,
index
):
raise
ValueError
self
.
assertRaises
(
ValueError
,
filter
,
lambda
x
:
x
>=
"3"
,
badstr
(
"1234"
))
if
have_unicode
:
# test biltinmodule.c::filterstring()
self
.
assertEqual
(
filter
(
None
,
unicode
(
"12"
)),
unicode
(
"12"
))
self
.
assertEqual
(
filter
(
lambda
x
:
x
>=
"3"
,
unicode
(
"1234"
)),
unicode
(
"34"
))
self
.
assertRaises
(
TypeError
,
filter
,
42
,
unicode
(
"12"
))
self
.
assertRaises
(
ValueError
,
filter
,
lambda
x
:
x
>=
"3"
,
badstr
(
unicode
(
"1234"
)))
def
test_float
(
self
):
self
.
assertEqual
(
float
(
3.14
),
3.14
)
...
...
Misc/NEWS
View file @
2fa9d99b
...
...
@@ -12,6 +12,8 @@ What's New in Python 2.3 alpha 2?
Core and builtins
-----------------
- filter returns now Unicode results for Unicode arguments.
- raw_input can now return Unicode objects.
- List objects'
sort
()
method
now
accepts
None
as
the
comparison
function
.
...
...
Python/bltinmodule.c
View file @
2fa9d99b
...
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment