Commit fc46bc42 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Add a whole bunch of refcounting annotations

parent 0b1acea8
...@@ -544,6 +544,10 @@ void ASTInterpreter::doStore(AST_expr* node, STOLEN(Value) value) { ...@@ -544,6 +544,10 @@ void ASTInterpreter::doStore(AST_expr* node, STOLEN(Value) value) {
Value target = visit_expr(subscript->value); Value target = visit_expr(subscript->value);
Value slice = visit_slice(subscript->slice); Value slice = visit_slice(subscript->slice);
AUTO_DECREF(target.o);
AUTO_DECREF(slice.o);
AUTO_DECREF(value.o);
if (jit) if (jit)
jit->emitSetItem(target, slice, value); jit->emitSetItem(target, slice, value);
setitem(target.o, slice.o, value.o); setitem(target.o, slice.o, value.o);
...@@ -602,6 +606,9 @@ Value ASTInterpreter::visit_slice(AST_Slice* node) { ...@@ -602,6 +606,9 @@ Value ASTInterpreter::visit_slice(AST_Slice* node) {
Value lower = node->lower ? visit_expr(node->lower) : getNone(); Value lower = node->lower ? visit_expr(node->lower) : getNone();
Value upper = node->upper ? visit_expr(node->upper) : getNone(); Value upper = node->upper ? visit_expr(node->upper) : getNone();
Value step = node->step ? visit_expr(node->step) : getNone(); Value step = node->step ? visit_expr(node->step) : getNone();
AUTO_DECREF(lower.o);
AUTO_DECREF(upper.o);
AUTO_DECREF(step.o);
Value v; Value v;
if (jit) if (jit)
...@@ -877,11 +884,15 @@ Value ASTInterpreter::visit_langPrimitive(AST_LangPrimitive* node) { ...@@ -877,11 +884,15 @@ Value ASTInterpreter::visit_langPrimitive(AST_LangPrimitive* node) {
assert(node->args[1]->type == AST_TYPE::Str); assert(node->args[1]->type == AST_TYPE::Str);
Value module = visit_expr(node->args[0]); Value module = visit_expr(node->args[0]);
AUTO_DECREF(module.o);
auto ast_str = ast_cast<AST_Str>(node->args[1]); auto ast_str = ast_cast<AST_Str>(node->args[1]);
assert(ast_str->str_type == AST_Str::STR); assert(ast_str->str_type == AST_Str::STR);
const std::string& name = ast_str->str_data; const std::string& name = ast_str->str_data;
assert(name.size()); assert(name.size());
BoxedString* name_boxed = source_info->parent_module->getStringConstant(name, true); BoxedString* name_boxed = source_info->parent_module->getStringConstant(name, true);
AUTO_DECREF(name_boxed);
if (jit) if (jit)
v.var = jit->emitImportFrom(module, name_boxed); v.var = jit->emitImportFrom(module, name_boxed);
v.o = importFrom(module.o, name_boxed); v.o = importFrom(module.o, name_boxed);
...@@ -1246,6 +1257,8 @@ Value ASTInterpreter::visit_delete(AST_Delete* node) { ...@@ -1246,6 +1257,8 @@ Value ASTInterpreter::visit_delete(AST_Delete* node) {
AST_Subscript* sub = (AST_Subscript*)target_; AST_Subscript* sub = (AST_Subscript*)target_;
Value value = visit_expr(sub->value); Value value = visit_expr(sub->value);
Value slice = visit_slice(sub->slice); Value slice = visit_slice(sub->slice);
AUTO_DECREF(value.o);
AUTO_DECREF(slice.o);
if (jit) if (jit)
jit->emitDelItem(value, slice); jit->emitDelItem(value, slice);
delitem(value.o, slice.o); delitem(value.o, slice.o);
...@@ -1625,6 +1638,9 @@ Value ASTInterpreter::visit_subscript(AST_Subscript* node) { ...@@ -1625,6 +1638,9 @@ Value ASTInterpreter::visit_subscript(AST_Subscript* node) {
Value value = visit_expr(node->value); Value value = visit_expr(node->value);
Value slice = visit_slice(node->slice); Value slice = visit_slice(node->slice);
AUTO_DECREF(value.o);
AUTO_DECREF(slice.o);
return Value(getitem(value.o, slice.o), jit ? jit->emitGetItem(node, value, slice) : NULL); return Value(getitem(value.o, slice.o), jit ? jit->emitGetItem(node, value, slice) : NULL);
} }
...@@ -1634,9 +1650,13 @@ Value ASTInterpreter::visit_list(AST_List* node) { ...@@ -1634,9 +1650,13 @@ Value ASTInterpreter::visit_list(AST_List* node) {
BoxedList* list = new BoxedList(); BoxedList* list = new BoxedList();
list->ensure(node->elts.size()); list->ensure(node->elts.size());
for (AST_expr* e : node->elts) { for (AST_expr* e : node->elts) {
Value v = visit_expr(e); try {
items.push_back(v); Value v = visit_expr(e);
listAppendInternalStolen(list, v.o); items.push_back(v);
listAppendInternalStolen(list, v.o);
} catch (ExcInfo e) {
RELEASE_ASSERT(0, "check refcounting");
}
} }
return Value(list, jit ? jit->emitCreateList(items) : NULL); return Value(list, jit ? jit->emitCreateList(items) : NULL);
......
...@@ -331,7 +331,7 @@ void compileAndRunModule(AST_Module* m, BoxedModule* bm) { ...@@ -331,7 +331,7 @@ void compileAndRunModule(AST_Module* m, BoxedModule* bm) {
static BoxedString* builtins_str = getStaticString("__builtins__"); static BoxedString* builtins_str = getStaticString("__builtins__");
if (!bm->hasattr(builtins_str)) if (!bm->hasattr(builtins_str))
bm->giveAttr(incref(builtins_str), PyModule_GetDict(builtins_module)); bm->setattr(builtins_str, PyModule_GetDict(builtins_module), NULL);
md = new FunctionMetadata(0, false, false, std::move(si)); md = new FunctionMetadata(0, false, false, std::move(si));
} }
......
...@@ -913,7 +913,7 @@ static int instance_compare(PyObject* v, PyObject* w) noexcept { ...@@ -913,7 +913,7 @@ static int instance_compare(PyObject* v, PyObject* w) noexcept {
Box* instanceCompare(Box* _inst, Box* other) { Box* instanceCompare(Box* _inst, Box* other) {
int rtn = instance_compare(_inst, other); int rtn = instance_compare(_inst, other);
if (rtn == 2) if (rtn == 2)
return NotImplemented; return incref(NotImplemented);
if (rtn == -2) if (rtn == -2)
throwCAPIException(); throwCAPIException();
return boxInt(rtn); return boxInt(rtn);
...@@ -1212,7 +1212,7 @@ static Box* _instanceBinary(Box* _inst, Box* other, BoxedString* attr) { ...@@ -1212,7 +1212,7 @@ static Box* _instanceBinary(Box* _inst, Box* other, BoxedString* attr) {
Box* func = _instanceGetattribute(inst, attr, false); Box* func = _instanceGetattribute(inst, attr, false);
if (!func) if (!func)
return NotImplemented; return incref(NotImplemented);
return runtimeCall(func, ArgPassSpec(1), other, NULL, NULL, NULL, NULL); return runtimeCall(func, ArgPassSpec(1), other, NULL, NULL, NULL, NULL);
} }
......
...@@ -156,7 +156,7 @@ extern "C" Box* complexAdd(BoxedComplex* lhs, Box* rhs) { ...@@ -156,7 +156,7 @@ extern "C" Box* complexAdd(BoxedComplex* lhs, Box* rhs) {
} else if (rhs->cls == complex_cls) { } else if (rhs->cls == complex_cls) {
return complexAddComplex(lhs, static_cast<BoxedComplex*>(rhs)); return complexAddComplex(lhs, static_cast<BoxedComplex*>(rhs));
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -189,7 +189,7 @@ extern "C" Box* complexSub(BoxedComplex* lhs, Box* rhs) { ...@@ -189,7 +189,7 @@ extern "C" Box* complexSub(BoxedComplex* lhs, Box* rhs) {
} else if (rhs->cls == complex_cls) { } else if (rhs->cls == complex_cls) {
return complexSubComplex(lhs, static_cast<BoxedComplex*>(rhs)); return complexSubComplex(lhs, static_cast<BoxedComplex*>(rhs));
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -205,7 +205,7 @@ extern "C" Box* complexRSub(BoxedComplex* _lhs, Box* _rhs) { ...@@ -205,7 +205,7 @@ extern "C" Box* complexRSub(BoxedComplex* _lhs, Box* _rhs) {
} else if (_rhs->cls == complex_cls) { } else if (_rhs->cls == complex_cls) {
lhs = static_cast<BoxedComplex*>(_rhs); lhs = static_cast<BoxedComplex*>(_rhs);
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
return complexSubComplex(lhs, _lhs); return complexSubComplex(lhs, _lhs);
} }
...@@ -239,7 +239,7 @@ extern "C" Box* complexMul(BoxedComplex* lhs, Box* rhs) { ...@@ -239,7 +239,7 @@ extern "C" Box* complexMul(BoxedComplex* lhs, Box* rhs) {
} else if (rhs->cls == complex_cls) { } else if (rhs->cls == complex_cls) {
return complexMulComplex(lhs, static_cast<BoxedComplex*>(rhs)); return complexMulComplex(lhs, static_cast<BoxedComplex*>(rhs));
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -308,7 +308,7 @@ extern "C" Box* complexDiv(BoxedComplex* lhs, Box* rhs) { ...@@ -308,7 +308,7 @@ extern "C" Box* complexDiv(BoxedComplex* lhs, Box* rhs) {
} else if (rhs->cls == complex_cls) { } else if (rhs->cls == complex_cls) {
return complexDivComplex(lhs, static_cast<BoxedComplex*>(rhs)); return complexDivComplex(lhs, static_cast<BoxedComplex*>(rhs));
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -324,7 +324,7 @@ extern "C" Box* complexRDiv(BoxedComplex* _lhs, Box* _rhs) { ...@@ -324,7 +324,7 @@ extern "C" Box* complexRDiv(BoxedComplex* _lhs, Box* _rhs) {
} else if (_rhs->cls == complex_cls) { } else if (_rhs->cls == complex_cls) {
lhs = static_cast<BoxedComplex*>(_rhs); lhs = static_cast<BoxedComplex*>(_rhs);
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
return complexDivComplex(lhs, _lhs); return complexDivComplex(lhs, _lhs);
} }
...@@ -584,7 +584,7 @@ Box* complexCoerce(Box* lhs, Box* rhs) { ...@@ -584,7 +584,7 @@ Box* complexCoerce(Box* lhs, Box* rhs) {
cval.real = PyFloat_AsDouble(rhs); cval.real = PyFloat_AsDouble(rhs);
rhs = PyComplex_FromCComplex(cval); rhs = PyComplex_FromCComplex(cval);
} else if (!PyComplex_Check(rhs)) { } else if (!PyComplex_Check(rhs)) {
return NotImplemented; return incref(NotImplemented);
} }
return BoxedTuple::create({ lhs, rhs }); return BoxedTuple::create({ lhs, rhs });
} }
...@@ -830,7 +830,7 @@ static Box* to_complex(Box* self) noexcept { ...@@ -830,7 +830,7 @@ static Box* to_complex(Box* self) noexcept {
} else if (self->cls == long_cls) { } else if (self->cls == long_cls) {
r = new BoxedComplex(PyLong_AsDouble(self), 0.0); r = new BoxedComplex(PyLong_AsDouble(self), 0.0);
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
return r; return r;
} }
...@@ -1023,7 +1023,7 @@ extern "C" Box* complexMod(BoxedComplex* lhs, Box* _rhs) { ...@@ -1023,7 +1023,7 @@ extern "C" Box* complexMod(BoxedComplex* lhs, Box* _rhs) {
Box* res = to_complex(_rhs); Box* res = to_complex(_rhs);
if (res == NotImplemented) { if (res == NotImplemented) {
return NotImplemented; return incref(NotImplemented);
} }
BoxedComplex* rhs = (BoxedComplex*)res; BoxedComplex* rhs = (BoxedComplex*)res;
...@@ -1046,7 +1046,7 @@ extern "C" Box* complexFloordiv(BoxedComplex* lhs, Box* _rhs) { ...@@ -1046,7 +1046,7 @@ extern "C" Box* complexFloordiv(BoxedComplex* lhs, Box* _rhs) {
Box* res = to_complex(_rhs); Box* res = to_complex(_rhs);
if (res == NotImplemented) { if (res == NotImplemented) {
return NotImplemented; return incref(NotImplemented);
} }
BoxedComplex* rhs = (BoxedComplex*)res; BoxedComplex* rhs = (BoxedComplex*)res;
......
...@@ -607,7 +607,7 @@ Box* dictEq(BoxedDict* self, Box* _rhs) { ...@@ -607,7 +607,7 @@ Box* dictEq(BoxedDict* self, Box* _rhs) {
_rhs = attrwrapperToDict(_rhs); _rhs = attrwrapperToDict(_rhs);
if (!PyDict_Check(_rhs)) if (!PyDict_Check(_rhs))
return NotImplemented; return incref(NotImplemented);
BoxedDict* rhs = static_cast<BoxedDict*>(_rhs); BoxedDict* rhs = static_cast<BoxedDict*>(_rhs);
......
...@@ -127,7 +127,7 @@ extern "C" Box* floatAdd(BoxedFloat* lhs, Box* rhs) { ...@@ -127,7 +127,7 @@ extern "C" Box* floatAdd(BoxedFloat* lhs, Box* rhs) {
} else if (rhs->cls == long_cls) { } else if (rhs->cls == long_cls) {
return boxFloat(lhs->d + PyLong_AsDouble(rhs)); return boxFloat(lhs->d + PyLong_AsDouble(rhs));
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -154,7 +154,7 @@ extern "C" Box* floatDiv(BoxedFloat* lhs, Box* rhs) { ...@@ -154,7 +154,7 @@ extern "C" Box* floatDiv(BoxedFloat* lhs, Box* rhs) {
} else if (rhs->cls == long_cls) { } else if (rhs->cls == long_cls) {
return boxFloat(lhs->d / PyLong_AsDouble(rhs)); return boxFloat(lhs->d / PyLong_AsDouble(rhs));
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -167,7 +167,7 @@ extern "C" Box* floatTruediv(BoxedFloat* lhs, Box* rhs) { ...@@ -167,7 +167,7 @@ extern "C" Box* floatTruediv(BoxedFloat* lhs, Box* rhs) {
} else if (rhs->cls == long_cls) { } else if (rhs->cls == long_cls) {
return boxFloat(lhs->d / PyLong_AsDouble(rhs)); return boxFloat(lhs->d / PyLong_AsDouble(rhs));
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -194,7 +194,7 @@ extern "C" Box* floatRDiv(BoxedFloat* lhs, Box* rhs) { ...@@ -194,7 +194,7 @@ extern "C" Box* floatRDiv(BoxedFloat* lhs, Box* rhs) {
} else if (rhs->cls == long_cls) { } else if (rhs->cls == long_cls) {
return boxFloat(PyLong_AsDouble(rhs) / lhs->d); return boxFloat(PyLong_AsDouble(rhs) / lhs->d);
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -219,7 +219,7 @@ extern "C" Box* floatFloorDiv(BoxedFloat* lhs, Box* rhs) { ...@@ -219,7 +219,7 @@ extern "C" Box* floatFloorDiv(BoxedFloat* lhs, Box* rhs) {
} else if (rhs->cls == float_cls) { } else if (rhs->cls == float_cls) {
return floatFloorDivFloat(lhs, static_cast<BoxedFloat*>(rhs)); return floatFloorDivFloat(lhs, static_cast<BoxedFloat*>(rhs));
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -546,7 +546,7 @@ extern "C" Box* floatMod(BoxedFloat* lhs, Box* rhs) { ...@@ -546,7 +546,7 @@ extern "C" Box* floatMod(BoxedFloat* lhs, Box* rhs) {
} else if (rhs->cls == long_cls) { } else if (rhs->cls == long_cls) {
return boxFloat(mod_float_float(lhs->d, PyLong_AsDouble(rhs))); return boxFloat(mod_float_float(lhs->d, PyLong_AsDouble(rhs)));
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -571,7 +571,7 @@ extern "C" Box* floatRMod(BoxedFloat* lhs, Box* rhs) { ...@@ -571,7 +571,7 @@ extern "C" Box* floatRMod(BoxedFloat* lhs, Box* rhs) {
} else if (rhs->cls == long_cls) { } else if (rhs->cls == long_cls) {
return boxFloat(mod_float_float(PyLong_AsDouble(rhs), lhs->d)); return boxFloat(mod_float_float(PyLong_AsDouble(rhs), lhs->d));
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -628,7 +628,7 @@ extern "C" Box* floatMul(BoxedFloat* lhs, Box* rhs) { ...@@ -628,7 +628,7 @@ extern "C" Box* floatMul(BoxedFloat* lhs, Box* rhs) {
} else if (rhs->cls == long_cls) { } else if (rhs->cls == long_cls) {
return boxFloat(lhs->d * PyLong_AsDouble(rhs)); return boxFloat(lhs->d * PyLong_AsDouble(rhs));
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -653,7 +653,7 @@ extern "C" Box* floatSub(BoxedFloat* lhs, Box* rhs) { ...@@ -653,7 +653,7 @@ extern "C" Box* floatSub(BoxedFloat* lhs, Box* rhs) {
} else if (rhs->cls == long_cls) { } else if (rhs->cls == long_cls) {
return boxFloat(lhs->d - PyLong_AsDouble(rhs)); return boxFloat(lhs->d - PyLong_AsDouble(rhs));
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -678,7 +678,7 @@ extern "C" Box* floatRSub(BoxedFloat* lhs, Box* rhs) { ...@@ -678,7 +678,7 @@ extern "C" Box* floatRSub(BoxedFloat* lhs, Box* rhs) {
} else if (rhs->cls == long_cls) { } else if (rhs->cls == long_cls) {
return boxFloat(PyLong_AsDouble(rhs) - lhs->d); return boxFloat(PyLong_AsDouble(rhs) - lhs->d);
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
namespace pyston { namespace pyston {
BoxedListIterator::BoxedListIterator(BoxedList* l, int start) : l(l), pos(start) { BoxedListIterator::BoxedListIterator(BoxedList* l, int start) : l(l), pos(start) {
Py_INCREF(l);
} }
Box* listIterIter(Box* s) { Box* listIterIter(Box* s) {
...@@ -45,6 +46,7 @@ Box* listiterHasnext(Box* s) { ...@@ -45,6 +46,7 @@ Box* listiterHasnext(Box* s) {
bool ans = (self->pos < self->l->size); bool ans = (self->pos < self->l->size);
if (!ans) { if (!ans) {
Py_DECREF(self->l);
self->l = NULL; self->l = NULL;
} }
return boxBool(ans); return boxBool(ans);
...@@ -60,6 +62,7 @@ i1 listiterHasnextUnboxed(Box* s) { ...@@ -60,6 +62,7 @@ i1 listiterHasnextUnboxed(Box* s) {
bool ans = (self->pos < self->l->size); bool ans = (self->pos < self->l->size);
if (!ans) { if (!ans) {
Py_DECREF(self->l);
self->l = NULL; self->l = NULL;
} }
return ans; return ans;
...@@ -73,6 +76,7 @@ Box* listiter_next(Box* s) noexcept { ...@@ -73,6 +76,7 @@ Box* listiter_next(Box* s) noexcept {
return NULL; return NULL;
if (!(self->pos >= 0 && self->pos < self->l->size)) { if (!(self->pos >= 0 && self->pos < self->l->size)) {
Py_DECREF(self->l);
self->l = NULL; self->l = NULL;
return NULL; return NULL;
} }
...@@ -163,6 +167,10 @@ extern "C" void listAppendArrayInternal(Box* s, Box** v, int nelts) { ...@@ -163,6 +167,10 @@ extern "C" void listAppendArrayInternal(Box* s, Box** v, int nelts) {
assert(self->size <= self->capacity); assert(self->size <= self->capacity);
self->ensure(nelts); self->ensure(nelts);
for (int i = 0; i < nelts; i++) {
Py_INCREF(v[i]);
}
assert(self->size <= self->capacity); assert(self->size <= self->capacity);
memcpy(&self->elts->elts[self->size], &v[0], nelts * sizeof(Box*)); memcpy(&self->elts->elts[self->size], &v[0], nelts * sizeof(Box*));
......
...@@ -515,7 +515,7 @@ extern "C" Box* intAdd(BoxedInt* lhs, Box* rhs) { ...@@ -515,7 +515,7 @@ extern "C" Box* intAdd(BoxedInt* lhs, Box* rhs) {
BoxedFloat* rhs_float = static_cast<BoxedFloat*>(rhs); BoxedFloat* rhs_float = static_cast<BoxedFloat*>(rhs);
return boxFloat(lhs->n + rhs_float->d); return boxFloat(lhs->n + rhs_float->d);
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -530,7 +530,7 @@ extern "C" Box* intAnd(BoxedInt* lhs, Box* rhs) { ...@@ -530,7 +530,7 @@ extern "C" Box* intAnd(BoxedInt* lhs, Box* rhs) {
raiseExcHelper(TypeError, "descriptor '__and__' requires a 'int' object but received a '%s'", getTypeName(lhs)); raiseExcHelper(TypeError, "descriptor '__and__' requires a 'int' object but received a '%s'", getTypeName(lhs));
if (!PyInt_Check(rhs)) { if (!PyInt_Check(rhs)) {
return NotImplemented; return incref(NotImplemented);
} }
BoxedInt* rhs_int = static_cast<BoxedInt*>(rhs); BoxedInt* rhs_int = static_cast<BoxedInt*>(rhs);
return boxInt(lhs->n & rhs_int->n); return boxInt(lhs->n & rhs_int->n);
...@@ -547,7 +547,7 @@ extern "C" Box* intOr(BoxedInt* lhs, Box* rhs) { ...@@ -547,7 +547,7 @@ extern "C" Box* intOr(BoxedInt* lhs, Box* rhs) {
raiseExcHelper(TypeError, "descriptor '__or__' requires a 'int' object but received a '%s'", getTypeName(lhs)); raiseExcHelper(TypeError, "descriptor '__or__' requires a 'int' object but received a '%s'", getTypeName(lhs));
if (!PyInt_Check(rhs)) { if (!PyInt_Check(rhs)) {
return NotImplemented; return incref(NotImplemented);
} }
BoxedInt* rhs_int = static_cast<BoxedInt*>(rhs); BoxedInt* rhs_int = static_cast<BoxedInt*>(rhs);
return boxInt(lhs->n | rhs_int->n); return boxInt(lhs->n | rhs_int->n);
...@@ -564,7 +564,7 @@ extern "C" Box* intXor(BoxedInt* lhs, Box* rhs) { ...@@ -564,7 +564,7 @@ extern "C" Box* intXor(BoxedInt* lhs, Box* rhs) {
raiseExcHelper(TypeError, "descriptor '__xor__' requires a 'int' object but received a '%s'", getTypeName(lhs)); raiseExcHelper(TypeError, "descriptor '__xor__' requires a 'int' object but received a '%s'", getTypeName(lhs));
if (!PyInt_Check(rhs)) { if (!PyInt_Check(rhs)) {
return NotImplemented; return incref(NotImplemented);
} }
BoxedInt* rhs_int = static_cast<BoxedInt*>(rhs); BoxedInt* rhs_int = static_cast<BoxedInt*>(rhs);
return boxInt(lhs->n ^ rhs_int->n); return boxInt(lhs->n ^ rhs_int->n);
...@@ -595,7 +595,7 @@ extern "C" Box* intDiv(BoxedInt* lhs, Box* rhs) { ...@@ -595,7 +595,7 @@ extern "C" Box* intDiv(BoxedInt* lhs, Box* rhs) {
} else if (rhs->cls == float_cls) { } else if (rhs->cls == float_cls) {
return intDivFloat(lhs, static_cast<BoxedFloat*>(rhs)); return intDivFloat(lhs, static_cast<BoxedFloat*>(rhs));
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -625,7 +625,7 @@ extern "C" Box* intFloordiv(BoxedInt* lhs, Box* rhs) { ...@@ -625,7 +625,7 @@ extern "C" Box* intFloordiv(BoxedInt* lhs, Box* rhs) {
} else if (rhs->cls == float_cls) { } else if (rhs->cls == float_cls) {
return intFloordivFloat(lhs, static_cast<BoxedFloat*>(rhs)); return intFloordivFloat(lhs, static_cast<BoxedFloat*>(rhs));
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -659,7 +659,7 @@ extern "C" Box* intTruediv(BoxedInt* lhs, Box* rhs) { ...@@ -659,7 +659,7 @@ extern "C" Box* intTruediv(BoxedInt* lhs, Box* rhs) {
} else if (rhs->cls == float_cls) { } else if (rhs->cls == float_cls) {
return intTruedivFloat(lhs, static_cast<BoxedFloat*>(rhs)); return intTruedivFloat(lhs, static_cast<BoxedFloat*>(rhs));
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -688,7 +688,7 @@ extern "C" Box* intLShift(BoxedInt* lhs, Box* rhs) { ...@@ -688,7 +688,7 @@ extern "C" Box* intLShift(BoxedInt* lhs, Box* rhs) {
return longLshift(boxLong(lhs->n), rhs); return longLshift(boxLong(lhs->n), rhs);
if (!PyInt_Check(rhs)) { if (!PyInt_Check(rhs)) {
return NotImplemented; return incref(NotImplemented);
} }
BoxedInt* rhs_int = static_cast<BoxedInt*>(rhs); BoxedInt* rhs_int = static_cast<BoxedInt*>(rhs);
return intLShiftInt(lhs, rhs_int); return intLShiftInt(lhs, rhs_int);
...@@ -705,7 +705,7 @@ extern "C" Box* intMod(BoxedInt* lhs, Box* rhs) { ...@@ -705,7 +705,7 @@ extern "C" Box* intMod(BoxedInt* lhs, Box* rhs) {
raiseExcHelper(TypeError, "descriptor '__mod__' requires a 'int' object but received a '%s'", getTypeName(lhs)); raiseExcHelper(TypeError, "descriptor '__mod__' requires a 'int' object but received a '%s'", getTypeName(lhs));
if (!PyInt_Check(rhs)) { if (!PyInt_Check(rhs)) {
return NotImplemented; return incref(NotImplemented);
} }
BoxedInt* rhs_int = static_cast<BoxedInt*>(rhs); BoxedInt* rhs_int = static_cast<BoxedInt*>(rhs);
return boxInt(mod_i64_i64(lhs->n, rhs_int->n)); return boxInt(mod_i64_i64(lhs->n, rhs_int->n));
...@@ -719,13 +719,13 @@ extern "C" Box* intDivmod(BoxedInt* lhs, Box* rhs) { ...@@ -719,13 +719,13 @@ extern "C" Box* intDivmod(BoxedInt* lhs, Box* rhs) {
Box* divResult = intDiv(lhs, rhs); Box* divResult = intDiv(lhs, rhs);
if (divResult == NotImplemented) { if (divResult == NotImplemented) {
return NotImplemented; return incref(NotImplemented);
} }
Box* modResult = intMod(lhs, rhs); Box* modResult = intMod(lhs, rhs);
if (modResult == NotImplemented) { if (modResult == NotImplemented) {
return NotImplemented; return incref(NotImplemented);
} }
Box* arg[2] = { divResult, modResult }; Box* arg[2] = { divResult, modResult };
...@@ -756,7 +756,7 @@ extern "C" Box* intMul(BoxedInt* lhs, Box* rhs) { ...@@ -756,7 +756,7 @@ extern "C" Box* intMul(BoxedInt* lhs, Box* rhs) {
BoxedFloat* rhs_float = static_cast<BoxedFloat*>(rhs); BoxedFloat* rhs_float = static_cast<BoxedFloat*>(rhs);
return intMulFloat(lhs, rhs_float); return intMulFloat(lhs, rhs_float);
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -796,7 +796,7 @@ extern "C" Box* intPow(BoxedInt* lhs, Box* rhs, Box* mod) { ...@@ -796,7 +796,7 @@ extern "C" Box* intPow(BoxedInt* lhs, Box* rhs, Box* mod) {
else if (PyFloat_Check(rhs)) else if (PyFloat_Check(rhs))
return intPowFloat(lhs, static_cast<BoxedFloat*>(rhs), mod); return intPowFloat(lhs, static_cast<BoxedFloat*>(rhs), mod);
else if (!PyInt_Check(rhs)) else if (!PyInt_Check(rhs))
return NotImplemented; return incref(NotImplemented);
BoxedInt* rhs_int = static_cast<BoxedInt*>(rhs); BoxedInt* rhs_int = static_cast<BoxedInt*>(rhs);
BoxedInt* mod_int = static_cast<BoxedInt*>(mod); BoxedInt* mod_int = static_cast<BoxedInt*>(mod);
...@@ -806,7 +806,7 @@ extern "C" Box* intPow(BoxedInt* lhs, Box* rhs, Box* mod) { ...@@ -806,7 +806,7 @@ extern "C" Box* intPow(BoxedInt* lhs, Box* rhs, Box* mod) {
raiseExcHelper(TypeError, "pow() 2nd argument " raiseExcHelper(TypeError, "pow() 2nd argument "
"cannot be negative when 3rd argument specified"); "cannot be negative when 3rd argument specified");
if (!PyInt_Check(mod)) { if (!PyInt_Check(mod)) {
return NotImplemented; return incref(NotImplemented);
} else if (mod_int->n == 0) { } else if (mod_int->n == 0) {
raiseExcHelper(ValueError, "pow() 3rd argument cannot be 0"); raiseExcHelper(ValueError, "pow() 3rd argument cannot be 0");
} }
...@@ -837,7 +837,7 @@ extern "C" Box* intRShift(BoxedInt* lhs, Box* rhs) { ...@@ -837,7 +837,7 @@ extern "C" Box* intRShift(BoxedInt* lhs, Box* rhs) {
return longRshift(boxLong(lhs->n), rhs); return longRshift(boxLong(lhs->n), rhs);
if (!PyInt_Check(rhs)) { if (!PyInt_Check(rhs)) {
return NotImplemented; return incref(NotImplemented);
} }
BoxedInt* rhs_int = static_cast<BoxedInt*>(rhs); BoxedInt* rhs_int = static_cast<BoxedInt*>(rhs);
return intRShiftInt(lhs, rhs_int); return intRShiftInt(lhs, rhs_int);
...@@ -866,7 +866,7 @@ extern "C" Box* intSub(BoxedInt* lhs, Box* rhs) { ...@@ -866,7 +866,7 @@ extern "C" Box* intSub(BoxedInt* lhs, Box* rhs) {
BoxedFloat* rhs_float = static_cast<BoxedFloat*>(rhs); BoxedFloat* rhs_float = static_cast<BoxedFloat*>(rhs);
return intSubFloat(lhs, rhs_float); return intSubFloat(lhs, rhs_float);
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
......
...@@ -198,6 +198,9 @@ Box* _listSlice(BoxedList* self, i64 start, i64 stop, i64 step, i64 length) { ...@@ -198,6 +198,9 @@ Box* _listSlice(BoxedList* self, i64 start, i64 stop, i64 step, i64 length) {
if (length > 0) { if (length > 0) {
rtn->ensure(length); rtn->ensure(length);
copySlice(&rtn->elts->elts[0], &self->elts->elts[0], start, step, length); copySlice(&rtn->elts->elts[0], &self->elts->elts[0], start, step, length);
for (int i = 0; i < length; i++) {
Py_INCREF(rtn->elts->elts[i]);
}
rtn->size += length; rtn->size += length;
} }
return rtn; return rtn;
...@@ -235,7 +238,7 @@ static Box* list_slice(Box* o, Py_ssize_t ilow, Py_ssize_t ihigh) noexcept { ...@@ -235,7 +238,7 @@ static Box* list_slice(Box* o, Py_ssize_t ilow, Py_ssize_t ihigh) noexcept {
return (PyObject*)np; return (PyObject*)np;
} }
static inline Box* listGetitemUnboxed(BoxedList* self, int64_t n) { static inline BORROWED(Box*) listGetitemUnboxed(BoxedList* self, int64_t n) {
assert(PyList_Check(self)); assert(PyList_Check(self));
if (n < 0) if (n < 0)
n = self->size + n; n = self->size + n;
...@@ -249,7 +252,7 @@ static inline Box* listGetitemUnboxed(BoxedList* self, int64_t n) { ...@@ -249,7 +252,7 @@ static inline Box* listGetitemUnboxed(BoxedList* self, int64_t n) {
extern "C" Box* listGetitemInt(BoxedList* self, BoxedInt* slice) { extern "C" Box* listGetitemInt(BoxedList* self, BoxedInt* slice) {
assert(PyInt_Check(slice)); assert(PyInt_Check(slice));
return listGetitemUnboxed(self, slice->n); return incref(listGetitemUnboxed(self, slice->n));
} }
extern "C" PyObject* PyList_GetItem(PyObject* op, Py_ssize_t i) noexcept { extern "C" PyObject* PyList_GetItem(PyObject* op, Py_ssize_t i) noexcept {
...@@ -293,7 +296,7 @@ extern "C" Box* listGetslice(BoxedList* self, Box* boxedStart, Box* boxedStop) { ...@@ -293,7 +296,7 @@ extern "C" Box* listGetslice(BoxedList* self, Box* boxedStart, Box* boxedStop) {
static PyObject* list_item(PyListObject* a, Py_ssize_t i) noexcept { static PyObject* list_item(PyListObject* a, Py_ssize_t i) noexcept {
try { try {
BoxedList* self = (BoxedList*)a; BoxedList* self = (BoxedList*)a;
return listGetitemUnboxed(self, i); return incref(listGetitemUnboxed(self, i));
} catch (ExcInfo e) { } catch (ExcInfo e) {
setCAPIException(e); setCAPIException(e);
return NULL; return NULL;
...@@ -315,7 +318,7 @@ template <ExceptionStyle S> Box* listGetitem(BoxedList* self, Box* slice) { ...@@ -315,7 +318,7 @@ template <ExceptionStyle S> Box* listGetitem(BoxedList* self, Box* slice) {
Py_ssize_t i = PyNumber_AsSsize_t(slice, PyExc_IndexError); Py_ssize_t i = PyNumber_AsSsize_t(slice, PyExc_IndexError);
if (i == -1 && PyErr_Occurred()) if (i == -1 && PyErr_Occurred())
throwCAPIException(); throwCAPIException();
return listGetitemUnboxed(self, i); return incref(listGetitemUnboxed(self, i));
} else if (slice->cls == slice_cls) { } else if (slice->cls == slice_cls) {
return listGetitemSlice<CXX>(self, static_cast<BoxedSlice*>(slice)); return listGetitemSlice<CXX>(self, static_cast<BoxedSlice*>(slice));
} else { } else {
...@@ -331,7 +334,10 @@ static void _listSetitem(BoxedList* self, int64_t n, Box* v) { ...@@ -331,7 +334,10 @@ static void _listSetitem(BoxedList* self, int64_t n, Box* v) {
raiseExcHelper(IndexError, "list index out of range"); raiseExcHelper(IndexError, "list index out of range");
} }
Py_INCREF(v);
Box* prev = self->elts->elts[n];
self->elts->elts[n] = v; self->elts->elts[n] = v;
Py_DECREF(prev);
} }
extern "C" Box* listSetitemUnboxed(BoxedList* self, int64_t n, Box* v) { extern "C" Box* listSetitemUnboxed(BoxedList* self, int64_t n, Box* v) {
...@@ -524,10 +530,11 @@ static inline void listSetitemSliceInt64(BoxedList* self, i64 start, i64 stop, i ...@@ -524,10 +530,11 @@ static inline void listSetitemSliceInt64(BoxedList* self, i64 start, i64 stop, i
v_size = 0; v_size = 0;
v_elts = NULL; v_elts = NULL;
} else { } else {
if (self == v) // handle self assignment by creating a copy if (self == v) { // handle self assignment by creating a copy
v = _listSlice(self, 0, self->size, 1, self->size); v_as_seq = _listSlice(self, 0, self->size, 1, self->size);
assert(v_as_seq->cls == list_cls);
v_as_seq = PySequence_Fast(v, "can only assign an iterable"); } else
v_as_seq = PySequence_Fast(v, "can only assign an iterable");
if (v_as_seq == NULL) if (v_as_seq == NULL)
throwCAPIException(); throwCAPIException();
...@@ -632,8 +639,10 @@ extern "C" Box* listDelitemInt(BoxedList* self, BoxedInt* slice) { ...@@ -632,8 +639,10 @@ extern "C" Box* listDelitemInt(BoxedList* self, BoxedInt* slice) {
if (n < 0 || n >= self->size) { if (n < 0 || n >= self->size) {
raiseExcHelper(IndexError, "list index out of range"); raiseExcHelper(IndexError, "list index out of range");
} }
Box* e = self->elts->elts[n];
memmove(self->elts->elts + n, self->elts->elts + n + 1, (self->size - n - 1) * sizeof(Box*)); memmove(self->elts->elts + n, self->elts->elts + n + 1, (self->size - n - 1) * sizeof(Box*));
self->size--; self->size--;
Py_DECREF(e);
Py_RETURN_NONE; Py_RETURN_NONE;
} }
...@@ -708,8 +717,6 @@ extern "C" int PyList_Insert(PyObject* op, Py_ssize_t where, PyObject* newitem) ...@@ -708,8 +717,6 @@ extern "C" int PyList_Insert(PyObject* op, Py_ssize_t where, PyObject* newitem)
} }
Box* listMul(BoxedList* self, Box* rhs) { Box* listMul(BoxedList* self, Box* rhs) {
static BoxedString* index_str = getStaticString("__index__");
Py_ssize_t n = PyNumber_AsSsize_t(rhs, PyExc_IndexError); Py_ssize_t n = PyNumber_AsSsize_t(rhs, PyExc_IndexError);
if (n == -1 && PyErr_Occurred()) if (n == -1 && PyErr_Occurred())
throwCAPIException(); throwCAPIException();
...@@ -732,8 +739,6 @@ Box* listMul(BoxedList* self, Box* rhs) { ...@@ -732,8 +739,6 @@ Box* listMul(BoxedList* self, Box* rhs) {
} }
Box* listImul(BoxedList* self, Box* rhs) { Box* listImul(BoxedList* self, Box* rhs) {
static BoxedString* index_str = getStaticString("__index__");
Py_ssize_t n = PyNumber_AsSsize_t(rhs, PyExc_IndexError); Py_ssize_t n = PyNumber_AsSsize_t(rhs, PyExc_IndexError);
if (n == -1 && PyErr_Occurred()) if (n == -1 && PyErr_Occurred())
throwCAPIException(); throwCAPIException();
...@@ -744,7 +749,7 @@ Box* listImul(BoxedList* self, Box* rhs) { ...@@ -744,7 +749,7 @@ Box* listImul(BoxedList* self, Box* rhs) {
if (n == 0) { if (n == 0) {
listSetitemSliceInt64(self, 0, s, 1, NULL); listSetitemSliceInt64(self, 0, s, 1, NULL);
} else if (n == 1) { } else if (n == 1) {
return self; return incref(self);
} else if (s == 1) { } else if (s == 1) {
for (long i = 1; i < n; i++) { for (long i = 1; i < n; i++) {
listAppendInternal(self, self->elts->elts[0]); listAppendInternal(self, self->elts->elts[0]);
...@@ -755,7 +760,7 @@ Box* listImul(BoxedList* self, Box* rhs) { ...@@ -755,7 +760,7 @@ Box* listImul(BoxedList* self, Box* rhs) {
} }
} }
return self; return incref(self);
} }
Box* listIAdd(BoxedList* self, Box* _rhs) { Box* listIAdd(BoxedList* self, Box* _rhs) {
...@@ -817,7 +822,7 @@ Box* listIAdd(BoxedList* self, Box* _rhs) { ...@@ -817,7 +822,7 @@ Box* listIAdd(BoxedList* self, Box* _rhs) {
Box* listAdd(BoxedList* self, Box* _rhs) { Box* listAdd(BoxedList* self, Box* _rhs) {
if (!PyList_Check(_rhs)) { if (!PyList_Check(_rhs)) {
return NotImplemented; return incref(NotImplemented);
raiseExcHelper(TypeError, "can only concatenate list (not \"%s\") to list", getTypeName(_rhs)); raiseExcHelper(TypeError, "can only concatenate list (not \"%s\") to list", getTypeName(_rhs));
} }
...@@ -832,6 +837,9 @@ Box* listAdd(BoxedList* self, Box* _rhs) { ...@@ -832,6 +837,9 @@ Box* listAdd(BoxedList* self, Box* _rhs) {
memcpy(rtn->elts->elts, self->elts->elts, sizeof(self->elts->elts[0]) * s1); memcpy(rtn->elts->elts, self->elts->elts, sizeof(self->elts->elts[0]) * s1);
memcpy(rtn->elts->elts + s1, rhs->elts->elts, sizeof(rhs->elts->elts[0]) * s2); memcpy(rtn->elts->elts + s1, rhs->elts->elts, sizeof(rhs->elts->elts[0]) * s2);
rtn->size = s1 + s2; rtn->size = s1 + s2;
for (int i = 0; i < s1 + s2; i++) {
Py_INCREF(rtn->elts->elts[i]);
}
return rtn; return rtn;
} }
...@@ -853,7 +861,7 @@ extern "C" int PyList_Reverse(PyObject* v) noexcept { ...@@ -853,7 +861,7 @@ extern "C" int PyList_Reverse(PyObject* v) noexcept {
} }
try { try {
listReverse(static_cast<BoxedList*>(v)); autoDecref(listReverse(static_cast<BoxedList*>(v)));
} catch (ExcInfo e) { } catch (ExcInfo e) {
setCAPIException(e); setCAPIException(e);
return -1; return -1;
...@@ -1086,7 +1094,7 @@ Box* listIndex(BoxedList* self, Box* elt, Box* _start, Box** args) { ...@@ -1086,7 +1094,7 @@ Box* listIndex(BoxedList* self, Box* elt, Box* _start, Box** args) {
} }
BoxedString* tostr = static_cast<BoxedString*>(repr(elt)); BoxedString* tostr = static_cast<BoxedString*>(repr(elt));
raiseExcHelper(ValueError, "%s is not in list", tostr->data()); raiseExcHelper(ValueError, "%s is not in list", autoDecref(tostr)->data());
} }
Box* listRemove(BoxedList* self, Box* elt) { Box* listRemove(BoxedList* self, Box* elt) {
...@@ -1102,6 +1110,7 @@ Box* listRemove(BoxedList* self, Box* elt) { ...@@ -1102,6 +1110,7 @@ Box* listRemove(BoxedList* self, Box* elt) {
if (r) { if (r) {
memmove(self->elts->elts + i, self->elts->elts + i + 1, (self->size - i - 1) * sizeof(Box*)); memmove(self->elts->elts + i, self->elts->elts + i + 1, (self->size - i - 1) * sizeof(Box*));
self->size--; self->size--;
Py_DECREF(e);
Py_RETURN_NONE; Py_RETURN_NONE;
} }
} }
...@@ -1195,7 +1204,7 @@ Box* _listCmp(BoxedList* lhs, BoxedList* rhs, AST_TYPE::AST_TYPE op_type) { ...@@ -1195,7 +1204,7 @@ Box* _listCmp(BoxedList* lhs, BoxedList* rhs, AST_TYPE::AST_TYPE op_type) {
Box* listEq(BoxedList* self, Box* rhs) { Box* listEq(BoxedList* self, Box* rhs) {
if (!PyList_Check(rhs)) { if (!PyList_Check(rhs)) {
return NotImplemented; return incref(NotImplemented);
} }
return _listCmp(self, static_cast<BoxedList*>(rhs), AST_TYPE::Eq); return _listCmp(self, static_cast<BoxedList*>(rhs), AST_TYPE::Eq);
...@@ -1203,7 +1212,7 @@ Box* listEq(BoxedList* self, Box* rhs) { ...@@ -1203,7 +1212,7 @@ Box* listEq(BoxedList* self, Box* rhs) {
Box* listNe(BoxedList* self, Box* rhs) { Box* listNe(BoxedList* self, Box* rhs) {
if (!PyList_Check(rhs)) { if (!PyList_Check(rhs)) {
return NotImplemented; return incref(NotImplemented);
} }
return _listCmp(self, static_cast<BoxedList*>(rhs), AST_TYPE::NotEq); return _listCmp(self, static_cast<BoxedList*>(rhs), AST_TYPE::NotEq);
...@@ -1211,7 +1220,7 @@ Box* listNe(BoxedList* self, Box* rhs) { ...@@ -1211,7 +1220,7 @@ Box* listNe(BoxedList* self, Box* rhs) {
Box* listLt(BoxedList* self, Box* rhs) { Box* listLt(BoxedList* self, Box* rhs) {
if (!PyList_Check(rhs)) { if (!PyList_Check(rhs)) {
return NotImplemented; return incref(NotImplemented);
} }
return _listCmp(self, static_cast<BoxedList*>(rhs), AST_TYPE::Lt); return _listCmp(self, static_cast<BoxedList*>(rhs), AST_TYPE::Lt);
...@@ -1219,7 +1228,7 @@ Box* listLt(BoxedList* self, Box* rhs) { ...@@ -1219,7 +1228,7 @@ Box* listLt(BoxedList* self, Box* rhs) {
Box* listLe(BoxedList* self, Box* rhs) { Box* listLe(BoxedList* self, Box* rhs) {
if (!PyList_Check(rhs)) { if (!PyList_Check(rhs)) {
return NotImplemented; return incref(NotImplemented);
} }
return _listCmp(self, static_cast<BoxedList*>(rhs), AST_TYPE::LtE); return _listCmp(self, static_cast<BoxedList*>(rhs), AST_TYPE::LtE);
...@@ -1227,7 +1236,7 @@ Box* listLe(BoxedList* self, Box* rhs) { ...@@ -1227,7 +1236,7 @@ Box* listLe(BoxedList* self, Box* rhs) {
Box* listGt(BoxedList* self, Box* rhs) { Box* listGt(BoxedList* self, Box* rhs) {
if (!PyList_Check(rhs)) { if (!PyList_Check(rhs)) {
return NotImplemented; return incref(NotImplemented);
} }
return _listCmp(self, static_cast<BoxedList*>(rhs), AST_TYPE::Gt); return _listCmp(self, static_cast<BoxedList*>(rhs), AST_TYPE::Gt);
...@@ -1235,7 +1244,7 @@ Box* listGt(BoxedList* self, Box* rhs) { ...@@ -1235,7 +1244,7 @@ Box* listGt(BoxedList* self, Box* rhs) {
Box* listGe(BoxedList* self, Box* rhs) { Box* listGe(BoxedList* self, Box* rhs) {
if (!PyList_Check(rhs)) { if (!PyList_Check(rhs)) {
return NotImplemented; return incref(NotImplemented);
} }
return _listCmp(self, static_cast<BoxedList*>(rhs), AST_TYPE::GtE); return _listCmp(self, static_cast<BoxedList*>(rhs), AST_TYPE::GtE);
......
...@@ -32,7 +32,7 @@ public: ...@@ -32,7 +32,7 @@ public:
static void dealloc(BoxedListIterator* o) noexcept { static void dealloc(BoxedListIterator* o) noexcept {
PyObject_GC_UnTrack(o); PyObject_GC_UnTrack(o);
Py_DECREF(o->l); Py_XDECREF(o->l);
o->cls->tp_free(o); o->cls->tp_free(o);
} }
......
...@@ -861,7 +861,7 @@ Box* longAdd(BoxedLong* v1, Box* _v2) { ...@@ -861,7 +861,7 @@ Box* longAdd(BoxedLong* v1, Box* _v2) {
mpz_sub_ui(r->n, v1->n, -v2->n); mpz_sub_ui(r->n, v1->n, -v2->n);
return r; return r;
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -889,7 +889,7 @@ extern "C" Box* longAnd(BoxedLong* v1, Box* _v2) { ...@@ -889,7 +889,7 @@ extern "C" Box* longAnd(BoxedLong* v1, Box* _v2) {
mpz_and(r->n, v1->n, v2_long); mpz_and(r->n, v1->n, v2_long);
return r; return r;
} }
return NotImplemented; return incref(NotImplemented);
} }
extern "C" Box* longOr(BoxedLong* v1, Box* _v2) { extern "C" Box* longOr(BoxedLong* v1, Box* _v2) {
...@@ -915,7 +915,7 @@ extern "C" Box* longOr(BoxedLong* v1, Box* _v2) { ...@@ -915,7 +915,7 @@ extern "C" Box* longOr(BoxedLong* v1, Box* _v2) {
mpz_ior(r->n, v1->n, v2_long); mpz_ior(r->n, v1->n, v2_long);
return r; return r;
} }
return NotImplemented; return incref(NotImplemented);
} }
extern "C" Box* longXor(BoxedLong* v1, Box* _v2) { extern "C" Box* longXor(BoxedLong* v1, Box* _v2) {
...@@ -941,7 +941,7 @@ extern "C" Box* longXor(BoxedLong* v1, Box* _v2) { ...@@ -941,7 +941,7 @@ extern "C" Box* longXor(BoxedLong* v1, Box* _v2) {
mpz_xor(r->n, v1->n, v2_long); mpz_xor(r->n, v1->n, v2_long);
return r; return r;
} }
return NotImplemented; return incref(NotImplemented);
} }
static PyObject* long_richcompare(Box* _v1, Box* _v2, int op) noexcept { static PyObject* long_richcompare(Box* _v1, Box* _v2, int op) noexcept {
...@@ -957,7 +957,7 @@ static PyObject* long_richcompare(Box* _v1, Box* _v2, int op) noexcept { ...@@ -957,7 +957,7 @@ static PyObject* long_richcompare(Box* _v1, Box* _v2, int op) noexcept {
return convert_3way_to_object(op, mpz_cmp_si(v1->n, v2->n)); return convert_3way_to_object(op, mpz_cmp_si(v1->n, v2->n));
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -987,7 +987,7 @@ Box* longLshift(BoxedLong* v1, Box* _v2) { ...@@ -987,7 +987,7 @@ Box* longLshift(BoxedLong* v1, Box* _v2) {
mpz_mul_2exp(r->n, v1->n, v2->n); mpz_mul_2exp(r->n, v1->n, v2->n);
return r; return r;
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -1017,7 +1017,7 @@ Box* longRshift(BoxedLong* v1, Box* _v2) { ...@@ -1017,7 +1017,7 @@ Box* longRshift(BoxedLong* v1, Box* _v2) {
mpz_div_2exp(r->n, v1->n, v2->n); mpz_div_2exp(r->n, v1->n, v2->n);
return r; return r;
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -1043,7 +1043,7 @@ Box* longSub(BoxedLong* v1, Box* _v2) { ...@@ -1043,7 +1043,7 @@ Box* longSub(BoxedLong* v1, Box* _v2) {
mpz_add_ui(r->n, v1->n, -v2->n); mpz_add_ui(r->n, v1->n, -v2->n);
return r; return r;
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -1074,7 +1074,7 @@ Box* longMul(BoxedLong* v1, Box* _v2) { ...@@ -1074,7 +1074,7 @@ Box* longMul(BoxedLong* v1, Box* _v2) {
mpz_mul_si(r->n, v1->n, v2->n); mpz_mul_si(r->n, v1->n, v2->n);
return r; return r;
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -1103,7 +1103,7 @@ Box* longDiv(BoxedLong* v1, Box* _v2) { ...@@ -1103,7 +1103,7 @@ Box* longDiv(BoxedLong* v1, Box* _v2) {
mpz_fdiv_q(r->n, v1->n, r->n); mpz_fdiv_q(r->n, v1->n, r->n);
return r; return r;
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -1139,7 +1139,7 @@ Box* longMod(BoxedLong* v1, Box* _v2) { ...@@ -1139,7 +1139,7 @@ Box* longMod(BoxedLong* v1, Box* _v2) {
mpz_mmod(r->n, v1->n, r->n); mpz_mmod(r->n, v1->n, r->n);
return r; return r;
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -1155,7 +1155,7 @@ Box* longRMod(BoxedLong* v1, Box* _v2) { ...@@ -1155,7 +1155,7 @@ Box* longRMod(BoxedLong* v1, Box* _v2) {
} else if (PyInt_Check(lhs)) { } else if (PyInt_Check(lhs)) {
return longMod(boxLong(((BoxedInt*)lhs)->n), rhs); return longMod(boxLong(((BoxedInt*)lhs)->n), rhs);
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -1189,7 +1189,7 @@ extern "C" Box* longDivmod(BoxedLong* lhs, Box* _rhs) { ...@@ -1189,7 +1189,7 @@ extern "C" Box* longDivmod(BoxedLong* lhs, Box* _rhs) {
mpz_fdiv_qr(q->n, r->n, lhs->n, r->n); mpz_fdiv_qr(q->n, r->n, lhs->n, r->n);
return BoxedTuple::create({ q, r }); return BoxedTuple::create({ q, r });
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -1216,7 +1216,7 @@ Box* longRdiv(BoxedLong* v1, Box* _v2) { ...@@ -1216,7 +1216,7 @@ Box* longRdiv(BoxedLong* v1, Box* _v2) {
mpz_fdiv_q(r->n, r->n, v1->n); mpz_fdiv_q(r->n, r->n, v1->n);
return r; return r;
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -1237,10 +1237,10 @@ Box* longTrueDiv(BoxedLong* v1, Box* _v2) { ...@@ -1237,10 +1237,10 @@ Box* longTrueDiv(BoxedLong* v1, Box* _v2) {
int overflow = 0; int overflow = 0;
long lhs = PyLong_AsLongAndOverflow(v1, &overflow); long lhs = PyLong_AsLongAndOverflow(v1, &overflow);
if (overflow) if (overflow)
return NotImplemented; return incref(NotImplemented);
long rhs = PyLong_AsLongAndOverflow(_v2, &overflow); long rhs = PyLong_AsLongAndOverflow(_v2, &overflow);
if (overflow) if (overflow)
return NotImplemented; return incref(NotImplemented);
if (rhs == 0) if (rhs == 0)
raiseExcHelper(ZeroDivisionError, "division by zero"); raiseExcHelper(ZeroDivisionError, "division by zero");
...@@ -1256,10 +1256,10 @@ Box* longRTrueDiv(BoxedLong* v1, Box* _v2) { ...@@ -1256,10 +1256,10 @@ Box* longRTrueDiv(BoxedLong* v1, Box* _v2) {
int overflow = 0; int overflow = 0;
long lhs = PyLong_AsLongAndOverflow(_v2, &overflow); long lhs = PyLong_AsLongAndOverflow(_v2, &overflow);
if (overflow) if (overflow)
return NotImplemented; return incref(NotImplemented);
long rhs = PyLong_AsLongAndOverflow(v1, &overflow); long rhs = PyLong_AsLongAndOverflow(v1, &overflow);
if (overflow) if (overflow)
return NotImplemented; return incref(NotImplemented);
if (rhs == 0) if (rhs == 0)
raiseExcHelper(ZeroDivisionError, "division by zero"); raiseExcHelper(ZeroDivisionError, "division by zero");
...@@ -1295,7 +1295,7 @@ Box* longPow(BoxedLong* lhs, Box* rhs, Box* mod) { ...@@ -1295,7 +1295,7 @@ Box* longPow(BoxedLong* lhs, Box* rhs, Box* mod) {
} else if (PyInt_Check(mod)) { } else if (PyInt_Check(mod)) {
mod_long = boxLong(static_cast<BoxedInt*>(mod)->n); mod_long = boxLong(static_cast<BoxedInt*>(mod)->n);
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -1305,7 +1305,7 @@ Box* longPow(BoxedLong* lhs, Box* rhs, Box* mod) { ...@@ -1305,7 +1305,7 @@ Box* longPow(BoxedLong* lhs, Box* rhs, Box* mod) {
} else if (PyInt_Check(rhs)) { } else if (PyInt_Check(rhs)) {
rhs_long = boxLong(static_cast<BoxedInt*>(rhs)->n); rhs_long = boxLong(static_cast<BoxedInt*>(rhs)->n);
} else { } else {
return NotImplemented; return incref(NotImplemented);
} }
if (mod != None) { if (mod != None) {
......
...@@ -3460,6 +3460,7 @@ Box* callattrInternal(Box* obj, BoxedString* attr, LookupScope scope, CallattrRe ...@@ -3460,6 +3460,7 @@ Box* callattrInternal(Box* obj, BoxedString* attr, LookupScope scope, CallattrRe
arg_array->setAttr(8, rewrite_args->rewriter->loadConst(0)); arg_array->setAttr(8, rewrite_args->rewriter->loadConst(0));
auto r_rtn = rewrite_args->rewriter->call(true, (void*)Helper::call, arg_vec)->setType(RefType::OWNED); auto r_rtn = rewrite_args->rewriter->call(true, (void*)Helper::call, arg_vec)->setType(RefType::OWNED);
rewrite_args->obj->refConsumed();
rewrite_args->setReturn(r_rtn, S == CXX ? ReturnConvention::HAS_RETURN : ReturnConvention::CAPI_RETURN); rewrite_args->setReturn(r_rtn, S == CXX ? ReturnConvention::HAS_RETURN : ReturnConvention::CAPI_RETURN);
void* _args[2] = { args, const_cast<std::vector<BoxedString*>*>(keyword_names) }; void* _args[2] = { args, const_cast<std::vector<BoxedString*>*>(keyword_names) };
...@@ -5948,6 +5949,7 @@ extern "C" void setitem(Box* target, Box* slice, Box* value) { ...@@ -5948,6 +5949,7 @@ extern "C" void setitem(Box* target, Box* slice, Box* value) {
if (rtn == NULL) { if (rtn == NULL) {
raiseExcHelper(TypeError, "'%s' object does not support item assignment", getTypeName(target)); raiseExcHelper(TypeError, "'%s' object does not support item assignment", getTypeName(target));
} }
Py_DECREF(rtn);
if (rewriter.get()) if (rewriter.get())
rewriter->commit(); rewriter->commit();
...@@ -5984,6 +5986,8 @@ extern "C" void delitem(Box* target, Box* slice) { ...@@ -5984,6 +5986,8 @@ extern "C" void delitem(Box* target, Box* slice) {
raiseExcHelper(TypeError, "'%s' object does not support item deletion", getTypeName(target)); raiseExcHelper(TypeError, "'%s' object does not support item deletion", getTypeName(target));
} }
Py_DECREF(rtn);
if (rewriter.get()) if (rewriter.get())
rewriter->commit(); rewriter->commit();
} }
......
...@@ -274,7 +274,7 @@ static Box* setIntersectionUpdate2(BoxedSet* self, Box* other) { ...@@ -274,7 +274,7 @@ static Box* setIntersectionUpdate2(BoxedSet* self, Box* other) {
Box* setIOr(BoxedSet* lhs, BoxedSet* rhs) { Box* setIOr(BoxedSet* lhs, BoxedSet* rhs) {
RELEASE_ASSERT(PyAnySet_Check(lhs), ""); RELEASE_ASSERT(PyAnySet_Check(lhs), "");
if (!PyAnySet_Check(rhs)) if (!PyAnySet_Check(rhs))
return NotImplemented; return incref(NotImplemented);
// TODO just [write and] call setUnionUpdate2 // TODO just [write and] call setUnionUpdate2
for (auto&& elt : rhs->s) { for (auto&& elt : rhs->s) {
...@@ -286,7 +286,7 @@ Box* setIOr(BoxedSet* lhs, BoxedSet* rhs) { ...@@ -286,7 +286,7 @@ Box* setIOr(BoxedSet* lhs, BoxedSet* rhs) {
Box* setOr(BoxedSet* lhs, BoxedSet* rhs) { Box* setOr(BoxedSet* lhs, BoxedSet* rhs) {
RELEASE_ASSERT(PyAnySet_Check(lhs), ""); RELEASE_ASSERT(PyAnySet_Check(lhs), "");
if (!PyAnySet_Check(rhs)) if (!PyAnySet_Check(rhs))
return NotImplemented; return incref(NotImplemented);
BoxedSet* rtn = makeNewSet(lhs->cls, lhs); BoxedSet* rtn = makeNewSet(lhs->cls, lhs);
return setIOr(rtn, rhs); return setIOr(rtn, rhs);
...@@ -295,7 +295,7 @@ Box* setOr(BoxedSet* lhs, BoxedSet* rhs) { ...@@ -295,7 +295,7 @@ Box* setOr(BoxedSet* lhs, BoxedSet* rhs) {
Box* setIAnd(BoxedSet* lhs, BoxedSet* rhs) { Box* setIAnd(BoxedSet* lhs, BoxedSet* rhs) {
RELEASE_ASSERT(PyAnySet_Check(lhs), ""); RELEASE_ASSERT(PyAnySet_Check(lhs), "");
if (!PyAnySet_Check(rhs)) if (!PyAnySet_Check(rhs))
return NotImplemented; return incref(NotImplemented);
setIntersectionUpdate2(lhs, rhs); setIntersectionUpdate2(lhs, rhs);
return lhs; return lhs;
...@@ -304,7 +304,7 @@ Box* setIAnd(BoxedSet* lhs, BoxedSet* rhs) { ...@@ -304,7 +304,7 @@ Box* setIAnd(BoxedSet* lhs, BoxedSet* rhs) {
Box* setAnd(BoxedSet* lhs, BoxedSet* rhs) { Box* setAnd(BoxedSet* lhs, BoxedSet* rhs) {
RELEASE_ASSERT(PyAnySet_Check(lhs), ""); RELEASE_ASSERT(PyAnySet_Check(lhs), "");
if (!PyAnySet_Check(rhs)) if (!PyAnySet_Check(rhs))
return NotImplemented; return incref(NotImplemented);
return setIntersection2(lhs, rhs); return setIntersection2(lhs, rhs);
} }
...@@ -312,7 +312,7 @@ Box* setAnd(BoxedSet* lhs, BoxedSet* rhs) { ...@@ -312,7 +312,7 @@ Box* setAnd(BoxedSet* lhs, BoxedSet* rhs) {
Box* setISub(BoxedSet* lhs, BoxedSet* rhs) { Box* setISub(BoxedSet* lhs, BoxedSet* rhs) {
RELEASE_ASSERT(PyAnySet_Check(lhs), ""); RELEASE_ASSERT(PyAnySet_Check(lhs), "");
if (!PyAnySet_Check(rhs)) if (!PyAnySet_Check(rhs))
return NotImplemented; return incref(NotImplemented);
// TODO: write and call setDifferenceUpdate2 // TODO: write and call setDifferenceUpdate2
for (auto&& elt : rhs->s) { for (auto&& elt : rhs->s) {
...@@ -324,7 +324,7 @@ Box* setISub(BoxedSet* lhs, BoxedSet* rhs) { ...@@ -324,7 +324,7 @@ Box* setISub(BoxedSet* lhs, BoxedSet* rhs) {
Box* setSub(BoxedSet* lhs, BoxedSet* rhs) { Box* setSub(BoxedSet* lhs, BoxedSet* rhs) {
RELEASE_ASSERT(PyAnySet_Check(lhs), ""); RELEASE_ASSERT(PyAnySet_Check(lhs), "");
if (!PyAnySet_Check(rhs)) if (!PyAnySet_Check(rhs))
return NotImplemented; return incref(NotImplemented);
BoxedSet* rtn = makeNewSet(lhs->cls, lhs); BoxedSet* rtn = makeNewSet(lhs->cls, lhs);
return setISub(rtn, rhs); return setISub(rtn, rhs);
...@@ -333,7 +333,7 @@ Box* setSub(BoxedSet* lhs, BoxedSet* rhs) { ...@@ -333,7 +333,7 @@ Box* setSub(BoxedSet* lhs, BoxedSet* rhs) {
Box* setIXor(BoxedSet* lhs, BoxedSet* rhs) { Box* setIXor(BoxedSet* lhs, BoxedSet* rhs) {
RELEASE_ASSERT(PyAnySet_Check(lhs), ""); RELEASE_ASSERT(PyAnySet_Check(lhs), "");
if (!PyAnySet_Check(rhs)) if (!PyAnySet_Check(rhs))
return NotImplemented; return incref(NotImplemented);
_setSymmetricDifferenceUpdate(lhs, rhs); _setSymmetricDifferenceUpdate(lhs, rhs);
...@@ -343,7 +343,7 @@ Box* setIXor(BoxedSet* lhs, BoxedSet* rhs) { ...@@ -343,7 +343,7 @@ Box* setIXor(BoxedSet* lhs, BoxedSet* rhs) {
Box* setXor(BoxedSet* lhs, BoxedSet* rhs) { Box* setXor(BoxedSet* lhs, BoxedSet* rhs) {
RELEASE_ASSERT(PyAnySet_Check(lhs), ""); RELEASE_ASSERT(PyAnySet_Check(lhs), "");
if (!PyAnySet_Check(rhs)) if (!PyAnySet_Check(rhs))
return NotImplemented; return incref(NotImplemented);
BoxedSet* rtn = makeNewSet(lhs->cls, lhs); BoxedSet* rtn = makeNewSet(lhs->cls, lhs);
return setIXor(rtn, rhs); return setIXor(rtn, rhs);
......
...@@ -349,8 +349,8 @@ extern "C" Box* strAdd(BoxedString* lhs, Box* _rhs) { ...@@ -349,8 +349,8 @@ extern "C" Box* strAdd(BoxedString* lhs, Box* _rhs) {
} else { } else {
// This is a compatibility break with CPython, which has their sq_concat method // This is a compatibility break with CPython, which has their sq_concat method
// directly throw a TypeError. Since we're not implementing this as a sq_concat, // directly throw a TypeError. Since we're not implementing this as a sq_concat,
// return NotImplemented for now. // Give NotImplemented for now.
return NotImplemented; return incref(NotImplemented);
} }
} }
...@@ -1196,7 +1196,7 @@ Box* str_richcompare(Box* lhs, Box* rhs, int op) { ...@@ -1196,7 +1196,7 @@ Box* str_richcompare(Box* lhs, Box* rhs, int op) {
// statement, rather than out here. It's functionally equivalent but the // statement, rather than out here. It's functionally equivalent but the
// generated assembly is somehow quite better: // generated assembly is somehow quite better:
// if (unlikely(!PyString_Check(rhs))) // if (unlikely(!PyString_Check(rhs)))
// return NotImplemented; // return incref(NotImplemented);
BoxedString* slhs = static_cast<BoxedString*>(lhs); BoxedString* slhs = static_cast<BoxedString*>(lhs);
BoxedString* srhs = static_cast<BoxedString*>(rhs); BoxedString* srhs = static_cast<BoxedString*>(rhs);
...@@ -1204,27 +1204,27 @@ Box* str_richcompare(Box* lhs, Box* rhs, int op) { ...@@ -1204,27 +1204,27 @@ Box* str_richcompare(Box* lhs, Box* rhs, int op) {
switch (op) { switch (op) {
case Py_EQ: case Py_EQ:
if (unlikely(!PyString_Check(rhs))) if (unlikely(!PyString_Check(rhs)))
return NotImplemented; return incref(NotImplemented);
return boxBool(slhs->s() == srhs->s()); return boxBool(slhs->s() == srhs->s());
case Py_NE: case Py_NE:
if (unlikely(!PyString_Check(rhs))) if (unlikely(!PyString_Check(rhs)))
return NotImplemented; return incref(NotImplemented);
return boxBool(slhs->s() != srhs->s()); return boxBool(slhs->s() != srhs->s());
case Py_LT: case Py_LT:
if (unlikely(!PyString_Check(rhs))) if (unlikely(!PyString_Check(rhs)))
return NotImplemented; return incref(NotImplemented);
return boxBool(slhs->s() < srhs->s()); return boxBool(slhs->s() < srhs->s());
case Py_LE: case Py_LE:
if (unlikely(!PyString_Check(rhs))) if (unlikely(!PyString_Check(rhs)))
return NotImplemented; return incref(NotImplemented);
return boxBool(slhs->s() <= srhs->s()); return boxBool(slhs->s() <= srhs->s());
case Py_GT: case Py_GT:
if (unlikely(!PyString_Check(rhs))) if (unlikely(!PyString_Check(rhs)))
return NotImplemented; return incref(NotImplemented);
return boxBool(slhs->s() > srhs->s()); return boxBool(slhs->s() > srhs->s());
case Py_GE: case Py_GE:
if (unlikely(!PyString_Check(rhs))) if (unlikely(!PyString_Check(rhs)))
return NotImplemented; return incref(NotImplemented);
return boxBool(slhs->s() >= srhs->s()); return boxBool(slhs->s() >= srhs->s());
default: default:
llvm_unreachable("invalid op"); llvm_unreachable("invalid op");
......
...@@ -47,6 +47,9 @@ Box* _tupleSlice(BoxedTuple* self, i64 start, i64 stop, i64 step, i64 length) { ...@@ -47,6 +47,9 @@ Box* _tupleSlice(BoxedTuple* self, i64 start, i64 stop, i64 step, i64 length) {
auto rtn = BoxedTuple::create(length); auto rtn = BoxedTuple::create(length);
if (length > 0) if (length > 0)
copySlice(&rtn->elts[0], &self->elts[0], start, step, length); copySlice(&rtn->elts[0], &self->elts[0], start, step, length);
for (int i = 0; i < length; i++) {
Py_INCREF(rtn->elts[i]);
}
return rtn; return rtn;
} }
...@@ -157,7 +160,7 @@ template <ExceptionStyle S> Box* tupleGetitem(BoxedTuple* self, Box* slice) { ...@@ -157,7 +160,7 @@ template <ExceptionStyle S> Box* tupleGetitem(BoxedTuple* self, Box* slice) {
Box* tupleAdd(BoxedTuple* self, Box* rhs) { Box* tupleAdd(BoxedTuple* self, Box* rhs) {
if (!PyTuple_Check(rhs)) { if (!PyTuple_Check(rhs)) {
return NotImplemented; return incref(NotImplemented);
} }
BoxedTuple* _rhs = static_cast<BoxedTuple*>(rhs); BoxedTuple* _rhs = static_cast<BoxedTuple*>(rhs);
......
...@@ -2694,7 +2694,7 @@ Box* objectSetattr(Box* obj, Box* attr, Box* value) { ...@@ -2694,7 +2694,7 @@ Box* objectSetattr(Box* obj, Box* attr, Box* value) {
} }
Box* objectSubclasshook(Box* cls, Box* a) { Box* objectSubclasshook(Box* cls, Box* a) {
return NotImplemented; return incref(NotImplemented);
} }
static PyObject* import_copyreg(void) noexcept { static PyObject* import_copyreg(void) noexcept {
......
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