Commit f1238071 authored by Marius Wachtler's avatar Marius Wachtler

Add PyClass_New, PyMethod_New, capifunc.__module__, missing gc handler, fix...

Add PyClass_New, PyMethod_New, capifunc.__module__, missing gc handler, fix instancemethodGet if obj == NULL
parent 3e3a9a74
......@@ -412,8 +412,8 @@ extern "C" PyObject* Py_InitModule4(const char* name, PyMethodDef* methods, cons
while (methods && methods->ml_name) {
RELEASE_ASSERT((methods->ml_flags & (~(METH_VARARGS | METH_KEYWORDS | METH_NOARGS | METH_O))) == 0, "%d",
methods->ml_flags);
module->giveAttr(methods->ml_name,
new BoxedCApiFunction(methods->ml_flags, passthrough, methods->ml_name, methods->ml_meth));
module->giveAttr(methods->ml_name, new BoxedCApiFunction(methods->ml_flags, passthrough, methods->ml_name,
methods->ml_meth, boxString(name)));
methods++;
}
......
......@@ -44,8 +44,11 @@ private:
PyCFunction func;
public:
BoxedCApiFunction(int ml_flags, Box* passthrough, const char* name, PyCFunction func)
: ml_flags(ml_flags), passthrough(passthrough), name(name), func(func) {}
Box* module;
public:
BoxedCApiFunction(int ml_flags, Box* passthrough, const char* name, PyCFunction func, Box* module = NULL)
: ml_flags(ml_flags), passthrough(passthrough), name(name), func(func), module(module) {}
DEFAULT_CLASS(capifunc_cls);
......@@ -112,6 +115,15 @@ public:
static Box* callInternal(BoxedFunctionBase* func, CallRewriteArgs* rewrite_args, ArgPassSpec argspec, Box* arg1,
Box* arg2, Box* arg3, Box** args, const std::vector<const std::string*>* keyword_names);
static void gcHandler(GCVisitor* v, Box* _o) {
assert(_o->cls == capifunc_cls);
BoxedCApiFunction* o = static_cast<BoxedCApiFunction*>(_o);
boxGCHandler(v, o);
v->visit(o->passthrough);
v->visit(o->module);
}
};
class BoxedWrapperDescriptor : public Box {
......@@ -126,6 +138,14 @@ public:
static Box* __get__(BoxedWrapperDescriptor* self, Box* inst, Box* owner);
static Box* __call__(BoxedWrapperDescriptor* descr, PyObject* self, BoxedTuple* args, Box** _args);
static void gcHandler(GCVisitor* v, Box* _o) {
assert(_o->cls == wrapperdescr_cls);
BoxedWrapperDescriptor* o = static_cast<BoxedWrapperDescriptor*>(_o);
boxGCHandler(v, o);
v->visit(o->type);
}
};
class BoxedWrapperObject : public Box {
......@@ -162,6 +182,14 @@ public:
assert(rtn && "should have set + thrown an exception!");
return rtn;
}
static void gcHandler(GCVisitor* v, Box* _o) {
assert(_o->cls == wrapperobject_cls);
BoxedWrapperObject* o = static_cast<BoxedWrapperObject*>(_o);
boxGCHandler(v, o);
v->visit(o->obj);
}
};
class BoxedMethodDescriptor : public Box {
......@@ -193,6 +221,14 @@ public:
}
static Box* __call__(BoxedMethodDescriptor* self, Box* obj, BoxedTuple* varargs, Box** _args);
static void gcHandler(GCVisitor* v, Box* _o) {
assert(_o->cls == method_cls);
BoxedMethodDescriptor* o = static_cast<BoxedMethodDescriptor*>(_o);
boxGCHandler(v, o);
v->visit(o->type);
}
};
} // namespace pyston
......
......@@ -1196,10 +1196,9 @@ extern "C" PyObject* Py_FindMethod(PyMethodDef* methods, PyObject* self, const c
}
extern "C" PyObject* PyCFunction_NewEx(PyMethodDef* ml, PyObject* self, PyObject* module) noexcept {
RELEASE_ASSERT(module == NULL, "not implemented");
assert((ml->ml_flags & (~(METH_VARARGS | METH_KEYWORDS | METH_NOARGS | METH_O))) == 0);
return new BoxedCApiFunction(ml->ml_flags, self, ml->ml_name, ml->ml_meth);
return new BoxedCApiFunction(ml->ml_flags, self, ml->ml_name, ml->ml_meth, module);
}
extern "C" PyCFunction PyCFunction_GetFunction(PyObject* op) noexcept {
......@@ -1414,6 +1413,8 @@ void setupCAPI() {
capifunc_cls->giveAttr("__call__", capi_call);
capifunc_cls->giveAttr("__name__",
new (pyston_getset_cls) BoxedGetsetDescriptor(BoxedCApiFunction::getname, NULL, NULL));
capifunc_cls->giveAttr(
"__module__", new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, offsetof(BoxedCApiFunction, module)));
capifunc_cls->freeze();
......
......@@ -609,6 +609,33 @@ Box* instanceCall(Box* _inst, Box* _args, Box* _kwargs) {
return runtimeCall(call_func, ArgPassSpec(0, 0, true, true), _args, _kwargs, NULL, NULL, NULL);
}
extern "C" PyObject* PyClass_New(PyObject* bases, PyObject* dict, PyObject* name) noexcept {
try {
if (name == NULL || !PyString_Check(name)) {
PyErr_SetString(PyExc_TypeError, "PyClass_New: name must be a string");
return NULL;
}
if (dict == NULL || !PyDict_Check(dict)) {
PyErr_SetString(PyExc_TypeError, "PyClass_New: dict must be a dictionary");
return NULL;
}
return runtimeCall(classobj_cls, ArgPassSpec(3), name, bases, dict, NULL, NULL);
} catch (ExcInfo e) {
setCAPIException(e);
return NULL;
}
}
extern "C" PyObject* PyMethod_New(PyObject* func, PyObject* self, PyObject* klass) noexcept {
try {
return new BoxedInstanceMethod(self, func, klass);
} catch (ExcInfo e) {
setCAPIException(e);
return NULL;
}
}
void setupClassobj() {
classobj_cls = BoxedHeapClass::create(type_cls, object_cls, &BoxedClassobj::gcHandler,
offsetof(BoxedClassobj, attrs), 0, sizeof(BoxedClassobj), false, "classobj");
......
......@@ -936,6 +936,9 @@ Box* instancemethodGet(BoxedInstanceMethod* self, Box* obj, Box* type) {
return self;
}
if (obj == None)
obj = NULL;
return new BoxedInstanceMethod(obj, self->func, self->im_class);
}
......@@ -2220,14 +2223,17 @@ void setupRuntime() {
static_cast<BoxedString*>(boxStrConstant("module")));
member_descriptor_cls = new (0) BoxedHeapClass(object_cls, NULL, 0, 0, sizeof(BoxedMemberDescriptor), false,
static_cast<BoxedString*>(boxStrConstant("member_descriptor")));
capifunc_cls = new (0) BoxedHeapClass(object_cls, NULL, 0, 0, sizeof(BoxedCApiFunction), false,
static_cast<BoxedString*>(boxStrConstant("capifunc")));
method_cls = new (0) BoxedHeapClass(object_cls, NULL, 0, 0, sizeof(BoxedMethodDescriptor), false,
static_cast<BoxedString*>(boxStrConstant("method")));
wrapperobject_cls = new (0) BoxedHeapClass(object_cls, NULL, 0, 0, sizeof(BoxedWrapperObject), false,
static_cast<BoxedString*>(boxStrConstant("method-wrapper")));
wrapperdescr_cls = new (0) BoxedHeapClass(object_cls, NULL, 0, 0, sizeof(BoxedWrapperDescriptor), false,
static_cast<BoxedString*>(boxStrConstant("wrapper_descriptor")));
capifunc_cls = new (0) BoxedHeapClass(object_cls, BoxedCApiFunction::gcHandler, 0, 0, sizeof(BoxedCApiFunction),
false, static_cast<BoxedString*>(boxStrConstant("capifunc")));
method_cls = new (0)
BoxedHeapClass(object_cls, BoxedMethodDescriptor::gcHandler, 0, 0, sizeof(BoxedMethodDescriptor), false,
static_cast<BoxedString*>(boxStrConstant("method")));
wrapperobject_cls = new (0)
BoxedHeapClass(object_cls, BoxedWrapperObject::gcHandler, 0, 0, sizeof(BoxedWrapperObject), false,
static_cast<BoxedString*>(boxStrConstant("method-wrapper")));
wrapperdescr_cls = new (0)
BoxedHeapClass(object_cls, BoxedWrapperDescriptor::gcHandler, 0, 0, sizeof(BoxedWrapperDescriptor), false,
static_cast<BoxedString*>(boxStrConstant("wrapper_descriptor")));
EmptyString = boxStrConstant("");
gc::registerPermanentRoot(EmptyString);
......
......@@ -8,3 +8,5 @@ time.clock()
print time.timezone
print long(time.mktime(time.localtime(1020)))
print time.sleep.__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