Commit 72bde9a5 authored by Kevin Modzelewski's avatar Kevin Modzelewski

vars(obj).items()

parent 8c5b2a78
...@@ -99,6 +99,12 @@ extern "C" Box* dir(Box* obj) { ...@@ -99,6 +99,12 @@ extern "C" Box* dir(Box* obj) {
return result; return result;
} }
extern "C" Box* vars(Box* obj) {
RELEASE_ASSERT(obj, "Don't support 0-arg vars() calls yet");
return makeAttrWrapper(obj);
}
extern "C" Box* abs_(Box* x) { extern "C" Box* abs_(Box* x) {
if (x->cls == int_cls) { if (x->cls == int_cls) {
i64 n = static_cast<BoxedInt*>(x)->n; i64 n = static_cast<BoxedInt*>(x)->n;
...@@ -919,6 +925,8 @@ void setupBuiltins() { ...@@ -919,6 +925,8 @@ void setupBuiltins() {
builtins_module->giveAttr("filter", new BoxedFunction(boxRTFunction((void*)filter2, LIST, 2))); builtins_module->giveAttr("filter", new BoxedFunction(boxRTFunction((void*)filter2, LIST, 2)));
builtins_module->giveAttr("zip", new BoxedFunction(boxRTFunction((void*)zip2, LIST, 2))); builtins_module->giveAttr("zip", new BoxedFunction(boxRTFunction((void*)zip2, LIST, 2)));
builtins_module->giveAttr("dir", new BoxedFunction(boxRTFunction((void*)dir, LIST, 1, 1, false, false), { NULL })); builtins_module->giveAttr("dir", new BoxedFunction(boxRTFunction((void*)dir, LIST, 1, 1, false, false), { NULL }));
builtins_module->giveAttr("vars",
new BoxedFunction(boxRTFunction((void*)vars, LIST, 1, 1, false, false), { NULL }));
builtins_module->giveAttr("object", object_cls); builtins_module->giveAttr("object", object_cls);
builtins_module->giveAttr("str", str_cls); builtins_module->giveAttr("str", str_cls);
builtins_module->giveAttr("basestring", basestring_cls); builtins_module->giveAttr("basestring", basestring_cls);
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include "runtime/classobj.h" #include "runtime/classobj.h"
#include "runtime/ics.h" #include "runtime/ics.h"
#include "runtime/iterobject.h" #include "runtime/iterobject.h"
#include "runtime/list.h"
#include "runtime/long.h" #include "runtime/long.h"
#include "runtime/objmodel.h" #include "runtime/objmodel.h"
#include "runtime/set.h" #include "runtime/set.h"
...@@ -726,6 +727,20 @@ public: ...@@ -726,6 +727,20 @@ public:
Box* r = self->b->getattr(key->s); Box* r = self->b->getattr(key->s);
return r ? True : False; return r ? True : False;
} }
static Box* items(Box* _self) {
RELEASE_ASSERT(_self->cls == attrwrapper_cls, "");
AttrWrapper* self = static_cast<AttrWrapper*>(_self);
BoxedList* rtn = new BoxedList();
HCAttrs* attrs = self->b->getAttrsPtr();
for (const auto& p : attrs->hcls->attr_offsets) {
BoxedTuple* t = new BoxedTuple({ boxString(p.first), attrs->attr_list->attrs[p.second] });
listAppend(rtn, t);
}
return rtn;
}
}; };
Box* makeAttrWrapper(Box* b) { Box* makeAttrWrapper(Box* b) {
...@@ -937,6 +952,7 @@ void setupRuntime() { ...@@ -937,6 +952,7 @@ void setupRuntime() {
attrwrapper_cls->giveAttr("__str__", new BoxedFunction(boxRTFunction((void*)AttrWrapper::str, UNKNOWN, 1))); attrwrapper_cls->giveAttr("__str__", new BoxedFunction(boxRTFunction((void*)AttrWrapper::str, UNKNOWN, 1)));
attrwrapper_cls->giveAttr("__contains__", attrwrapper_cls->giveAttr("__contains__",
new BoxedFunction(boxRTFunction((void*)AttrWrapper::contains, UNKNOWN, 2))); new BoxedFunction(boxRTFunction((void*)AttrWrapper::contains, UNKNOWN, 2)));
attrwrapper_cls->giveAttr("items", new BoxedFunction(boxRTFunction((void*)AttrWrapper::items, LIST, 1)));
attrwrapper_cls->freeze(); attrwrapper_cls->freeze();
// sys is the first module that needs to be set up, due to modules // sys is the first module that needs to be set up, due to modules
......
...@@ -51,3 +51,7 @@ def G(): ...@@ -51,3 +51,7 @@ def G():
yield "A"; yield "B"; yield "C" yield "A"; yield "B"; yield "C"
print list(enumerate(G())) print list(enumerate(G()))
class C(object):
def __init__(self):
self.a = 1
print vars(C()).items()
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