summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
Diffstat (limited to 'Python')
-rw-r--r--Python/ast.c2
-rw-r--r--Python/bltinmodule.c78
-rw-r--r--Python/marshal.c2
3 files changed, 81 insertions, 1 deletions
diff --git a/Python/ast.c b/Python/ast.c
index 8dd3c4ab..485dafb 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -1539,7 +1539,7 @@ ast_for_binop(struct compiling *c, const node *n)
tmp_result = BinOp(result, newoperator, tmp,
LINENO(next_oper), next_oper->n_col_offset,
c->c_arena);
- if (!tmp)
+ if (!tmp_result)
return NULL;
result = tmp_result;
}
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 70f1170..ecc84b5 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -1611,6 +1611,84 @@ builtin_sum(PyObject *self, PyObject *args)
Py_INCREF(result);
}
+#ifndef SLOW_SUM
+ /* Fast addition by keeping temporary sums in C instead of new Python objects.
+ Assumes all inputs are the same type. If the assumption fails, default
+ to the more general routine.
+ */
+ if (PyInt_CheckExact(result)) {
+ long i_result = PyInt_AS_LONG(result);
+ Py_DECREF(result);
+ result = NULL;
+ while(result == NULL) {
+ item = PyIter_Next(iter);
+ if (item == NULL) {
+ Py_DECREF(iter);
+ if (PyErr_Occurred())
+ return NULL;
+ return PyInt_FromLong(i_result);
+ }
+ if (PyInt_CheckExact(item)) {
+ long b = PyInt_AS_LONG(item);
+ long x = i_result + b;
+ if ((x^i_result) >= 0 || (x^b) >= 0) {
+ i_result = x;
+ Py_DECREF(item);
+ continue;
+ }
+ }
+ /* Either overflowed or is not an int. Restore real objects and process normally */
+ result = PyInt_FromLong(i_result);
+ temp = PyNumber_Add(result, item);
+ Py_DECREF(result);
+ Py_DECREF(item);
+ result = temp;
+ if (result == NULL) {
+ Py_DECREF(iter);
+ return NULL;
+ }
+ }
+ }
+
+ if (PyFloat_CheckExact(result)) {
+ double f_result = PyFloat_AS_DOUBLE(result);
+ Py_DECREF(result);
+ result = NULL;
+ while(result == NULL) {
+ item = PyIter_Next(iter);
+ if (item == NULL) {
+ Py_DECREF(iter);
+ if (PyErr_Occurred())
+ return NULL;
+ return PyFloat_FromDouble(f_result);
+ }
+ if (PyFloat_CheckExact(item)) {
+ PyFPE_START_PROTECT("add", return 0)
+ f_result += PyFloat_AS_DOUBLE(item);
+ PyFPE_END_PROTECT(f_result)
+ Py_DECREF(item);
+ continue;
+ }
+ if (PyInt_CheckExact(item)) {
+ PyFPE_START_PROTECT("add", return 0)
+ f_result += (double)PyInt_AS_LONG(item);
+ PyFPE_END_PROTECT(f_result)
+ Py_DECREF(item);
+ continue;
+ }
+ result = PyFloat_FromDouble(f_result);
+ temp = PyNumber_Add(result, item);
+ Py_DECREF(result);
+ Py_DECREF(item);
+ result = temp;
+ if (result == NULL) {
+ Py_DECREF(iter);
+ return NULL;
+ }
+ }
+ }
+#endif
+
for(;;) {
item = PyIter_Next(iter);
if (item == NULL) {
diff --git a/Python/marshal.c b/Python/marshal.c
index c488f27..5cc0fb8 100644
--- a/Python/marshal.c
+++ b/Python/marshal.c
@@ -1009,6 +1009,7 @@ PyMarshal_ReadLongFromFile(FILE *fp)
RFILE rf;
rf.fp = fp;
rf.strings = NULL;
+ rf.ptr = rf.end = NULL;
return r_long(&rf);
}
@@ -1082,6 +1083,7 @@ PyMarshal_ReadObjectFromFile(FILE *fp)
rf.fp = fp;
rf.strings = PyList_New(0);
rf.depth = 0;
+ rf.ptr = rf.end = NULL;
result = r_object(&rf);
Py_DECREF(rf.strings);
return result;