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
ee1ae7cc
Commit
ee1ae7cc
authored
Feb 08, 2009
by
Benjamin Peterson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix len() when __len__() returns a non number type #5137
parent
c7055a59
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
16 additions
and
1 deletion
+16
-1
Lib/test/test_builtin.py
Lib/test/test_builtin.py
+12
-0
Misc/NEWS
Misc/NEWS
+3
-0
Objects/typeobject.c
Objects/typeobject.c
+1
-1
No files found.
Lib/test/test_builtin.py
View file @
ee1ae7cc
...
@@ -611,6 +611,18 @@ class BuiltinTest(unittest.TestCase):
...
@@ -611,6 +611,18 @@ class BuiltinTest(unittest.TestCase):
def
__len__
(
self
):
def
__len__
(
self
):
raise
ValueError
raise
ValueError
self
.
assertRaises
(
ValueError
,
len
,
BadSeq
())
self
.
assertRaises
(
ValueError
,
len
,
BadSeq
())
class
InvalidLen
:
def
__len__
(
self
):
return
None
self
.
assertRaises
(
TypeError
,
len
,
InvalidLen
())
class
FloatLen
:
def
__len__
(
self
):
return
4.5
self
.
assertRaises
(
TypeError
,
len
,
FloatLen
())
class
HugeLen
:
def
__len__
(
self
):
return
sys
.
maxsize
+
1
self
.
assertRaises
(
OverflowError
,
len
,
HugeLen
())
def
test_map
(
self
):
def
test_map
(
self
):
self
.
assertEqual
(
self
.
assertEqual
(
...
...
Misc/NEWS
View file @
ee1ae7cc
...
@@ -12,6 +12,9 @@ What's New in Python 3.1 alpha 0
...
@@ -12,6 +12,9 @@ What's New in Python 3.1 alpha 0
Core and Builtins
Core and Builtins
-----------------
-----------------
- Issue #5137: Make len() correctly raise a TypeError when a __len__ method
returns a non-number type.
- Issue #5182: Removed memoryview.__str__.
- Issue #5182: Removed memoryview.__str__.
- Issue #1717: Removed builtin cmp() function, dropped tp_compare
- Issue #1717: Removed builtin cmp() function, dropped tp_compare
...
...
Objects/typeobject.c
View file @
ee1ae7cc
...
@@ -4618,7 +4618,7 @@ slot_sq_length(PyObject *self)
...
@@ -4618,7 +4618,7 @@ slot_sq_length(PyObject *self)
if
(
res
==
NULL
)
if
(
res
==
NULL
)
return
-
1
;
return
-
1
;
len
=
Py
Long_AsSsize_t
(
res
);
len
=
Py
Number_AsSsize_t
(
res
,
PyExc_OverflowError
);
Py_DECREF
(
res
);
Py_DECREF
(
res
);
if
(
len
<
0
)
{
if
(
len
<
0
)
{
if
(
!
PyErr_Occurred
())
if
(
!
PyErr_Occurred
())
...
...
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