Commit da727edf authored by Kevin Modzelewski's avatar Kevin Modzelewski

add refcounting to min/max

parent dcb6fca3
...@@ -159,7 +159,6 @@ extern "C" Box* any(Box* container) { ...@@ -159,7 +159,6 @@ extern "C" Box* any(Box* container) {
} }
Box* min_max(Box* arg0, BoxedTuple* args, BoxedDict* kwargs, int opid) { Box* min_max(Box* arg0, BoxedTuple* args, BoxedDict* kwargs, int opid) {
assert(0 && "check refcounting");
assert(args->cls == tuple_cls); assert(args->cls == tuple_cls);
if (kwargs) if (kwargs)
assert(kwargs->cls == dict_cls); assert(kwargs->cls == dict_cls);
...@@ -182,16 +181,18 @@ Box* min_max(Box* arg0, BoxedTuple* args, BoxedDict* kwargs, int opid) { ...@@ -182,16 +181,18 @@ Box* min_max(Box* arg0, BoxedTuple* args, BoxedDict* kwargs, int opid) {
} }
} }
XKEEP_ALIVE(key_func); // probably not necessary
if (args->size() == 0) { if (args->size() == 0) {
extremElement = nullptr; extremElement = nullptr;
extremVal = nullptr; extremVal = nullptr;
container = arg0; container = arg0;
} else { } else {
extremElement = arg0; extremElement = incref(arg0);
if (key_func != NULL) { if (key_func != NULL) {
extremVal = runtimeCall(key_func, ArgPassSpec(1), extremElement, NULL, NULL, NULL, NULL); extremVal = runtimeCall(key_func, ArgPassSpec(1), extremElement, NULL, NULL, NULL, NULL);
} else { } else {
extremVal = extremElement; extremVal = incref(extremElement);
} }
container = args; container = args;
} }
...@@ -207,25 +208,35 @@ Box* min_max(Box* arg0, BoxedTuple* args, BoxedDict* kwargs, int opid) { ...@@ -207,25 +208,35 @@ Box* min_max(Box* arg0, BoxedTuple* args, BoxedDict* kwargs, int opid) {
curVal = runtimeCall(key_func, ArgPassSpec(1), e, NULL, NULL, NULL, NULL); curVal = runtimeCall(key_func, ArgPassSpec(1), e, NULL, NULL, NULL, NULL);
} else { } else {
if (!extremElement) { if (!extremElement) {
extremVal = e; extremVal = incref(e);
extremElement = e; extremElement = e;
continue; continue;
} }
curVal = e; curVal = incref(e);
} }
int r = PyObject_RichCompareBool(curVal, extremVal, opid); int r = PyObject_RichCompareBool(curVal, extremVal, opid);
if (r == -1) if (r == -1) {
Py_DECREF(e);
Py_DECREF(extremVal);
Py_DECREF(extremElement);
Py_DECREF(curVal);
throwCAPIException(); throwCAPIException();
}
if (r) { if (r) {
Py_DECREF(extremElement);
Py_DECREF(extremVal);
extremElement = e; extremElement = e;
extremVal = curVal; extremVal = curVal;
} else {
Py_DECREF(curVal);
Py_DECREF(e);
} }
} }
Py_DECREF(extremVal);
return extremElement; return extremElement;
} }
extern "C" Box* min(Box* arg0, BoxedTuple* args, BoxedDict* kwargs) { extern "C" Box* min(Box* arg0, BoxedTuple* args, BoxedDict* kwargs) {
assert(0 && "check refcounting");
if (arg0 == None && args->size() == 0) { if (arg0 == None && args->size() == 0) {
raiseExcHelper(TypeError, "min expected 1 arguments, got 0"); raiseExcHelper(TypeError, "min expected 1 arguments, got 0");
} }
...@@ -239,7 +250,6 @@ extern "C" Box* min(Box* arg0, BoxedTuple* args, BoxedDict* kwargs) { ...@@ -239,7 +250,6 @@ extern "C" Box* min(Box* arg0, BoxedTuple* args, BoxedDict* kwargs) {
} }
extern "C" Box* max(Box* arg0, BoxedTuple* args, BoxedDict* kwargs) { extern "C" Box* max(Box* arg0, BoxedTuple* args, BoxedDict* kwargs) {
assert(0 && "check refcounting");
if (arg0 == None && args->size() == 0) { if (arg0 == None && args->size() == 0) {
raiseExcHelper(TypeError, "max expected 1 arguments, got 0"); raiseExcHelper(TypeError, "max expected 1 arguments, got 0");
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment