Commit 8aea2cc5 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Misc builtin functions

parent b2e8f49d
......@@ -483,8 +483,8 @@ Box* issubclass_func(Box* child, Box* parent) {
return boxBool(classobjIssubclass(static_cast<BoxedClassobj*>(child), static_cast<BoxedClassobj*>(parent)));
}
assert(isSubclass(child->cls, type_cls));
if (parent->cls != type_cls)
RELEASE_ASSERT(isSubclass(child->cls, type_cls), "");
if (!isSubclass(parent->cls, type_cls))
return False;
return boxBool(isSubclass(static_cast<BoxedClass*>(child), static_cast<BoxedClass*>(parent)));
......
......@@ -231,6 +231,14 @@ Box* setUpdate(BoxedSet* self, BoxedTuple* args) {
return None;
}
Box* setCopy(BoxedSet* self) {
assert(self->cls == set_cls);
BoxedSet* rtn = new BoxedSet();
rtn->s.insert(self->s.begin(), self->s.end());
return rtn;
}
Box* setContains(BoxedSet* self, Box* v) {
assert(self->cls == set_cls || self->cls == frozenset_cls);
return boxBool(self->s.count(v) != 0);
......@@ -313,6 +321,8 @@ void setupSet() {
set_cls->giveAttr("clear", new BoxedFunction(boxRTFunction((void*)setClear, NONE, 1)));
set_cls->giveAttr("update", new BoxedFunction(boxRTFunction((void*)setUpdate, NONE, 1, 0, true, false)));
set_cls->giveAttr("copy", new BoxedFunction(boxRTFunction((void*)setCopy, UNKNOWN, 1)));
set_cls->freeze();
frozenset_cls->freeze();
}
......
......@@ -614,7 +614,7 @@ extern "C" Box* createUserClass(const std::string* name, Box* _bases, Box* _attr
}
// Go through these routines since they do some normalization:
PyErr_Restore(e.type, e.value, NULL);
PyErr_Restore(e.type, e.value, e.traceback);
throwCAPIException();
}
}
......@@ -1012,6 +1012,21 @@ public:
return None;
}
static Box* setdefault(Box* _self, Box* _key, Box* value) {
RELEASE_ASSERT(_self->cls == attrwrapper_cls, "");
AttrWrapper* self = static_cast<AttrWrapper*>(_self);
_key = coerceUnicodeToStr(_key);
RELEASE_ASSERT(_key->cls == str_cls, "");
BoxedString* key = static_cast<BoxedString*>(_key);
Box* cur = self->b->getattr(key->s);
if (cur)
return cur;
self->b->setattr(key->s, value, NULL);
return value;
}
static Box* get(Box* _self, Box* _key, Box* def) {
RELEASE_ASSERT(_self->cls == attrwrapper_cls, "");
AttrWrapper* self = static_cast<AttrWrapper*>(_self);
......@@ -1145,8 +1160,14 @@ public:
for (const auto& p : attrs->hcls->attr_offsets) {
self->b->setattr(p.first, attrs->attr_list->attrs[p.second], NULL);
}
} else if (_container->cls == dict_cls) {
BoxedDict* container = static_cast<BoxedDict*>(_container);
for (const auto& p : container->d) {
AttrWrapper::setitem(self, p.first, p.second);
}
} else {
RELEASE_ASSERT(0, "not implemented");
RELEASE_ASSERT(0, "not implemented: %s", _container->cls->tp_name);
}
return None;
}
......@@ -1820,6 +1841,8 @@ void setupRuntime() {
attrwrapper_cls->giveAttr("__setitem__", new BoxedFunction(boxRTFunction((void*)AttrWrapper::setitem, UNKNOWN, 3)));
attrwrapper_cls->giveAttr("__getitem__", new BoxedFunction(boxRTFunction((void*)AttrWrapper::getitem, UNKNOWN, 2)));
attrwrapper_cls->giveAttr("setdefault",
new BoxedFunction(boxRTFunction((void*)AttrWrapper::setdefault, UNKNOWN, 3)));
attrwrapper_cls->giveAttr(
"get", new BoxedFunction(boxRTFunction((void*)AttrWrapper::get, UNKNOWN, 3, 1, false, false), { None }));
attrwrapper_cls->giveAttr("__str__", new BoxedFunction(boxRTFunction((void*)AttrWrapper::str, UNKNOWN, 1)));
......
......@@ -13,7 +13,8 @@ def fake():
class TestClass(object):
def __init__(self):
self.__dict__ = {n: n for n in fake()}
for n in fake():
setattr(self, n, n)
def __dir__(self):
return fake()
......@@ -21,7 +22,8 @@ class TestClass(object):
class TestClass2(object):
def __init__(self):
self.__dict__ = {'dictAttr': False, 'attribute1': None}
self.dictAttr = False
self.attribute1 = None
self.other_attribute = False
def method1(self):
......@@ -63,7 +65,7 @@ test_in_dir(['__str__', '__new__', '__repr__', '__dir__', '__init__',
test_in_dir(['__str__', '__new__', '__repr__', '__dir__', '__init__',
'__module__', 'method1', 'dictAttr', 'attribute1'], TestClass2)
test_in_dir(['attribute1', 'dictAttr', '__init__', '__module__', 'method1',
'other_attribute', '__dict__'], TestClass2())
'other_attribute'], TestClass2())
test_in_dir(fake(), TestClass())
print len(fake()) == len(dir(TestClass()))
......@@ -91,3 +93,7 @@ for x in d1:
l.append(x)
l.sort()
print l
c = C1()
c.__dict__.update(dict(a=1, b=5))
print sorted(c.__dict__.items())
......@@ -74,3 +74,8 @@ except KeyError, e:
def f2():
print {5}
f2()
s = set([])
s2 = s.copy()
s.add(1)
print s, s2
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