Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
147 lines (122 sloc) 3.49 KB
from builtin import signature
from rpython.rlib.objectmodel import compute_hash, r_dict
from interface import Object
from listobject import List
from numbers import Integer
import space
def eq_fn(this, other):
return this.eq(other)
def hash_fn(this):
return this.hash()
class Dict(Object):
_immutable_fields_ = ['data']
__slots__ = ['data']
def __init__(self):
self.data = r_dict(eq_fn, hash_fn, force_non_null=True)
def contains(self, index):
if index in self.data:
return True
return False
def getattr(self, name):
if name == u'length':
return Integer(len(self.data))
return Object.getattr(self, name)
def getitem(self, index):
try:
return self.data[index]
except KeyError as _:
raise space.unwind(space.LKeyError(self, index))
def setitem(self, index, value):
self.data[index] = value
return value
def iter(self):
return KeyIterator(self.data.iterkeys())
@Dict.builtin_method
def get(argv):
if len(argv) == 2:
v0 = argv[0]
v1 = argv[1]
v2 = space.null
else:
assert len(argv) == 3
v0 = argv[0]
v1 = argv[1]
v2 = argv[2]
assert isinstance(v0, Dict)
try:
return v0.data[v1]
except KeyError as error:
return v2
@Dict.method(u"pop", signature(Dict, Object))
def Dict_pop(self, key):
try:
return self.data.pop(key)
except KeyError as error:
raise space.unwind(space.LKeyError(self, key))
@Dict.builtin_method
@signature(Dict)
def keys(self):
return KeyIterator(self.data.iterkeys())
@Dict.builtin_method
@signature(Dict)
def items(self):
return ItemIterator(self.data.iteritems())
@Dict.builtin_method
@signature(Dict)
def values(self):
return ValueIterator(self.data.itervalues())
class KeyIterator(Object):
_immutable_fields_ = ['iterator']
def __init__(self, iterator):
self.iterator = iterator
def iter(self):
return self
@KeyIterator.builtin_method
@signature(KeyIterator)
def next(self):
return self.iterator.next()
class ItemIterator(Object):
_immutable_fields_ = ['iterator']
def __init__(self, iterator):
self.iterator = iterator
def iter(self):
return self
@ItemIterator.builtin_method
@signature(ItemIterator)
def next(self):
key, value = self.iterator.next()
return List([key, value])
class ValueIterator(Object):
_immutable_fields_ = ['iterator']
def __init__(self, iterator):
self.iterator = iterator
def iter(self):
return self
@ValueIterator.builtin_method
@signature(ValueIterator)
def next(self):
return self.iterator.next()
@Dict.instantiator2(signature(Object, optional=1))
def instantiate(other):
dict_ = Dict()
if other is not None:
Dict_update(dict_, other)
return dict_
@Dict.method(u"update", signature(Dict, Object))
def Dict_update(self, other):
if isinstance(other, Dict):
self.data.update(other.data)
elif isinstance(other, space.Exnihilo):
for name in other.map.attribute_indexes:
self.setitem(space.String(name), other.getattr(name))
else:
it = other.iter()
try:
while True:
item = it.callattr(u"next", [])
key = item.getitem(Integer(0))
val = item.getitem(Integer(1))
self.setitem(key, val)
except StopIteration as stop:
pass
return space.null