Commit ae7851d6 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Move module.__new__ to module.__init__

This was preventing subclasses of module from overriding __init__.
Yes, that really happens.
parent d05a5b30
......@@ -397,14 +397,6 @@ static void functionDtor(Box* b) {
self->dependent_ics.~ICInvalidator();
}
// TODO(kmod): builtin modules are not supposed to have a __file__ attribute
BoxedModule::BoxedModule(const std::string& name, const std::string& fn, const char* doc)
: attrs(HiddenClass::makeSingleton()) {
this->giveAttr("__name__", boxString(name));
this->giveAttr("__file__", boxString(fn));
this->giveAttr("__doc__", doc ? boxStrConstant(doc) : None);
}
std::string BoxedModule::name() {
Box* name = this->getattr("__name__");
if (!name || name->cls != str_cls) {
......@@ -1240,15 +1232,19 @@ Box* typeMro(BoxedClass* self) {
return r;
}
Box* moduleNew(BoxedClass* cls, BoxedString* name, BoxedString* fn) {
RELEASE_ASSERT(isSubclass(cls, module_cls), "");
Box* moduleInit(BoxedModule* self, Box* name, Box* doc) {
RELEASE_ASSERT(isSubclass(self->cls, module_cls), "");
RELEASE_ASSERT(name->cls == str_cls, "");
RELEASE_ASSERT(!fn || fn->cls == str_cls, "");
RELEASE_ASSERT(!doc || doc->cls == str_cls, "");
if (fn)
return new (cls) BoxedModule(name->s, fn->s);
else
return new (cls) BoxedModule(name->s, "__builtin__");
HCAttrs* attrs = self->getHCAttrsPtr();
RELEASE_ASSERT(attrs->hcls->attributeArraySize() == 0, "");
attrs->hcls = HiddenClass::makeSingleton();
self->giveAttr("__name__", name);
self->giveAttr("__doc__", doc ? doc : boxString(""));
return None;
}
Box* moduleRepr(BoxedModule* m) {
......@@ -2379,8 +2375,8 @@ void setupRuntime() {
none_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)noneNonzero, BOXED_BOOL, 1)));
none_cls->freeze();
module_cls->giveAttr("__new__",
new BoxedFunction(boxRTFunction((void*)moduleNew, UNKNOWN, 3, 1, false, false), { NULL }));
module_cls->giveAttr("__init__",
new BoxedFunction(boxRTFunction((void*)moduleInit, UNKNOWN, 3, 1, false, false), { NULL }));
module_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)moduleRepr, STR, 1)));
module_cls->giveAttr("__dict__", dict_descr);
module_cls->freeze();
......@@ -2600,7 +2596,10 @@ BoxedModule* createModule(const std::string& name, const std::string& fn, const
ptr = NULL;
}
BoxedModule* module = new BoxedModule(name, fn, doc);
BoxedModule* module = new BoxedModule();
moduleInit(module, boxString(name), boxString(doc ? doc : ""));
module->giveAttr("__file__", boxString(fn));
ptr = module;
return module;
}
......
......@@ -705,7 +705,7 @@ public:
FutureFlags future_flags;
BoxedModule(const std::string& name, const std::string& fn, const char* doc = NULL);
BoxedModule() {} // noop constructor to disable zero-initialization of cls
std::string name();
Box* getStringConstant(const std::string& ast_str);
......
from types import ModuleType
class MyModule(ModuleType):
pass
# Make sure that we can override __init__:
def __init__(self, fn, doc, myarg):
super(MyModule, self).__init__(fn, doc)
self.myarg = myarg
print MyModule('o')
print MyModule('o', "doc", 1)
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