Commit e157d33f authored by Leandro Lameiro's avatar Leandro Lameiro

Uses module name when generating IR, for improved readability.

As I touched the module repr, I added some tests to it. Turned out that cpython normalizes the path of a module and uses the pyc file in the repr string. That part is ignored in this test.
parent 666eadb0
......@@ -52,7 +52,7 @@ const std::string SourceInfo::getName() {
case AST_TYPE::FunctionDef:
return ast_cast<AST_FunctionDef>(ast)->name;
case AST_TYPE::Module:
return "module";
return this->parent_module->name();
default:
RELEASE_ASSERT(0, "%d", ast->type);
}
......
......@@ -52,6 +52,16 @@ BoxedModule::BoxedModule(const std::string &name, const std::string &fn) : HCBox
this->giveAttr("__file__", boxString(fn));
}
std::string BoxedModule::name(){
Box* name = this->peekattr("__name__");
if (!name || name->cls != str_cls) {
return "?";
} else {
BoxedString *sname = static_cast<BoxedString*>(name);
return sname->s;
}
}
extern "C" Box* boxCLFunction(CLFunction *f) {
return new BoxedFunction(f);
}
......@@ -317,20 +327,12 @@ Box* moduleRepr(BoxedModule* m) {
assert(m->cls == module_cls);
std::ostringstream os;
os << "<module '";
Box* name = m->peekattr("__name__");
if (!name || name->cls != str_cls) {
os << '?';
} else {
BoxedString *sname = static_cast<BoxedString*>(name);
os << sname->s;
}
os << "<module '" << m->name() << "' ";
if (m->fn == "__builtin__") {
os << "' (built-in)>";
os << "(built-in)>";
} else {
os << "' from '" << m->fn << "'>";
os << "from '" << m->fn << "'>";
}
return boxString(os.str());
}
......
......@@ -256,6 +256,7 @@ class BoxedModule : public HCBox {
const std::string fn; // for traceback purposes; not the same as __file__
BoxedModule(const std::string &name, const std::string &fn);
std::string name();
};
class BoxedSlice : public HCBox {
......
import empty_module
import math
x = repr(empty_module)
# cpython will simplify the path (remove . and ..), and use the pyc file in the repr string. Pyston still doesn't support that, so we are only checking the non-path part.
print x[0:29]
print x[-2:]
print repr(math) # no path for built-ins, so we can just check as normal
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