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
218a8ab5
Commit
218a8ab5
authored
Sep 28, 2012
by
Mark Dickinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issues #16029, #16030: Fix pickling and repr of large xranges.
parent
9faaf1b4
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
110 additions
and
5 deletions
+110
-5
Lib/test/test_xrange.py
Lib/test/test_xrange.py
+75
-0
Misc/NEWS
Misc/NEWS
+6
-0
Objects/rangeobject.c
Objects/rangeobject.c
+29
-5
No files found.
Lib/test/test_xrange.py
View file @
218a8ab5
...
...
@@ -46,6 +46,28 @@ class XrangeTest(unittest.TestCase):
self
.
fail
(
'{}: wrong element at position {};'
'expected {}, got {}'
.
format
(
test_id
,
i
,
y
,
x
))
def
assert_xranges_equivalent
(
self
,
x
,
y
):
# Check that two xrange objects are equivalent, in the sense of the
# associated sequences being the same. We want to use this for large
# xrange objects, so instead of converting to lists and comparing
# directly we do a number of indirect checks.
if
len
(
x
)
!=
len
(
y
):
self
.
fail
(
'{} and {} have different '
'lengths: {} and {} '
.
format
(
x
,
y
,
len
(
x
),
len
(
y
)))
if
len
(
x
)
>=
1
:
if
x
[
0
]
!=
y
[
0
]:
self
.
fail
(
'{} and {} have different initial '
'elements: {} and {} '
.
format
(
x
,
y
,
x
[
0
],
y
[
0
]))
if
x
[
-
1
]
!=
y
[
-
1
]:
self
.
fail
(
'{} and {} have different final '
'elements: {} and {} '
.
format
(
x
,
y
,
x
[
-
1
],
y
[
-
1
]))
if
len
(
x
)
>=
2
:
x_step
=
x
[
1
]
-
x
[
0
]
y_step
=
y
[
1
]
-
y
[
0
]
if
x_step
!=
y_step
:
self
.
fail
(
'{} and {} have different step: '
'{} and {} '
.
format
(
x
,
y
,
x_step
,
y_step
))
def
test_xrange
(
self
):
self
.
assertEqual
(
list
(
xrange
(
3
)),
[
0
,
1
,
2
])
self
.
assertEqual
(
list
(
xrange
(
1
,
5
)),
[
1
,
2
,
3
,
4
])
...
...
@@ -104,6 +126,59 @@ class XrangeTest(unittest.TestCase):
self
.
assertEqual
(
list
(
pickle
.
loads
(
pickle
.
dumps
(
r
,
proto
))),
list
(
r
))
M
=
min
(
sys
.
maxint
,
sys
.
maxsize
)
large_testcases
=
testcases
+
[
(
0
,
M
,
1
),
(
M
,
0
,
-
1
),
(
0
,
M
,
M
-
1
),
(
M
//
2
,
M
,
1
),
(
0
,
-
M
,
-
1
),
(
0
,
-
M
,
1
-
M
),
(
-
M
,
M
,
2
),
(
-
M
,
M
,
1024
),
(
-
M
,
M
,
10585
),
(
M
,
-
M
,
-
2
),
(
M
,
-
M
,
-
1024
),
(
M
,
-
M
,
-
10585
),
]
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
for
t
in
large_testcases
:
r
=
xrange
(
*
t
)
r_out
=
pickle
.
loads
(
pickle
.
dumps
(
r
,
proto
))
self
.
assert_xranges_equivalent
(
r_out
,
r
)
def
test_repr
(
self
):
# Check that repr of an xrange is a valid representation
# of that xrange.
# Valid xranges have at most min(sys.maxint, sys.maxsize) elements.
M
=
min
(
sys
.
maxint
,
sys
.
maxsize
)
testcases
=
[
(
13
,),
(
0
,
11
),
(
-
22
,
10
),
(
20
,
3
,
-
1
),
(
13
,
21
,
3
),
(
-
2
,
2
,
2
),
(
0
,
M
,
1
),
(
M
,
0
,
-
1
),
(
0
,
M
,
M
-
1
),
(
M
//
2
,
M
,
1
),
(
0
,
-
M
,
-
1
),
(
0
,
-
M
,
1
-
M
),
(
-
M
,
M
,
2
),
(
-
M
,
M
,
1024
),
(
-
M
,
M
,
10585
),
(
M
,
-
M
,
-
2
),
(
M
,
-
M
,
-
1024
),
(
M
,
-
M
,
-
10585
),
]
for
t
in
testcases
:
r
=
xrange
(
*
t
)
r_out
=
eval
(
repr
(
r
))
self
.
assert_xranges_equivalent
(
r
,
r_out
)
def
test_range_iterators
(
self
):
# see issue 7298
limits
=
[
base
+
jiggle
...
...
Misc/NEWS
View file @
218a8ab5
...
...
@@ -9,6 +9,12 @@ What's New in Python 2.7.4
Core and Builtins
-----------------
- Issue #16030: Fix overflow bug in computing the `repr` of an xrange object
with large start, step or length.
- Issue #16029: Fix overflow bug occurring when pickling xranges with large
start, step or length.
- Issue #16037: Limit httplib'
s
_read_status
()
function
to
work
around
broken
HTTP
servers
and
reduce
memory
usage
.
It
's actually a backport of a Python
3.2 fix. Thanks to Adrien Kunysz.
...
...
Objects/rangeobject.c
View file @
218a8ab5
...
...
@@ -37,6 +37,30 @@ get_len_of_range(long lo, long hi, long step)
return
0UL
;
}
/* Return a stop value suitable for reconstructing the xrange from
* a (start, stop, step) triple. Used in range_repr and range_reduce.
* Computes start + len * step, clipped to the range [LONG_MIN, LONG_MAX].
*/
static
long
get_stop_for_range
(
rangeobject
*
r
)
{
long
last
;
if
(
r
->
len
==
0
)
return
r
->
start
;
/* The tricky bit is avoiding overflow. We first compute the last entry in
the xrange, start + (len - 1) * step, which is guaranteed to lie within
the range of a long, and then add step to it. See the range_reverse
comments for an explanation of the casts below.
*/
last
=
(
long
)(
r
->
start
+
(
unsigned
long
)(
r
->
len
-
1
)
*
r
->
step
);
if
(
r
->
step
>
0
)
return
last
>
LONG_MAX
-
r
->
step
?
LONG_MAX
:
last
+
r
->
step
;
else
return
last
<
LONG_MIN
-
r
->
step
?
LONG_MIN
:
last
+
r
->
step
;
}
static
PyObject
*
range_new
(
PyTypeObject
*
type
,
PyObject
*
args
,
PyObject
*
kw
)
{
...
...
@@ -112,17 +136,17 @@ range_repr(rangeobject *r)
if
(
r
->
start
==
0
&&
r
->
step
==
1
)
rtn
=
PyString_FromFormat
(
"xrange(%ld)"
,
r
->
start
+
r
->
len
*
r
->
step
);
get_stop_for_range
(
r
)
);
else
if
(
r
->
step
==
1
)
rtn
=
PyString_FromFormat
(
"xrange(%ld, %ld)"
,
r
->
start
,
r
->
start
+
r
->
len
*
r
->
step
);
get_stop_for_range
(
r
)
);
else
rtn
=
PyString_FromFormat
(
"xrange(%ld, %ld, %ld)"
,
r
->
start
,
r
->
start
+
r
->
len
*
r
->
step
,
get_stop_for_range
(
r
)
,
r
->
step
);
return
rtn
;
}
...
...
@@ -131,9 +155,9 @@ range_repr(rangeobject *r)
static
PyObject
*
range_reduce
(
rangeobject
*
r
,
PyObject
*
args
)
{
return
Py_BuildValue
(
"(O(
iii
))"
,
Py_TYPE
(
r
),
return
Py_BuildValue
(
"(O(
lll
))"
,
Py_TYPE
(
r
),
r
->
start
,
r
->
start
+
r
->
len
*
r
->
step
,
get_stop_for_range
(
r
)
,
r
->
step
);
}
...
...
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