Commit ead008eb authored by Kevin Modzelewski's avatar Kevin Modzelewski

get through some weakref

parent b363d7a0
......@@ -7,6 +7,10 @@ extern "C" {
#endif
// Pyston addition: refcounting annotations:
#define BORROWED(T) T
#define STOLEN(T) T
/* Object and type object interface */
/*
......
......@@ -417,13 +417,13 @@ extern "C" PyObject* Py_InitModule4(const char* name, PyMethodDef* methods, cons
}
}
BoxedModule* module = createModule(boxString(name), NULL, doc);
BoxedModule* module = createModule(autoDecref(boxString(name)), NULL, doc);
// Pass self as is, even if NULL we are not allowed to change it to None
Box* passthrough = static_cast<Box*>(self);
while (methods && methods->ml_name) {
module->giveAttr(methods->ml_name, new BoxedCApiFunction(methods, passthrough, boxString(name)));
module->giveAttr(methods->ml_name, new BoxedCApiFunction(methods, passthrough, autoDecref(boxString(name))));
methods++;
}
......
......@@ -834,7 +834,7 @@ class BoxedClass;
// TODO these shouldn't be here
void setupRuntime();
Box* createAndRunModule(BoxedString* name, const std::string& fn);
BoxedModule* createModule(BoxedString* name, const char* fn = NULL, const char* doc = NULL);
BoxedModule* createModule(BoxedString* name, const char* fn = NULL, const char* doc = NULL) noexcept;
Box* moduleInit(BoxedModule* self, Box* name, Box* doc = NULL);
// TODO where to put this
......
......@@ -646,7 +646,10 @@ void setupSys() {
gc::registerPermanentRoot(sys_modules_dict);
// This is ok to call here because we've already created the sys_modules_dict
sys_module = createModule(boxString("sys"));
sys_module = createModule(autoDecref(boxString("sys")));
// sys_module is what holds on to all of the other modules:
Py_INCREF(sys_module);
constants.push_back(sys_module);
sys_module->giveAttrBorrowed("modules", sys_modules_dict);
......
......@@ -785,6 +785,10 @@ static Box* dict_repr(PyObject* self) noexcept {
void BoxedDict::dealloc(Box* b) noexcept {
assert(PyDict_Check(b));
for (auto p : *static_cast<BoxedDict*>(b)) {
Py_DECREF(p.first);
Py_DECREF(p.second);
}
static_cast<BoxedDict*>(b)->d.freeAllMemory();
}
......@@ -807,7 +811,6 @@ void setupDict() {
dict_iterator_cls->instances_are_nonzero = dict_keys_cls->instances_are_nonzero
= dict_values_cls->instances_are_nonzero = dict_items_cls->instances_are_nonzero = true;
dict_cls->tp_dealloc = &BoxedDict::dealloc;
dict_cls->has_safe_tp_dealloc = true;
dict_cls->giveAttr("__len__", new BoxedFunction(FunctionMetadata::create((void*)dictLen, BOXED_INT, 1)));
......
......@@ -2238,14 +2238,14 @@ Box* moduleInit(BoxedModule* self, Box* name, Box* doc) {
if (attrs->hcls->attributeArraySize() == 0) {
attrs->hcls = HiddenClass::makeSingleton();
self->giveAttr("__name__", name);
self->giveAttr("__doc__", doc);
self->giveAttrBorrowed("__name__", name);
self->giveAttrBorrowed("__doc__", doc);
} else {
self->setattr(internStringMortal("__name__"), name, NULL);
self->setattr(internStringMortal("__doc__"), doc, NULL);
self->setattr(autoDecref(internStringMortal("__name__")), name, NULL);
self->setattr(autoDecref(internStringMortal("__doc__")), doc, NULL);
}
return None;
Py_RETURN_NONE;
}
Box* moduleRepr(BoxedModule* m) {
......@@ -3673,6 +3673,11 @@ done:
Py_TRASHCAN_SAFE_END(op)
}
void BoxedModule::dealloc(Box* b) noexcept {
BoxedModule* self = static_cast<BoxedModule*>(b);
self->clearAttrs();
}
#ifndef Py_REF_DEBUG
#define PRINT_TOTAL_REFS()
......@@ -3764,6 +3769,7 @@ void setupRuntime() {
BoxedClass(object_cls, &AttrWrapper::gcHandler, 0, 0, sizeof(AttrWrapper), false, "attrwrapper");
dict_cls = new (0) BoxedClass(object_cls, &BoxedDict::gcHandler, 0, 0, sizeof(BoxedDict), false, "dict");
dict_cls->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS;
dict_cls->tp_dealloc = BoxedDict::dealloc;
file_cls = new (0) BoxedClass(object_cls, &BoxedFile::gcHandler, 0, offsetof(BoxedFile, weakreflist),
sizeof(BoxedFile), false, "file");
file_cls->tp_dealloc = file_dealloc;
......@@ -3785,6 +3791,7 @@ void setupRuntime() {
module_cls = new (0) BoxedClass(object_cls, &BoxedModule::gcHandler, offsetof(BoxedModule, attrs), 0,
sizeof(BoxedModule), false, "module");
module_cls->tp_dealloc = BoxedModule::dealloc;
member_descriptor_cls = new (0)
BoxedClass(object_cls, NULL, 0, 0, sizeof(BoxedMemberDescriptor), false, "member_descriptor");
capifunc_cls = new (0)
......@@ -4252,7 +4259,7 @@ void setupRuntime() {
TRACK_ALLOCATIONS = true;
}
BoxedModule* createModule(BoxedString* name, const char* fn, const char* doc) {
BoxedModule* createModule(BoxedString* name, const char* fn, const char* doc) noexcept {
assert((!fn || strlen(fn)) && "probably wanted to set the fn to <stdin>?");
BoxedDict* d = getSysModulesDict();
......@@ -4265,13 +4272,16 @@ BoxedModule* createModule(BoxedString* name, const char* fn, const char* doc) {
}
BoxedModule* module = new BoxedModule();
moduleInit(module, name, boxString(doc ? doc : ""));
autoDecref(moduleInit(module, name, autoDecref(boxString(doc ? doc : ""))));
if (fn)
module->giveAttr("__file__", boxString(fn));
d->d[name] = module;
Py_XDECREF(existing);
d->d[incref(name)] = module;
if (name->s() == "__main__")
module->giveAttr("__builtins__", builtins_module);
module->giveAttrBorrowed("__builtins__", builtins_module);
return module;
}
......
......@@ -924,6 +924,7 @@ public:
Box* getLongConstant(llvm::StringRef s);
static void gcHandler(GCVisitor* v, Box* self);
static void dealloc(Box* b) noexcept;
private:
ContiguousMap<llvm::StringRef, BoxedString*, llvm::StringMap<int>> str_constants;
......
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