Commit 38fdf541 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Allow subclassing from ModuleType

There are still a couple subtle differences in how we track and
store the various module attributes.  For example, we show something
different for 'print ModuleType("a", "b")'.

Also sneak in a fix for "import a.b.c as c"
parent a7c5cfcb
......@@ -1303,8 +1303,9 @@ public:
l = r + 1;
continue;
}
pushAssign(tmpname, new AST_Attribute(makeName(tmpname, AST_TYPE::Load, node->lineno),
AST_TYPE::Load, internString(a->name.str().substr(l, r))));
pushAssign(tmpname,
new AST_Attribute(makeName(tmpname, AST_TYPE::Load, node->lineno), AST_TYPE::Load,
internString(a->name.str().substr(l, r - l))));
l = r + 1;
} while (l < a->name.str().size());
pushAssign(a->asname, makeName(tmpname, AST_TYPE::Load, node->lineno));
......
......@@ -918,8 +918,19 @@ Box* typeMro(BoxedClass* self) {
return r;
}
Box* moduleNew(BoxedClass* cls, BoxedString* name, BoxedString* fn) {
RELEASE_ASSERT(isSubclass(cls, module_cls), "");
RELEASE_ASSERT(name->cls == str_cls, "");
RELEASE_ASSERT(!fn || fn->cls == str_cls, "");
if (fn)
return new (cls) BoxedModule(name->s, fn->s);
else
return new (cls) BoxedModule(name->s, "__builtin__");
}
Box* moduleRepr(BoxedModule* m) {
assert(m->cls == module_cls);
RELEASE_ASSERT(isSubclass(m->cls, module_cls), "");
std::ostringstream os;
os << "<module '" << m->name() << "' ";
......@@ -1395,6 +1406,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("__repr__", new BoxedFunction(boxRTFunction((void*)moduleRepr, STR, 1)));
module_cls->freeze();
......
......@@ -466,7 +466,9 @@ public:
class BoxedModule : public Box {
public:
HCAttrs attrs;
std::string fn; // for traceback purposes; not the same as __file__
// for traceback purposes; not the same as __file__. This corresponds to co_filename
std::string fn;
FutureFlags future_flags;
BoxedModule(const std::string& name, const std::string& fn);
......
# skip-if: True
# - wip
from distutils.core import setup
import distutils.command.build as build
from types import ModuleType
class MyModule(ModuleType):
pass
print MyModule('o')
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