Commit 358ab2ab authored by Kevin Modzelewski's avatar Kevin Modzelewski

Move a bunch of functionality from __new__ to __init__

It's more efficient since __new__ ends up being harder for the
JIT to handle than __init__.

It's also more correct, though hopefully no one relies on this.
parent 8d9e1bb8
......@@ -21,15 +21,28 @@ namespace pyston {
static Box* memberGet(BoxedMemberDescriptor* self, Box* inst, Box* owner) {
RELEASE_ASSERT(self->cls == member_cls, "");
if (inst == None)
return self;
if (self->type == BoxedMemberDescriptor::OBJECT) {
return *(Box**)(((char*)inst) + self->offset);
}
Py_FatalError("unimplemented");
}
static Box* propertyNew(Box* cls, Box* fget, Box* fset, Box** args) {
RELEASE_ASSERT(cls == property_cls, "");
static Box* propertyInit(Box* _self, Box* fget, Box* fset, Box** args) {
RELEASE_ASSERT(_self->cls == property_cls, "");
Box* fdel = args[0];
Box* doc = args[1];
return new BoxedProperty(fget, fset, fdel, doc);
BoxedProperty* self = static_cast<BoxedProperty*>(_self);
self->prop_get = fget;
self->prop_set = fset;
self->prop_del = fdel;
self->prop_doc = doc;
return None;
}
static Box* propertyGet(Box* self, Box* obj, Box* type) {
......@@ -70,9 +83,12 @@ static Box* propertySet(Box* self, Box* obj, Box* val) {
return None;
}
static Box* staticmethodNew(Box* cls, Box* f) {
RELEASE_ASSERT(cls == staticmethod_cls, "");
return new BoxedStaticmethod(f);
static Box* staticmethodInit(Box* _self, Box* f) {
RELEASE_ASSERT(_self->cls == staticmethod_cls, "");
BoxedStaticmethod* self = static_cast<BoxedStaticmethod*>(_self);
self->sm_callable = f;
return None;
}
static Box* staticmethodGet(Box* self, Box* obj, Box* type) {
......@@ -87,9 +103,12 @@ static Box* staticmethodGet(Box* self, Box* obj, Box* type) {
return sm->sm_callable;
}
static Box* classmethodNew(Box* cls, Box* f) {
RELEASE_ASSERT(cls == classmethod_cls, "");
return new BoxedClassmethod(f);
static Box* classmethodInit(Box* _self, Box* f) {
RELEASE_ASSERT(_self->cls == classmethod_cls, "");
BoxedClassmethod* self = static_cast<BoxedClassmethod*>(_self);
self->cm_callable = f;
return None;
}
static Box* classmethodGet(Box* self, Box* obj, Box* type) {
......@@ -114,29 +133,38 @@ void setupDescr() {
member_cls->freeze();
property_cls->giveAttr("__name__", boxStrConstant("property"));
property_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)propertyNew, UNKNOWN, 5, 4, false, false),
{ None, None, None, None }));
property_cls->giveAttr(
"__init__",
new BoxedFunction(boxRTFunction((void*)propertyInit, UNKNOWN, 5, 4, false, false), { None, None, None, None }));
property_cls->giveAttr("__get__",
new BoxedFunction(boxRTFunction((void*)propertyGet, UNKNOWN, 3, 0, false, false)));
property_cls->giveAttr("__set__",
new BoxedFunction(boxRTFunction((void*)propertySet, UNKNOWN, 3, 0, false, false)));
property_cls->giveAttr("fget",
new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, offsetof(BoxedProperty, prop_get)));
property_cls->giveAttr("fset",
new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, offsetof(BoxedProperty, prop_set)));
property_cls->giveAttr("fdel",
new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, offsetof(BoxedProperty, prop_del)));
property_cls->giveAttr("fdoc",
new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, offsetof(BoxedProperty, prop_doc)));
property_cls->freeze();
staticmethod_cls->giveAttr("__name__", boxStrConstant("staticmethod"));
staticmethod_cls->giveAttr("__new__",
new BoxedFunction(boxRTFunction((void*)staticmethodNew, UNKNOWN, 5, 4, false, false),
staticmethod_cls->giveAttr("__init__",
new BoxedFunction(boxRTFunction((void*)staticmethodInit, UNKNOWN, 5, 4, false, false),
{ None, None, None, None }));
staticmethod_cls->giveAttr("__get__",
new BoxedFunction(boxRTFunction((void*)staticmethodGet, UNKNOWN, 3, 0, false, false)));
staticmethod_cls->giveAttr(
"__get__", new BoxedFunction(boxRTFunction((void*)staticmethodGet, UNKNOWN, 3, 1, false, false), { None }));
staticmethod_cls->freeze();
classmethod_cls->giveAttr("__name__", boxStrConstant("classmethod"));
classmethod_cls->giveAttr("__new__",
new BoxedFunction(boxRTFunction((void*)classmethodNew, UNKNOWN, 5, 4, false, false),
classmethod_cls->giveAttr("__init__",
new BoxedFunction(boxRTFunction((void*)classmethodInit, UNKNOWN, 5, 4, false, false),
{ None, None, None, None }));
classmethod_cls->giveAttr("__get__",
new BoxedFunction(boxRTFunction((void*)classmethodGet, UNKNOWN, 3, 0, false, false)));
classmethod_cls->giveAttr(
"__get__", new BoxedFunction(boxRTFunction((void*)classmethodGet, UNKNOWN, 3, 1, false, false), { None }));
classmethod_cls->freeze();
}
......
......@@ -545,7 +545,7 @@ std::string floatFmt(double x, int precision, char code) {
}
Box* floatNew(BoxedClass* cls, Box* a) {
assert(cls == float_cls);
RELEASE_ASSERT(cls == float_cls, "");
if (a->cls == float_cls) {
return a;
......
......@@ -73,7 +73,9 @@ Box* superGetattribute(Box* _s, Box* _attr) {
}
}
RELEASE_ASSERT(0, "should call the equivalent of objectGetattr here");
Box* r = typeLookup(s->cls, attr->s, NULL);
RELEASE_ASSERT(r, "should call the equivalent of objectGetattr here");
return processDescriptor(r, s, s->cls);
}
Box* superRepr(Box* _s) {
......@@ -107,9 +109,9 @@ BoxedClass* supercheck(BoxedClass* type, Box* obj) {
raiseExcHelper(TypeError, "super(type, obj): obj must be an instance or subtype of type");
}
// TODO This functionality is supposed to be in the __init__ function:
Box* superNew(Box* _cls, Box* _type, Box* obj) {
RELEASE_ASSERT(_cls == super_cls, "");
Box* superInit(Box* _self, Box* _type, Box* obj) {
RELEASE_ASSERT(_self->cls == super_cls, "");
BoxedSuper* self = static_cast<BoxedSuper*>(_self);
if (!isSubclass(_type->cls, type_cls))
raiseExcHelper(TypeError, "must be type, not %s", getTypeName(_type)->c_str());
......@@ -121,7 +123,11 @@ Box* superNew(Box* _cls, Box* _type, Box* obj) {
if (obj != NULL)
obj_type = supercheck(type, obj);
return new BoxedSuper(type, obj, obj_type);
self->type = type;
self->obj = obj;
self->obj_type = obj_type;
return None;
}
void setupSuper() {
......@@ -132,8 +138,15 @@ void setupSuper() {
super_cls->giveAttr("__getattribute__", new BoxedFunction(boxRTFunction((void*)superGetattribute, UNKNOWN, 2)));
super_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)superRepr, STR, 1)));
super_cls->giveAttr("__new__",
new BoxedFunction(boxRTFunction((void*)superNew, UNKNOWN, 3, 1, false, false), { NULL }));
super_cls->giveAttr("__init__",
new BoxedFunction(boxRTFunction((void*)superInit, UNKNOWN, 3, 1, false, false), { NULL }));
super_cls->giveAttr("__thisclass__",
new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, offsetof(BoxedSuper, type)));
super_cls->giveAttr("__self__",
new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, offsetof(BoxedSuper, obj)));
super_cls->giveAttr("__self_class__",
new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, offsetof(BoxedSuper, obj_type)));
super_cls->freeze();
}
......
......@@ -949,6 +949,10 @@ void setupRuntime() {
instancemethod_cls->giveAttr("__eq__", new BoxedFunction(boxRTFunction((void*)instancemethodEq, UNKNOWN, 2)));
instancemethod_cls->giveAttr(
"__get__", new BoxedFunction(boxRTFunction((void*)instancemethodGet, UNKNOWN, 3, 0, false, false)));
instancemethod_cls->giveAttr(
"im_func", new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, offsetof(BoxedInstanceMethod, func)));
instancemethod_cls->giveAttr(
"im_self", new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, offsetof(BoxedInstanceMethod, obj)));
instancemethod_cls->freeze();
slice_cls->giveAttr("__name__", boxStrConstant("slice"));
......
# Not sure if these are important or not:
p = property(1, 2, 3)
print p.fset
property.__init__(p, 4, 5, 6)
print p.fset
s = staticmethod(7)
print s.__get__(8)
staticmethod.__init__(s, 9)
print s.__get__(10)
c = classmethod(11)
print c.__get__(12).im_func
classmethod.__init__(c, 13)
print c.__get__(13).im_func
sl = slice(14, 15, 16)
print sl.start
slice.__init__(sl, 17, 18, 19)
print sl.start
# l = list((20,))
# print l
# list.__init__(l, (21,))
# print l
sp = super(int, 1)
print sp.__thisclass__
super.__init__(sp, float, 1.0)
print sp.__thisclass__
......@@ -16,3 +16,13 @@ class C(B):
c = C(1, 2)
print c.arg1
print c.arg2
try:
super(1)
except Exception, e:
print e
try:
super(int, [])
except Exception, e:
print e
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