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
c3da83fc
Commit
c3da83fc
authored
Feb 04, 2003
by
Walter Dörwald
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make sure filter() never returns tuple, str or unicode
subclasses. (Discussed in SF patch #665835)
parent
29273c87
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
5 deletions
+45
-5
Lib/test/test_builtin.py
Lib/test/test_builtin.py
+23
-0
Python/bltinmodule.c
Python/bltinmodule.c
+22
-5
No files found.
Lib/test/test_builtin.py
View file @
c3da83fc
...
...
@@ -408,6 +408,29 @@ class BuiltinTest(unittest.TestCase):
unicode
(
"345"
)
)
def
test_filter_subclasses
(
self
):
# test, that filter() never returns tuple, str or unicode subclasses
funcs
=
(
None
,
lambda
x
:
True
)
class
tuple2
(
tuple
):
pass
class
str2
(
str
):
pass
inputs
=
{
tuple2
:
[(),
(
1
,
2
,
3
)],
str2
:
[
""
,
"123"
]
}
if
have_unicode
:
class
unicode2
(
unicode
):
pass
inputs
[
unicode2
]
=
[
unicode
(),
unicode
(
"123"
)]
for
func
in
funcs
:
for
(
cls
,
inps
)
in
inputs
.
iteritems
():
for
inp
in
inps
:
out
=
filter
(
func
,
cls
(
inp
))
self
.
assertEqual
(
inp
,
out
)
self
.
assert_
(
not
isinstance
(
out
,
cls
))
def
test_float
(
self
):
self
.
assertEqual
(
float
(
3.14
),
3.14
)
self
.
assertEqual
(
float
(
314
),
314.0
)
...
...
Python/bltinmodule.c
View file @
c3da83fc
...
...
@@ -1838,7 +1838,10 @@ filtertuple(PyObject *func, PyObject *tuple)
int
len
=
PyTuple_Size
(
tuple
);
if
(
len
==
0
)
{
Py_INCREF
(
tuple
);
if
(
PyTuple_CheckExact
(
tuple
))
Py_INCREF
(
tuple
);
else
tuple
=
PyTuple_New
(
0
);
return
tuple
;
}
...
...
@@ -1895,8 +1898,15 @@ filterstring(PyObject *func, PyObject *strobj)
int
outlen
=
len
;
if
(
func
==
Py_None
)
{
/* No character is ever false -- share input string */
Py_INCREF
(
strobj
);
/* No character is ever false -- share input string
* (if it's not a subclass) */
if
(
PyString_CheckExact
(
strobj
))
Py_INCREF
(
strobj
);
else
strobj
=
PyString_FromStringAndSize
(
PyString_AS_STRING
(
strobj
),
len
);
return
strobj
;
}
if
((
result
=
PyString_FromStringAndSize
(
NULL
,
len
))
==
NULL
)
...
...
@@ -1980,8 +1990,15 @@ filterunicode(PyObject *func, PyObject *strobj)
int
outlen
=
len
;
if
(
func
==
Py_None
)
{
/* No character is ever false -- share input string */
Py_INCREF
(
strobj
);
/* No character is ever false -- share input string
* (it if's not a subclass) */
if
(
PyUnicode_CheckExact
(
strobj
))
Py_INCREF
(
strobj
);
else
strobj
=
PyUnicode_FromUnicode
(
PyUnicode_AS_UNICODE
(
strobj
),
len
);
return
strobj
;
}
if
((
result
=
PyUnicode_FromUnicode
(
NULL
,
len
))
==
NULL
)
...
...
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