Commit 567d507c authored by Travis Hance's avatar Travis Hance

made __module__ a member descriptor for functions

parent d7932903
......@@ -84,6 +84,8 @@ llvm::iterator_range<BoxIterator> Box::pyElements() {
raiseExcHelper(TypeError, "'%s' object is not iterable", getTypeName(this)->c_str());
}
std::string builtinStr("__builtin__");
extern "C" BoxedFunction::BoxedFunction(CLFunction* f)
: Box(function_cls), f(f), closure(NULL), isGenerator(false), ndefaults(0), defaults(NULL) {
if (f->source) {
......@@ -91,8 +93,9 @@ extern "C" BoxedFunction::BoxedFunction(CLFunction* f)
// this->giveAttr("__name__", boxString(&f->source->ast->name));
this->giveAttr("__name__", boxString(f->source->getName()));
Box* modname = f->source->parent_module->getattr("__name__", NULL);
this->giveAttr("__module__", modname);
this->modname = f->source->parent_module->getattr("__name__", NULL);
} else {
this->modname = boxStringPtr(&builtinStr);
}
this->giveAttr("__doc__", None);
......@@ -116,8 +119,9 @@ extern "C" BoxedFunction::BoxedFunction(CLFunction* f, std::initializer_list<Box
// this->giveAttr("__name__", boxString(&f->source->ast->name));
this->giveAttr("__name__", boxString(f->source->getName()));
Box* modname = f->source->parent_module->getattr("__name__", NULL);
this->giveAttr("__module__", modname);
this->modname = f->source->parent_module->getattr("__name__", NULL);
} else {
this->modname = boxStringPtr(&builtinStr);
}
this->giveAttr("__doc__", None);
......@@ -743,6 +747,8 @@ void setupRuntime() {
function_cls->giveAttr("__name__", boxStrConstant("function"));
function_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)functionRepr, STR, 1)));
function_cls->giveAttr("__str__", function_cls->getattr("__repr__"));
function_cls->giveAttr("__module__",
new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, offsetof(BoxedFunction, modname)));
function_cls->freeze();
instancemethod_cls->giveAttr("__name__", boxStrConstant("instancemethod"));
......
......@@ -298,6 +298,9 @@ public:
int ndefaults;
GCdArray* defaults;
// Accessed via member descriptor
Box* modname; // __module__
BoxedFunction(CLFunction* f);
BoxedFunction(CLFunction* f, std::initializer_list<Box*> defaults, BoxedClosure* closure = NULL,
bool isGenerator = false);
......
......@@ -3,3 +3,5 @@ def f():
print f.__name__
print f.__module__
print sum.__module__
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