Commit 0ba7e94f authored by Kevin Modzelewski's avatar Kevin Modzelewski

dict.copy, obj.__dict__, str.split(max_split=)

With these changes, the optparse test successfully runs!
It outputs a slightly different thing for us, since it
looks at the str of obj.__dict__, which for us is currently
an AttrWrapper.

I think this case is benign, so I'm hopeful that we'll be able
to keep __dict__ as an AttrWrapper and not as a full dict
(which would require deoptimizing that object and/or tracking
the existence of that dict to see if we can reoptimize).
I'm not sure if we'll be able to, but there's only one way to find out.
parent eb51f03a
......@@ -1356,7 +1356,8 @@ public:
for (int i = args.size() + 1; i < cl->num_args; i++) {
// TODO should _call() be able to take llvm::Value's directly?
new_args.push_back(new ConcreteCompilerVariable(
UNKNOWN, embedConstantPtr(rtattr_func->defaults->elts[i - args.size() - 1], g.llvm_value_type_ptr),
UNKNOWN, embedConstantPtr(rtattr_func->defaults->elts[i - cl->num_args + cl->num_defaults],
g.llvm_value_type_ptr),
true));
}
......
......@@ -47,6 +47,14 @@ Box* dictRepr(BoxedDict* self) {
return boxString(std::string(chars.begin(), chars.end()));
}
Box* dictCopy(BoxedDict* self) {
RELEASE_ASSERT(self->cls == dict_cls, "");
BoxedDict* r = new BoxedDict();
r->d.insert(self->d.begin(), self->d.end());
return r;
}
Box* dictItems(BoxedDict* self) {
BoxedList* rtn = new BoxedList();
......@@ -266,6 +274,8 @@ void setupDict() {
dict_cls->giveAttr("__iter__",
new BoxedFunction(boxRTFunction((void*)dictIterKeys, typeFromClass(dict_iterator_cls), 1)));
dict_cls->giveAttr("copy", new BoxedFunction(boxRTFunction((void*)dictCopy, DICT, 1)));
dict_cls->giveAttr("items", new BoxedFunction(boxRTFunction((void*)dictItems, LIST, 1)));
dict_cls->giveAttr("iteritems",
new BoxedFunction(boxRTFunction((void*)dictIterItems, typeFromClass(dict_iterator_cls), 1)));
......
......@@ -1299,6 +1299,11 @@ extern "C" Box* getattr(Box* obj, const char* attr) {
#endif
}
if (strcmp(attr, "__dict__") == 0) {
if (obj->cls->instancesHaveAttrs())
return makeAttrWrapper(obj);
}
std::unique_ptr<Rewriter> rewriter(
Rewriter::createRewriter(__builtin_extract_return_addr(__builtin_return_address(0)), 2, "getattr"));
......
......@@ -533,13 +533,15 @@ Box* strJoin(BoxedString* self, Box* rhs) {
}
}
Box* strSplit(BoxedString* self, BoxedString* sep) {
Box* strSplit(BoxedString* self, BoxedString* sep, BoxedInt* _max_split) {
assert(self->cls == str_cls);
if (_max_split->cls != int_cls)
raiseExcHelper(TypeError, "an integer is required");
if (sep->cls == str_cls) {
if (!sep->s.empty()) {
llvm::SmallVector<llvm::StringRef, 16> parts;
llvm::StringRef(self->s).split(parts, sep->s);
llvm::StringRef(self->s).split(parts, sep->s, _max_split->n);
BoxedList* rtn = new BoxedList();
for (const auto& s : parts)
......@@ -549,6 +551,7 @@ Box* strSplit(BoxedString* self, BoxedString* sep) {
raiseExcHelper(ValueError, "empty separator");
}
} else if (sep->cls == none_cls) {
RELEASE_ASSERT(_max_split->n < 0, "this case hasn't been updated to handle limited splitting amounts");
BoxedList* rtn = new BoxedList();
std::ostringstream os("");
......@@ -571,6 +574,14 @@ Box* strSplit(BoxedString* self, BoxedString* sep) {
}
}
Box* strRsplit(BoxedString* self, BoxedString* sep, BoxedInt* _max_split) {
// TODO: implement this for real
// for now, just forward rsplit() to split() in the cases they have to return the same value
assert(_max_split->cls == int_cls);
RELEASE_ASSERT(_max_split->n <= 0, "");
return strSplit(self, sep, _max_split);
}
Box* strStrip(BoxedString* self, Box* chars) {
assert(self->cls == str_cls);
......@@ -867,8 +878,10 @@ void setupStr() {
str_cls->giveAttr("join", new BoxedFunction(boxRTFunction((void*)strJoin, STR, 2)));
str_cls->giveAttr("split", new BoxedFunction(boxRTFunction((void*)strSplit, LIST, 2, 1, false, false), { None }));
str_cls->giveAttr("rsplit", str_cls->getattr("split"));
str_cls->giveAttr(
"split", new BoxedFunction(boxRTFunction((void*)strSplit, LIST, 3, 2, false, false), { None, boxInt(-1) }));
str_cls->giveAttr(
"rsplit", new BoxedFunction(boxRTFunction((void*)strRsplit, LIST, 3, 2, false, false), { None, boxInt(-1) }));
CLFunction* count = boxRTFunction((void*)strCount2Unboxed, INT, 2);
addRTFunction(count, (void*)strCount2, BOXED_INT);
......
......@@ -583,6 +583,7 @@ public:
};
Box* makeAttrWrapper(Box* b) {
assert(b->cls->instancesHaveAttrs());
return new AttrWrapper(b);
}
......
......@@ -101,3 +101,11 @@ try:
del d[2]
except KeyError, e:
print e
d = {1:[2]}
d2 = d.copy()
print d2, d
d2[1].append(1)
print d2, d
d2[1] = 1
print d2, d
......@@ -53,3 +53,6 @@ test_comparisons("a", "aa")
test_comparisons("ab", "aa")
print sorted([str(i) for i in xrange(25)])
for i in xrange(-3, 5):
print i, "bananananananananana".split("an", i)
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