Commit 41d97c30 authored by Marius Wachtler's avatar Marius Wachtler

Add more __repr__ attributes to our builtins

parent df3e7ee9
......@@ -398,6 +398,15 @@ static Box* methodGetDoc(Box* b, void*) {
return None;
}
static Box* methodRepr(Box* _o) {
assert(_o->cls == method_cls);
BoxedMethodDescriptor* md = static_cast<BoxedMethodDescriptor*>(_o);
const char* name = md->method->ml_name;
if (!name)
name = "?";
return PyString_FromFormat("<method '%s' of '%s' objects>", name, getNameOfClass(md->type));
}
Box* BoxedMethodDescriptor::__get__(BoxedMethodDescriptor* self, Box* inst, Box* owner) {
RELEASE_ASSERT(self->cls == method_cls, "");
......@@ -471,6 +480,22 @@ static Box* wrapperdescrGetDoc(Box* b, void*) {
return boxString(s);
}
static Box* wrapperDescrRepr(Box* _o) {
assert(_o->cls == wrapperdescr_cls);
BoxedWrapperDescriptor* wd = static_cast<BoxedWrapperDescriptor*>(_o);
const char* name = "?";
if (wd->wrapper != NULL)
name = wd->wrapper->name.data();
return PyString_FromFormat("<slot wrapper '%s' of '%s' objects>", name, getNameOfClass(wd->type));
}
static Box* wrapperObjectRepr(Box* _o) {
assert(_o->cls == wrapperobject_cls);
BoxedWrapperObject* wp = static_cast<BoxedWrapperObject*>(_o);
return PyString_FromFormat("<method-wrapper '%s' of %s object at %p>", wp->descr->wrapper->name.str().c_str(),
getTypeName(wp->obj), wp->obj);
}
Box* BoxedWrapperObject::__call__(BoxedWrapperObject* self, Box* args, Box* kwds) {
STAT_TIMER(t0, "us_timer_boxedwrapperobject_call", (self->cls->is_user_defined ? 10 : 20));
......@@ -635,6 +660,7 @@ void setupDescr() {
method_cls->tpp_call.capi_val = BoxedMethodDescriptor::tppCall<CAPI>;
method_cls->tpp_call.cxx_val = BoxedMethodDescriptor::tppCall<CXX>;
method_cls->giveAttr("__doc__", new (pyston_getset_cls) BoxedGetsetDescriptor(methodGetDoc, NULL, NULL));
method_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)methodRepr, UNKNOWN, 1)));
method_cls->freeze();
wrapperdescr_cls->giveAttr("__call__", new BoxedFunction(boxRTFunction((void*)BoxedWrapperDescriptor::__call__,
......@@ -643,6 +669,7 @@ void setupDescr() {
new (pyston_getset_cls) BoxedGetsetDescriptor(wrapperdescrGetDoc, NULL, NULL));
wrapperdescr_cls->tp_descr_get = BoxedWrapperDescriptor::descr_get;
add_operators(wrapperdescr_cls);
wrapperdescr_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)wrapperDescrRepr, UNKNOWN, 1)));
wrapperdescr_cls->freeze();
assert(wrapperdescr_cls->tp_descr_get == BoxedWrapperDescriptor::descr_get);
......@@ -650,6 +677,7 @@ void setupDescr() {
"__call__", new BoxedFunction(boxRTFunction((void*)BoxedWrapperObject::__call__, UNKNOWN, 1, 0, true, true)));
wrapperobject_cls->tpp_call.capi_val = BoxedWrapperObject::tppCall<CAPI>;
wrapperobject_cls->tpp_call.cxx_val = BoxedWrapperObject::tppCall<CXX>;
wrapperobject_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)wrapperObjectRepr, UNKNOWN, 1)));
wrapperobject_cls->freeze();
}
......
......@@ -1484,34 +1484,13 @@ extern "C" Box* noneNonzero(Box* v) {
}
extern "C" BoxedString* builtinFunctionOrMethodRepr(BoxedBuiltinFunctionOrMethod* v) {
// TODO there has to be a better way
if (v == repr_obj)
return boxString("<built-in function repr>");
if (v == len_obj)
return boxString("<built-in function len>");
if (v == hash_obj)
return boxString("<built-in function hash>");
if (v == range_obj)
return boxString("<built-in function range>");
if (v == abs_obj)
return boxString("<built-in function abs>");
if (v == min_obj)
return boxString("<built-in function min>");
if (v == max_obj)
return boxString("<built-in function max>");
if (v == open_obj)
return boxString("<built-in function open>");
if (v == id_obj)
return boxString("<built-in function id>");
if (v == chr_obj)
return boxString("<built-in function chr>");
if (v == ord_obj)
return boxString("<built-in function ord>");
if (v->name != NULL)
return (BoxedString*)PyString_FromFormat("<built-in function %s>", PyString_AsString(v->name));
RELEASE_ASSERT(false, "builtinFunctionOrMethodRepr not properly implemented");
}
extern "C" BoxedString* functionRepr(BoxedFunction* v) {
return boxString("function");
return (BoxedString*)PyString_FromFormat("<function %s at %p>", PyString_AsString(v->name), v);
}
static Box* functionGet(BoxedFunction* self, Box* inst, Box* owner) {
......@@ -2551,14 +2530,21 @@ static PyObject* object_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
return type->tp_alloc(type, 0);
}
Box* objectRepr(Box* obj) {
char buf[80];
if (obj->cls == type_cls) {
snprintf(buf, 80, "<type '%s'>", getNameOfClass(static_cast<BoxedClass*>(obj)));
} else {
snprintf(buf, 80, "<%s object at %p>", getTypeName(obj), obj);
static Box* typeName(Box* b, void*);
Box* objectRepr(Box* self) {
BoxedClass* type = self->cls;
Box* mod = NULL;
try {
mod = typeModule(type, NULL);
if (!PyString_Check(mod))
mod = NULL;
} catch (ExcInfo) {
}
return boxString(buf);
Box* name = typeName(type, NULL);
if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
return PyString_FromFormat("<%s.%s object at %p>", PyString_AS_STRING(mod), PyString_AS_STRING(name), self);
return PyString_FromFormat("<%s object at %p>", type->tp_name, self);
}
static Box* object_str(Box* obj) noexcept {
......
......@@ -7,3 +7,22 @@ except AttributeError, e:
print repr("both\'\"quotes")
print repr("single\'quote")
print repr("double\"quote")
import re
def p(o):
s = repr(o)
s = re.sub("0x[0-9a-fA-F]+", "0x0", s)
return s
def foo():
pass
class C(object):
def a(self):
pass
print p(C.a), p(C().a)
print p(C.__str__), p(C().__str__)
print p(type(u"").find)
print p(foo)
foo.__name__ = "bar"
print p(foo)
print p(sorted)
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