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