Commit 0b1acea8 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Working on getting old-style classes working

parent 3874fd6c
......@@ -431,11 +431,11 @@ extern "C" PyObject* Py_InitModule4(const char* name, PyMethodDef* methods, cons
return module;
}
extern "C" PyObject* PyModule_GetDict(PyObject* _m) noexcept {
extern "C" PyObject* PyModule_GetDict(BORROWED(PyObject*) _m) noexcept {
BoxedModule* m = static_cast<BoxedModule*>(_m);
assert(m->cls == module_cls);
return m->getAttrWrapper();
return autoDecref(m->getAttrWrapper());
}
extern "C" int PyModule_AddObject(PyObject* _m, const char* name, PyObject* value) noexcept {
......
......@@ -3427,7 +3427,7 @@ extern "C" int PyType_Ready(PyTypeObject* cls) noexcept {
cls->giveAttrBorrowed("__base__", base);
assert(cls->tp_dict == NULL);
cls->tp_dict = incref(cls->getAttrWrapper());
cls->tp_dict = cls->getAttrWrapper();
assert(cls->tp_name);
......
......@@ -184,7 +184,7 @@ public:
Box* getGlobals() {
assert(globals);
return globals;
return incref(globals);
}
FunctionMetadata* getMD() { return md; }
......
......@@ -331,7 +331,7 @@ void compileAndRunModule(AST_Module* m, BoxedModule* bm) {
static BoxedString* builtins_str = getStaticString("__builtins__");
if (!bm->hasattr(builtins_str))
bm->giveAttr(builtins_str, PyModule_GetDict(builtins_module));
bm->giveAttr(incref(builtins_str), PyModule_GetDict(builtins_module));
md = new FunctionMetadata(0, false, false, std::move(si));
}
......@@ -565,16 +565,20 @@ static void pickGlobalsAndLocals(Box*& globals, Box*& locals) {
if (globals) {
// From CPython (they set it to be f->f_builtins):
Box* globals_dict = globals;
Box* globals_dict;
if (globals->cls == module_cls)
globals_dict = globals->getAttrWrapper();
else
globals_dict = incref(globals);
AUTO_DECREF(globals_dict);
auto requested_builtins = PyDict_GetItemString(globals_dict, "__builtins__");
if (requested_builtins == NULL)
PyDict_SetItemString(globals_dict, "__builtins__", builtins_module);
else
RELEASE_ASSERT(requested_builtins == builtins_module
|| requested_builtins == builtins_module->getAttrWrapper(),
|| requested_builtins == autoDecref(builtins_module->getAttrWrapper()),
"we don't support overriding __builtins__");
}
}
......
......@@ -369,7 +369,7 @@ public:
auto locations = findLocations(PASSED_GLOBALS_NAME);
assert(locations.size() == 1);
Box* r = (Box*)readLocation(locations[0]);
return r;
return incref(r);
} else if (id.type == PythonFrameId::INTERPRETED) {
return getGlobalsForInterpretedFrame((void*)id.bp);
}
......@@ -381,8 +381,10 @@ public:
if (!globals)
return NULL;
if (PyModule_Check(globals))
if (PyModule_Check(globals)) {
AUTO_DECREF(globals);
return globals->getAttrWrapper();
}
return globals;
}
......@@ -785,7 +787,6 @@ Box* getGlobals() {
auto it = getTopPythonFrame();
if (!it)
return NULL;
RELEASE_ASSERT(0, "check refcounting");
return it->getGlobals();
}
......
......@@ -670,7 +670,7 @@ public:
Py_INCREF(val);
giveAttr(internStringMortal(attr), val);
}
void giveAttr(BoxedString* attr, Box* val);
void giveAttr(STOLEN(BoxedString*) attr, STOLEN(Box*) val);
void clearAttrs();
......@@ -685,7 +685,8 @@ public:
void delattr(BoxedString* attr, DelattrRewriteArgs* rewrite_args);
// Only valid for hc-backed instances:
BORROWED(Box*) getAttrWrapper();
// Maybe this should return a borrowed reference?
Box* getAttrWrapper();
Box* reprIC();
BoxedString* reprICAsString();
......
......@@ -1067,7 +1067,7 @@ public:
static Box* __reduce__(Box* self) {
RELEASE_ASSERT(isSubclass(self->cls, BaseException), "");
BoxedException* exc = static_cast<BoxedException*>(self);
return BoxedTuple::create({ self->cls, EmptyTuple, self->getAttrWrapper() });
return BoxedTuple::create({ self->cls, EmptyTuple, autoDecref(self->getAttrWrapper()) });
}
};
......
This diff is collapsed.
......@@ -48,17 +48,14 @@ public:
Box** weakreflist;
BoxedClassobj(BoxedString* name, BoxedTuple* bases) : bases(bases), name(name) {}
static void dealloc(Box* b) noexcept {
Py_FatalError("unimplemented");
}
static int traverse(Box* self, visitproc visit, void *arg) noexcept {
Py_FatalError("unimplemented");
}
static int clear(Box* self) noexcept {
Py_FatalError("unimplemented");
BoxedClassobj(BoxedString* name, BoxedTuple* bases) : bases(bases), name(name) {
Py_INCREF(name);
Py_INCREF(bases);
}
static void dealloc(Box* b) noexcept;
static int traverse(Box* self, visitproc visit, void *arg) noexcept;
static int clear(Box* self) noexcept;
};
class BoxedInstance : public Box {
......@@ -69,19 +66,15 @@ public:
Box** weakreflist;
BoxedInstance(BoxedClassobj* inst_cls) : inst_cls(inst_cls) {}
BoxedInstance(BoxedClassobj* inst_cls) : inst_cls(inst_cls) {
Py_INCREF(inst_cls);
}
DEFAULT_CLASS(instance_cls);
static void dealloc(Box* b) noexcept {
Py_FatalError("unimplemented");
}
static int traverse(Box* self, visitproc visit, void *arg) noexcept {
Py_FatalError("unimplemented");
}
static int clear(Box* self) noexcept {
Py_FatalError("unimplemented");
}
static void dealloc(Box* b) noexcept;
static int traverse(Box* self, visitproc visit, void *arg) noexcept;
static int clear(Box* self) noexcept;
};
Box* instance_getattro(Box* cls, Box* attr) noexcept;
......@@ -89,7 +82,7 @@ int instance_setattro(Box* cls, Box* attr, Box* value) noexcept;
class GetattrRewriteArgs;
template <ExceptionStyle S>
Box* instanceGetattroInternal(Box* self, Box* attr, GetattrRewriteArgs* rewrite_args) noexcept(S == CAPI);
Box* instanceSetattroInternal(Box* self, Box* attr, Box* val, SetattrRewriteArgs* rewrite_args);
void instanceSetattroInternal(Box* self, STOLEN(Box*) attr, Box* val, SetattrRewriteArgs* rewrite_args);
}
#endif
......@@ -779,7 +779,7 @@ BoxedClass* BoxedClass::create(BoxedClass* metaclass, BoxedClass* base, int attr
void BoxedClass::finishInitialization() {
assert(!this->tp_dict);
this->tp_dict = incref(this->getAttrWrapper());
this->tp_dict = this->getAttrWrapper();
commonClassSetup(this);
tp_flags |= Py_TPFLAGS_READY;
......@@ -992,7 +992,7 @@ BoxedDict* Box::getDict() {
}
assert(d->cls == dict_cls);
return d;
return incref(d);
}
static StatCounter box_getattr_slowpath("slowpath_box_getattr");
......@@ -2838,7 +2838,6 @@ extern "C" void setattr(Box* obj, BoxedString* attr, STOLEN(Box*) attr_val) {
RewriterVar* r_setattr;
if (tp_setattro == instance_setattro) {
assert(0 && "check refcounting");
if (rewriter.get()) {
SetattrRewriteArgs rewrite_args(rewriter.get(), rewriter->getArg(0), rewriter->getArg(2));
instanceSetattroInternal(obj, attr, attr_val, &rewrite_args);
......
......@@ -1305,7 +1305,7 @@ Box* typeCall(Box* obj, BoxedTuple* vararg, BoxedDict* kwargs) {
static Box* typeDict(Box* obj, void* context) {
if (obj->cls->instancesHaveHCAttrs())
return PyDictProxy_New(obj->getAttrWrapper());
return PyDictProxy_New(autoDecref(obj->getAttrWrapper()));
abort();
}
......@@ -1392,6 +1392,7 @@ extern "C" Box* createUserClass(BoxedString* name, Box* _bases, Box* _attr_dict)
metaclass = bases->elts[0]->cls;
} else {
Box* gl = getGlobalsDict();
AUTO_DECREF(gl);
metaclass = PyDict_GetItemString(gl, "__metaclass__");
if (!metaclass) {
......@@ -1411,8 +1412,10 @@ extern "C" Box* createUserClass(BoxedString* name, Box* _bases, Box* _attr_dict)
// But for classes created from Python, we don't have this extra untracked reference.
// Rather than fix up the plumbing for now, just reach into the other system and remove this
// class from the list.
if (isSubclass(r->cls, type_cls)) {
RELEASE_ASSERT(classes.back() == r, "");
classes.pop_back();
}
return r;
} catch (ExcInfo e) {
......@@ -2559,7 +2562,7 @@ Box* Box::getAttrWrapper() {
HiddenClass* hcls = attrs->hcls;
if (hcls->type == HiddenClass::DICT_BACKED) {
return attrs->attr_list->attrs[0];
return incref(attrs->attr_list->attrs[0]);
}
int offset = hcls->getAttrwrapperOffset();
......@@ -2569,17 +2572,15 @@ Box* Box::getAttrWrapper() {
auto new_hcls = hcls->getAttrwrapperChild();
appendNewHCAttr(aw, NULL);
attrs->hcls = new_hcls;
Py_DECREF(aw);
return aw;
} else {
assert(hcls->type == HiddenClass::SINGLETON);
appendNewHCAttr(aw, NULL);
hcls->appendAttrwrapper();
Py_DECREF(aw);
return aw;
}
}
return attrs->attr_list->attrs[offset];
return incref(attrs->attr_list->attrs[offset]);
}
extern "C" PyObject* PyObject_GetAttrWrapper(PyObject* obj) 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