summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2017-09-26 19:20:22 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2017-09-26 19:20:22 (GMT)
commit69b2dc8637ba924d78f9869be592c5a545510f10 (patch)
treec780196933559436c173e7bd072ce548e6ea472d /Modules
parentd6a356209abd026b41245ad76ba3a374ab0c60db (diff)
downloadcpython-69b2dc8637ba924d78f9869be592c5a545510f10.zip
cpython-69b2dc8637ba924d78f9869be592c5a545510f10.tar.gz
cpython-69b2dc8637ba924d78f9869be592c5a545510f10.tar.bz2
[3.6] bpo-30347: Stop crashes when concurrently iterate over itertools.groupby() iterators. (GH-1557) (#3770)
(cherry picked from commit c740e4fe8a9bc5815dc18c38d7f7600b128c3c51)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/itertoolsmodule.c67
1 files changed, 31 insertions, 36 deletions
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index 21c0e82..3ff597b 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -72,10 +72,37 @@ groupby_traverse(groupbyobject *gbo, visitproc visit, void *arg)
return 0;
}
+Py_LOCAL_INLINE(int)
+groupby_step(groupbyobject *gbo)
+{
+ PyObject *newvalue, *newkey, *oldvalue;
+
+ newvalue = PyIter_Next(gbo->it);
+ if (newvalue == NULL)
+ return -1;
+
+ if (gbo->keyfunc == Py_None) {
+ newkey = newvalue;
+ Py_INCREF(newvalue);
+ } else {
+ newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc, newvalue, NULL);
+ if (newkey == NULL) {
+ Py_DECREF(newvalue);
+ return -1;
+ }
+ }
+
+ oldvalue = gbo->currvalue;
+ gbo->currvalue = newvalue;
+ Py_XSETREF(gbo->currkey, newkey);
+ Py_XDECREF(oldvalue);
+ return 0;
+}
+
static PyObject *
groupby_next(groupbyobject *gbo)
{
- PyObject *newvalue, *newkey, *r, *grouper;
+ PyObject *r, *grouper;
/* skip to next iteration group */
for (;;) {
@@ -93,25 +120,9 @@ groupby_next(groupbyobject *gbo)
break;
}
- newvalue = PyIter_Next(gbo->it);
- if (newvalue == NULL)
+ if (groupby_step(gbo) < 0)
return NULL;
-
- if (gbo->keyfunc == Py_None) {
- newkey = newvalue;
- Py_INCREF(newvalue);
- } else {
- newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc, newvalue, NULL);
- if (newkey == NULL) {
- Py_DECREF(newvalue);
- return NULL;
- }
- }
-
- Py_XSETREF(gbo->currkey, newkey);
- Py_XSETREF(gbo->currvalue, newvalue);
}
-
Py_INCREF(gbo->currkey);
Py_XSETREF(gbo->tgtkey, gbo->currkey);
@@ -282,28 +293,12 @@ static PyObject *
_grouper_next(_grouperobject *igo)
{
groupbyobject *gbo = (groupbyobject *)igo->parent;
- PyObject *newvalue, *newkey, *r;
+ PyObject *r;
int rcmp;
if (gbo->currvalue == NULL) {
- newvalue = PyIter_Next(gbo->it);
- if (newvalue == NULL)
+ if (groupby_step(gbo) < 0)
return NULL;
-
- if (gbo->keyfunc == Py_None) {
- newkey = newvalue;
- Py_INCREF(newvalue);
- } else {
- newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc, newvalue, NULL);
- if (newkey == NULL) {
- Py_DECREF(newvalue);
- return NULL;
- }
- }
-
- assert(gbo->currkey == NULL);
- gbo->currkey = newkey;
- gbo->currvalue = newvalue;
}
assert(gbo->currkey != NULL);