Commit 2e0b2c58 authored by Kevin Modzelewski's avatar Kevin Modzelewski

More refcount fixes

parent 28af3827
...@@ -467,9 +467,6 @@ void ASTInterpreter::doStore(AST_Name* node, STOLEN(Value) value) { ...@@ -467,9 +467,6 @@ void ASTInterpreter::doStore(AST_Name* node, STOLEN(Value) value) {
if (jit) if (jit)
jit->emitSetGlobal(globals, name.getBox(), value); jit->emitSetGlobal(globals, name.getBox(), value);
setGlobal(globals, name.getBox(), value.o); setGlobal(globals, name.getBox(), value.o);
Py_DECREF(value.o);
if (jit)
value.var->decvref();
} else if (vst == ScopeInfo::VarScopeType::NAME) { } else if (vst == ScopeInfo::VarScopeType::NAME) {
assert(0 && "check refcounting"); assert(0 && "check refcounting");
if (jit) if (jit)
...@@ -510,9 +507,12 @@ void ASTInterpreter::doStore(AST_expr* node, STOLEN(Value) value) { ...@@ -510,9 +507,12 @@ void ASTInterpreter::doStore(AST_expr* node, STOLEN(Value) value) {
} else if (node->type == AST_TYPE::Attribute) { } else if (node->type == AST_TYPE::Attribute) {
AST_Attribute* attr = (AST_Attribute*)node; AST_Attribute* attr = (AST_Attribute*)node;
Value o = visit_expr(attr->value); Value o = visit_expr(attr->value);
if (jit) if (jit) {
jit->emitSetAttr(node, o, attr->attr.getBox(), value); jit->emitSetAttr(node, o, attr->attr.getBox(), value);
o.var->decvref();
}
pyston::setattr(o.o, attr->attr.getBox(), value.o); pyston::setattr(o.o, attr->attr.getBox(), value.o);
Py_DECREF(o.o);
} else if (node->type == AST_TYPE::Tuple) { } else if (node->type == AST_TYPE::Tuple) {
AST_Tuple* tuple = (AST_Tuple*)node; AST_Tuple* tuple = (AST_Tuple*)node;
Box** array = unpackIntoArray(value.o, tuple->elts.size()); Box** array = unpackIntoArray(value.o, tuple->elts.size());
......
...@@ -671,7 +671,6 @@ public: ...@@ -671,7 +671,6 @@ public:
} }
void giveAttr(BoxedString* attr, Box* val); void giveAttr(BoxedString* attr, Box* val);
// for debugging mostly:
void clearAttrs(); void clearAttrs();
// getattr() does the equivalent of PyDict_GetItem(obj->dict, attr): it looks up the attribute's value on the // getattr() does the equivalent of PyDict_GetItem(obj->dict, attr): it looks up the attribute's value on the
......
...@@ -785,7 +785,7 @@ BoxedDict* Box::getDict() { ...@@ -785,7 +785,7 @@ BoxedDict* Box::getDict() {
static StatCounter box_getattr_slowpath("slowpath_box_getattr"); static StatCounter box_getattr_slowpath("slowpath_box_getattr");
template <Rewritable rewritable> Box* Box::getattr(BoxedString* attr, GetattrRewriteArgs* rewrite_args) { template <Rewritable rewritable> BORROWED(Box*) Box::getattr(BoxedString* attr, GetattrRewriteArgs* rewrite_args) {
if (rewritable == NOT_REWRITABLE) { if (rewritable == NOT_REWRITABLE) {
assert(!rewrite_args); assert(!rewrite_args);
rewrite_args = NULL; rewrite_args = NULL;
...@@ -2541,6 +2541,8 @@ void setattrGeneric(Box* obj, BoxedString* attr, STOLEN(Box*) val, SetattrRewrit ...@@ -2541,6 +2541,8 @@ void setattrGeneric(Box* obj, BoxedString* attr, STOLEN(Box*) val, SetattrRewrit
} }
obj->setattr(attr, val, rewrite_args); obj->setattr(attr, val, rewrite_args);
// TODO: make setattr() stealing
Py_DECREF(val);
} }
// TODO this should be in type_setattro // TODO this should be in type_setattro
...@@ -2639,10 +2641,8 @@ extern "C" void setattr(Box* obj, BoxedString* attr, Box* attr_val) { ...@@ -2639,10 +2641,8 @@ extern "C" void setattr(Box* obj, BoxedString* attr, Box* attr_val) {
} }
} }
// We should probably add this as a GC root, but we can cheat a little bit since // This is a borrowed reference so we don't need to register it
// we know it's not going to get deallocated:
static Box* object_setattr = object_cls->getattr(getStaticString("__setattr__")); static Box* object_setattr = object_cls->getattr(getStaticString("__setattr__"));
assert(object_setattr);
// I guess this check makes it ok for us to just rely on having guarded on the value of setattr without // I guess this check makes it ok for us to just rely on having guarded on the value of setattr without
// invalidating on deallocation, since we assume that object.__setattr__ will never get deallocated. // invalidating on deallocation, since we assume that object.__setattr__ will never get deallocated.
...@@ -6494,13 +6494,13 @@ extern "C" Box* getGlobal(Box* globals, BoxedString* name) { ...@@ -6494,13 +6494,13 @@ extern "C" Box* getGlobal(Box* globals, BoxedString* name) {
rtn = builtins_module->getattr(name); rtn = builtins_module->getattr(name);
} }
// XXX // XXX
#ifndef NDEBUG #ifndef NDEBUG
rewriter.release(); rewriter.release();
#endif #endif
assert(rtn->ob_refcnt > 0);
if (rtn) { if (rtn) {
assert(rtn->ob_refcnt > 0);
Py_INCREF(rtn); Py_INCREF(rtn);
return rtn; return rtn;
} }
...@@ -6529,7 +6529,7 @@ Box* getFromGlobals(Box* globals, BoxedString* name) { ...@@ -6529,7 +6529,7 @@ Box* getFromGlobals(Box* globals, BoxedString* name) {
} }
} }
extern "C" void setGlobal(Box* globals, BoxedString* name, Box* value) { extern "C" void setGlobal(Box* globals, BoxedString* name, STOLEN(Box*) value) {
if (globals->cls == attrwrapper_cls) { if (globals->cls == attrwrapper_cls) {
globals = unwrapAttrWrapper(globals); globals = unwrapAttrWrapper(globals);
RELEASE_ASSERT(globals->cls == module_cls, "%s", globals->cls->tp_name); RELEASE_ASSERT(globals->cls == module_cls, "%s", globals->cls->tp_name);
......
...@@ -222,7 +222,7 @@ extern "C" Box* getGlobal(Box* globals, BoxedString* name); ...@@ -222,7 +222,7 @@ extern "C" Box* getGlobal(Box* globals, BoxedString* name);
// Checks for the name just in the passed globals object, and returns NULL if it is not found. // Checks for the name just in the passed globals object, and returns NULL if it is not found.
// This includes if the globals object defined a custom __getattr__ method that threw an AttributeError. // This includes if the globals object defined a custom __getattr__ method that threw an AttributeError.
Box* getFromGlobals(Box* globals, BoxedString* name); Box* getFromGlobals(Box* globals, BoxedString* name);
extern "C" void setGlobal(Box* globals, BoxedString* name, Box* value); extern "C" void setGlobal(Box* globals, BoxedString* name, STOLEN(Box*) value);
extern "C" void delGlobal(Box* globals, BoxedString* name); extern "C" void delGlobal(Box* globals, BoxedString* name);
extern "C" void boxedLocalsSet(Box* boxedLocals, BoxedString* attr, Box* val); extern "C" void boxedLocalsSet(Box* boxedLocals, BoxedString* attr, Box* val);
......
...@@ -1157,28 +1157,25 @@ extern "C" Box* strMod(BoxedString* lhs, Box* rhs) { ...@@ -1157,28 +1157,25 @@ extern "C" Box* strMod(BoxedString* lhs, Box* rhs) {
extern "C" Box* strMul(BoxedString* lhs, Box* rhs) { extern "C" Box* strMul(BoxedString* lhs, Box* rhs) {
assert(PyString_Check(lhs)); assert(PyString_Check(lhs));
int n; Py_ssize_t n;
if (PyIndex_Check(rhs)) {
Box* index = PyNumber_Index(rhs);
if (!index) {
throwCAPIException();
}
rhs = index;
}
if (PyInt_Check(rhs)) if (PyInt_Check(rhs))
n = static_cast<BoxedInt*>(rhs)->n; n = static_cast<BoxedInt*>(rhs)->n;
else if (isSubclass(rhs->cls, long_cls)) { else if (PyLong_Check(rhs)) {
n = _PyLong_AsInt(rhs); n = _PyLong_AsInt(rhs);
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
PyErr_Clear(); PyErr_Clear();
raiseExcHelper(OverflowError, "cannot fit 'long' into index-sized integer"); raiseExcHelper(OverflowError, "cannot fit 'long' into index-sized integer");
} }
} else if (PyIndex_Check(rhs)) {
n = PyNumber_AsSsize_t(rhs, PyExc_OverflowError);
if (n == -1 && PyErr_Occurred())
throwCAPIException();
} else } else
return NotImplemented; return incref(NotImplemented);
if (n <= 0) if (n <= 0)
return EmptyString; return incref(EmptyString);
// TODO: use createUninitializedString and getWriteableStringContents // TODO: use createUninitializedString and getWriteableStringContents
int sz = lhs->size(); int sz = lhs->size();
......
...@@ -395,6 +395,8 @@ static void functionDtor(Box* b) { ...@@ -395,6 +395,8 @@ static void functionDtor(Box* b) {
self->dependent_ics.invalidateAll(); self->dependent_ics.invalidateAll();
self->dependent_ics.~ICInvalidator(); self->dependent_ics.~ICInvalidator();
self->clearAttrs();
Py_DECREF(self->doc); Py_DECREF(self->doc);
Py_DECREF(self->modname); Py_DECREF(self->modname);
Py_XDECREF(self->name); Py_XDECREF(self->name);
......
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