mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Drop _PyLong_FitsInLong. Fixes #1666.
This commit is contained in:
parent
704b34d9e4
commit
8445104d7d
3 changed files with 36 additions and 35 deletions
|
@ -50,7 +50,6 @@ PyAPI_DATA(int) _PyLong_DigitValue[256];
|
||||||
be multiplied by SHIFT! There may not be enough room in an int to store
|
be multiplied by SHIFT! There may not be enough room in an int to store
|
||||||
e*SHIFT directly. */
|
e*SHIFT directly. */
|
||||||
PyAPI_FUNC(double) _PyLong_AsScaledDouble(PyObject *vv, int *e);
|
PyAPI_FUNC(double) _PyLong_AsScaledDouble(PyObject *vv, int *e);
|
||||||
PyAPI_FUNC(int) _PyLong_FitsInLong(PyObject* vv);
|
|
||||||
|
|
||||||
PyAPI_FUNC(double) PyLong_AsDouble(PyObject *);
|
PyAPI_FUNC(double) PyLong_AsDouble(PyObject *);
|
||||||
PyAPI_FUNC(PyObject *) PyLong_FromVoidPtr(void *);
|
PyAPI_FUNC(PyObject *) PyLong_FromVoidPtr(void *);
|
||||||
|
|
|
@ -392,19 +392,6 @@ PyLong_AsLong(PyObject *obj)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
|
||||||
_PyLong_FitsInLong(PyObject *vv)
|
|
||||||
{
|
|
||||||
int size;
|
|
||||||
if (!PyLong_CheckExact(vv)) {
|
|
||||||
PyErr_BadInternalCall();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
/* conservative estimate */
|
|
||||||
size = Py_SIZE(vv);
|
|
||||||
return -2 <= size && size <= 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Get a Py_ssize_t from a long int object.
|
/* Get a Py_ssize_t from a long int object.
|
||||||
Returns -1 and sets an error condition if overflow occurs. */
|
Returns -1 and sets an error condition if overflow occurs. */
|
||||||
|
|
||||||
|
|
|
@ -558,14 +558,23 @@ range_iter(PyObject *seq)
|
||||||
rangeobject *r = (rangeobject *)seq;
|
rangeobject *r = (rangeobject *)seq;
|
||||||
longrangeiterobject *it;
|
longrangeiterobject *it;
|
||||||
PyObject *tmp, *len;
|
PyObject *tmp, *len;
|
||||||
|
long lstart, lstop, lstep;
|
||||||
|
|
||||||
assert(PyRange_Check(seq));
|
assert(PyRange_Check(seq));
|
||||||
if (_PyLong_FitsInLong(r->start) &&
|
|
||||||
_PyLong_FitsInLong(r->stop) &&
|
/* If all three fields convert to long, use the int version */
|
||||||
_PyLong_FitsInLong(r->step))
|
lstart = PyLong_AsLong(r->start);
|
||||||
return int_range_iter(PyLong_AsLong(r->start),
|
if (lstart != -1 || !PyErr_Occurred()) {
|
||||||
PyLong_AsLong(r->stop),
|
lstop = PyLong_AsLong(r->stop);
|
||||||
PyLong_AsLong(r->step));
|
if (lstop != -1 || !PyErr_Occurred()) {
|
||||||
|
lstep = PyLong_AsLong(r->step);
|
||||||
|
if (lstep != -1 || !PyErr_Occurred())
|
||||||
|
return int_range_iter(lstart, lstop, lstep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* Some conversion failed, so there is an error set. Clear it,
|
||||||
|
and try again with a long range. */
|
||||||
|
PyErr_Clear();
|
||||||
|
|
||||||
it = PyObject_New(longrangeiterobject, &PyLongRangeIter_Type);
|
it = PyObject_New(longrangeiterobject, &PyLongRangeIter_Type);
|
||||||
if (it == NULL)
|
if (it == NULL)
|
||||||
|
@ -605,27 +614,33 @@ range_reverse(PyObject *seq)
|
||||||
rangeobject *range = (rangeobject*) seq;
|
rangeobject *range = (rangeobject*) seq;
|
||||||
longrangeiterobject *it;
|
longrangeiterobject *it;
|
||||||
PyObject *one, *sum, *diff, *len = NULL, *product;
|
PyObject *one, *sum, *diff, *len = NULL, *product;
|
||||||
|
long lstart, lstop, lstep;
|
||||||
|
|
||||||
/* XXX(nnorwitz): do the calc for the new start/stop first,
|
/* XXX(nnorwitz): do the calc for the new start/stop first,
|
||||||
then if they fit, call the proper iter()?
|
then if they fit, call the proper iter()?
|
||||||
*/
|
*/
|
||||||
assert(PyRange_Check(seq));
|
assert(PyRange_Check(seq));
|
||||||
if (_PyLong_FitsInLong(range->start) &&
|
|
||||||
_PyLong_FitsInLong(range->stop) &&
|
/* If all three fields convert to long, use the int version */
|
||||||
_PyLong_FitsInLong(range->step)) {
|
lstart = PyLong_AsLong(range->start);
|
||||||
long start = PyLong_AsLong(range->start);
|
if (lstart != -1 || !PyErr_Occurred()) {
|
||||||
long step = PyLong_AsLong(range->step);
|
lstop = PyLong_AsLong(range->stop);
|
||||||
long stop = PyLong_AsLong(range->stop);
|
if (lstop != -1 || !PyErr_Occurred()) {
|
||||||
|
lstep = PyLong_AsLong(range->step);
|
||||||
|
if (lstep != -1 || !PyErr_Occurred()) {
|
||||||
/* XXX(nnorwitz): need to check for overflow and simplify. */
|
/* XXX(nnorwitz): need to check for overflow and simplify. */
|
||||||
long len = get_len_of_range(start, stop, step);
|
long len = get_len_of_range(lstart, lstop, lstep);
|
||||||
long new_start = start + (len - 1) * step;
|
long new_start = lstart + (len - 1) * lstep;
|
||||||
long new_stop = start;
|
long new_stop = lstart;
|
||||||
if (step > 0)
|
if (lstep > 0)
|
||||||
new_stop -= 1;
|
new_stop -= 1;
|
||||||
else
|
else
|
||||||
new_stop += 1;
|
new_stop += 1;
|
||||||
return int_range_iter(new_start, new_stop, -step);
|
return int_range_iter(new_start, new_stop, -lstep);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PyErr_Clear();
|
||||||
|
|
||||||
it = PyObject_New(longrangeiterobject, &PyLongRangeIter_Type);
|
it = PyObject_New(longrangeiterobject, &PyLongRangeIter_Type);
|
||||||
if (it == NULL)
|
if (it == NULL)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue