summaryrefslogtreecommitdiffstats
path: root/Objects/longobject.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2006-03-07 18:50:55 (GMT)
committerGuido van Rossum <guido@python.org>2006-03-07 18:50:55 (GMT)
commit38fff8c4e4276e4e57660a78f305e68bfa87874b (patch)
tree5f79c06159053f9c7113410fc69dccba01e331ab /Objects/longobject.c
parent9d7855076a8e030e30459de685e762f63bdecac6 (diff)
downloadcpython-38fff8c4e4276e4e57660a78f305e68bfa87874b.zip
cpython-38fff8c4e4276e4e57660a78f305e68bfa87874b.tar.gz
cpython-38fff8c4e4276e4e57660a78f305e68bfa87874b.tar.bz2
Checking in the code for PEP 357.
This was mostly written by Travis Oliphant. I've inspected it all; Neal Norwitz and MvL have also looked at it (in an earlier incarnation).
Diffstat (limited to 'Objects/longobject.c')
-rw-r--r--Objects/longobject.c49
1 files changed, 42 insertions, 7 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 9032656..e47c292 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -241,12 +241,8 @@ PyLong_AsLong(PyObject *vv)
return -1;
}
-/* Get a Py_ssize_t from a long int object.
- Returns -1 and sets an error condition if overflow occurs. */
-
-Py_ssize_t
-_PyLong_AsSsize_t(PyObject *vv)
-{
+static Py_ssize_t
+_long_as_ssize_t(PyObject *vv) {
register PyLongObject *v;
size_t x, prev;
Py_ssize_t i;
@@ -282,7 +278,45 @@ _PyLong_AsSsize_t(PyObject *vv)
overflow:
PyErr_SetString(PyExc_OverflowError,
"long int too large to convert to int");
- return -1;
+ if (sign > 0)
+ return PY_SSIZE_T_MAX;
+ else
+ return -PY_SSIZE_T_MAX-1;
+}
+
+/* Get a Py_ssize_t from a long int object.
+ Returns -1 and sets an error condition if overflow occurs. */
+
+Py_ssize_t
+_PyLong_AsSsize_t(PyObject *vv)
+{
+ Py_ssize_t x;
+
+ x = _long_as_ssize_t(vv);
+ if (PyErr_Occurred()) return -1;
+ return x;
+}
+
+
+/* Get a Py_ssize_t from a long int object.
+ Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
+ and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
+ Return 0 on error, 1 on success.
+*/
+
+static Py_ssize_t
+long_index(PyObject *vv)
+{
+ Py_ssize_t x;
+
+ x = _long_as_ssize_t(vv);
+ if (PyErr_Occurred()) {
+ /* If overflow error, ignore the error */
+ if (x != -1) {
+ PyErr_Clear();
+ }
+ }
+ return x;
}
/* Get a C unsigned long int from a long int object.
@@ -3131,6 +3165,7 @@ static PyNumberMethods long_as_number = {
long_true_divide, /* nb_true_divide */
0, /* nb_inplace_floor_divide */
0, /* nb_inplace_true_divide */
+ (lenfunc)long_index, /* nb_index */
};
PyTypeObject PyLong_Type = {