Commit 6abd5698 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Basic collections support

parent 7aaaf4bf
......@@ -124,7 +124,34 @@ class OrderedDict(dict):
for k in self:
yield (k, self[k])
update = MutableMapping.update
# Pyston change: copied the code in from _abcoll rather than calling "update = MutableMapping.update"
def update(*args, **kwds):
''' D.update([E, ]**F) -> None. Update D from mapping/iterable E and F.
If E present and has a .keys() method, does: for k in E: D[k] = E[k]
If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v
In either case, this is followed by: for k, v in F.items(): D[k] = v
'''
if len(args) > 2:
raise TypeError("update() takes at most 2 positional "
"arguments ({} given)".format(len(args)))
elif not args:
raise TypeError("update() takes at least 1 argument (0 given)")
self = args[0]
other = args[1] if len(args) >= 2 else ()
# Pyston change: changed this from "Mapping" to "dict"
if isinstance(other, dict):
for key in other:
self[key] = other[key]
elif hasattr(other, "keys"):
for key in other.keys():
self[key] = other[key]
else:
for key, value in other:
self[key] = value
for key, value in kwds.items():
self[key] = value
__update = update # let subclasses override update without breaking __init__
......
......@@ -368,9 +368,7 @@ extern "C" Box* dictNew(Box* _cls, BoxedTuple* args, BoxedDict* kwargs) {
raiseExcHelper(TypeError, "dict.__new__(%s): %s is not a subtype of dict", getNameOfClass(cls)->c_str(),
getNameOfClass(cls)->c_str());
RELEASE_ASSERT(cls == dict_cls, "");
return new BoxedDict();
return new (cls) BoxedDict();
}
void dictMerge(BoxedDict* self, Box* other) {
......
......@@ -1463,12 +1463,6 @@ extern "C" Box* getattr(Box* obj, const char* attr) {
#endif
}
if (strcmp(attr, "__dict__") == 0) {
// TODO this is wrong, should be added at the class level as a getset
if (obj->cls->instancesHaveHCAttrs())
return makeAttrWrapper(obj);
}
std::unique_ptr<Rewriter> rewriter(
Rewriter::createRewriter(__builtin_extract_return_addr(__builtin_return_address(0)), 2, "getattr"));
......@@ -1505,6 +1499,21 @@ extern "C" Box* getattr(Box* obj, const char* attr) {
if (val) {
return val;
}
if (attr[0] == '_' && attr[1] == '_') {
if (strcmp(attr, "__dict__") == 0) {
// TODO this is wrong, should be added at the class level as a getset
if (obj->cls->instancesHaveHCAttrs())
return makeAttrWrapper(obj);
}
// I'm not sure if there's more to it than this:
if (strcmp(attr, "__class__") == 0) {
assert(obj->cls != instance_cls); // I think in this case __class__ is supposed to be the classobj?
return obj->cls;
}
}
raiseAttributeError(obj, attr);
}
......
# skip-if: True
# - wip
# allow-warning: converting unicode literal to str
import collections
o = collections.OrderedDict()
print o.items()
for i in xrange(30):
o[(i ** 2) ^ 0xace] = i
print o
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