Commit f80d8bb6 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Fix up some attributes and __dict__ handling

- make sure that, except for a few special cases, any type which
  has instance attributes gets a __dict__ descriptor
- remove instance attributes from generator and builtin_function_or_method
  objects
parent b71d7063
......@@ -176,8 +176,6 @@ extern "C" BoxedGenerator::BoxedGenerator(BoxedFunctionBase* function, Box* arg1
: function(function), arg1(arg1), arg2(arg2), arg3(arg3), args(nullptr), entryExited(false), running(false),
returnValue(nullptr), exception(nullptr, nullptr, nullptr), context(nullptr), returnContext(nullptr) {
giveAttr("__name__", boxString(function->f->source->getName()));
int numArgs = function->f->num_args;
if (numArgs > 3) {
numArgs -= 3;
......@@ -280,6 +278,13 @@ extern "C" void generatorGCHandler(GCVisitor* v, Box* b) {
}
}
Box* generatorName(Box* _self, void* context) {
assert(isSubclass(_self->cls, generator_cls));
BoxedGenerator* self = static_cast<BoxedGenerator*>(_self);
return boxString(self->function->f->source->getName());
}
void generatorDestructor(Box* b) {
assert(isSubclass(b->cls, generator_cls));
BoxedGenerator* self = static_cast<BoxedGenerator*>(b);
......@@ -299,8 +304,8 @@ void generatorDestructor(Box* b) {
void setupGenerator() {
generator_cls
= BoxedHeapClass::create(type_cls, object_cls, &generatorGCHandler, offsetof(BoxedGenerator, attrs),
offsetof(BoxedGenerator, weakreflist), sizeof(BoxedGenerator), false, "generator");
= BoxedHeapClass::create(type_cls, object_cls, &generatorGCHandler, 0, offsetof(BoxedGenerator, weakreflist),
sizeof(BoxedGenerator), false, "generator");
generator_cls->simple_destructor = generatorDestructor;
generator_cls->giveAttr("__iter__",
new BoxedFunction(boxRTFunction((void*)generatorIter, typeFromClass(generator_cls), 1)));
......@@ -310,6 +315,8 @@ void setupGenerator() {
generator_cls->giveAttr("send", new BoxedFunction(boxRTFunction((void*)generatorSend, UNKNOWN, 2)));
generator_cls->giveAttr("throw", new BoxedFunction(boxRTFunction((void*)generatorThrow, UNKNOWN, 2)));
generator_cls->giveAttr("__name__", new (pyston_getset_cls) BoxedGetsetDescriptor(generatorName, NULL, NULL));
generator_cls->freeze();
}
}
......@@ -293,6 +293,11 @@ void BoxedClass::freeze() {
fixup_slot_dispatchers(this);
if (instancesHaveDictAttrs() || instancesHaveHCAttrs())
ASSERT(this == closure_cls || this == classobj_cls || this == instance_cls
|| typeLookup(this, "__dict__", NULL),
"%s", tp_name);
is_constant = true;
}
......
......@@ -289,8 +289,6 @@ extern "C" BoxedFunctionBase::BoxedFunctionBase(CLFunction* f, std::initializer_
this->modname = boxStringPtr(&builtinStr);
}
this->giveAttr("__doc__", None);
assert(f->num_defaults == ndefaults);
}
......@@ -307,6 +305,8 @@ BoxedFunction::BoxedFunction(CLFunction* f, std::initializer_list<Box*> defaults
if (f->source) {
this->name = static_cast<BoxedString*>(boxString(f->source->getName()));
}
this->giveAttr("__doc__", None);
}
BoxedBuiltinFunctionOrMethod::BoxedBuiltinFunctionOrMethod(CLFunction* f, const char* name)
......@@ -1758,9 +1758,8 @@ void setupRuntime() {
offsetof(BoxedFunction, in_weakreflist), sizeof(BoxedFunction), false, "function");
builtin_function_or_method_cls = BoxedHeapClass::create(
type_cls, object_cls, &functionGCHandler, offsetof(BoxedBuiltinFunctionOrMethod, attrs),
offsetof(BoxedBuiltinFunctionOrMethod, in_weakreflist), sizeof(BoxedBuiltinFunctionOrMethod), false,
"builtin_function_or_method");
type_cls, object_cls, &functionGCHandler, 0, offsetof(BoxedBuiltinFunctionOrMethod, in_weakreflist),
sizeof(BoxedBuiltinFunctionOrMethod), false, "builtin_function_or_method");
function_cls->simple_destructor = builtin_function_or_method_cls->simple_destructor = functionDtor;
instancemethod_cls = BoxedHeapClass::create(type_cls, object_cls, &instancemethodGCHandler, 0,
......@@ -1840,6 +1839,7 @@ void setupRuntime() {
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->giveAttr("__dict__", dict_descr);
module_cls->freeze();
closure_cls->freeze();
......
......@@ -430,7 +430,6 @@ class BoxedFunctionBase : public Box {
public:
Box** in_weakreflist;
HCAttrs attrs;
CLFunction* f;
BoxedClosure* closure;
......@@ -451,6 +450,8 @@ public:
class BoxedFunction : public BoxedFunctionBase {
public:
HCAttrs attrs;
BoxedFunction(CLFunction* f);
BoxedFunction(CLFunction* f, std::initializer_list<Box*> defaults, BoxedClosure* closure = NULL,
bool isGenerator = false);
......@@ -580,8 +581,6 @@ public:
class BoxedGenerator : public Box {
public:
HCAttrs attrs;
Box** weakreflist;
BoxedFunctionBase* function;
......
......@@ -3,9 +3,10 @@ def G1(i=0):
yield i
i += i
g1 = G1();
g1 = G1()
for i in range(5):
print g1.next()
print g1.__name__
......
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