| 1 |
"""dict implementation specialized for object loaded by the _pypyjson module. |
| 2 |
|
| 3 |
Somewhat similar to MapDictStrategy, also uses a map. |
| 4 |
""" |
| 5 |
|
| 6 |
from rpython.rlib import jit, rerased, objectmodel, debug |
| 7 |
|
| 8 |
from pypy.objspace.std.dictmultiobject import ( |
| 9 |
UnicodeDictStrategy, DictStrategy, |
| 10 |
create_iterator_classes, W_DictObject) |
| 11 |
|
| 12 |
|
| 13 |
def from_values_and_jsonmap(space, values_w, jsonmap): |
| 14 |
if not objectmodel.we_are_translated(): |
| 15 |
assert len(values_w) == len(jsonmap.get_keys_in_order()) |
| 16 |
assert len(values_w) != 0 |
| 17 |
debug.make_sure_not_resized(values_w) |
| 18 |
strategy = jsonmap.strategy_instance |
| 19 |
if strategy is None: |
| 20 |
jsonmap.strategy_instance = strategy = JsonDictStrategy(space, jsonmap) |
| 21 |
storage = strategy.erase(values_w) |
| 22 |
return W_DictObject(space, strategy, storage) |
| 23 |
|
| 24 |
def devolve_jsonmap_dict(w_dict): |
| 25 |
assert isinstance(w_dict, W_DictObject) |
| 26 |
strategy = w_dict.get_strategy() |
| 27 |
assert isinstance(strategy, JsonDictStrategy) |
| 28 |
strategy.switch_to_unicode_strategy(w_dict) |
| 29 |
|
| 30 |
def get_jsonmap_from_dict(w_dict): |
| 31 |
assert isinstance(w_dict, W_DictObject) |
| 32 |
strategy = w_dict.get_strategy() |
| 33 |
assert isinstance(strategy, JsonDictStrategy) |
| 34 |
return strategy.jsonmap |
| 35 |
|
| 36 |
class JsonDictStrategy(DictStrategy): |
| 37 |
erase, unerase = rerased.new_erasing_pair("jsondict") |
| 38 |
erase = staticmethod(erase) |
| 39 |
unerase = staticmethod(unerase) |
| 40 |
|
| 41 |
_immutable_fields_ = ['jsonmap'] |
| 42 |
|
| 43 |
def __init__(self, space, jsonmap): |
| 44 |
DictStrategy.__init__(self, space) |
| 45 |
self.jsonmap = jsonmap |
| 46 |
|
| 47 |
def wrap(self, w_key): |
| 48 |
return w_key |
| 49 |
|
| 50 |
def wrapkey(space, key): |
| 51 |
return key |
| 52 |
|
| 53 |
def get_empty_storage(self): |
| 54 |
raise NotImplementedError("should not be reachable") |
| 55 |
|
| 56 |
def is_correct_type(self, w_obj): |
| 57 |
space = self.space |
| 58 |
return space.is_w(space.type(w_obj), space.w_unicode) |
| 59 |
|
| 60 |
def _never_equal_to(self, w_lookup_type): |
| 61 |
return False |
| 62 |
|
| 63 |
def length(self, w_dict): |
| 64 |
return len(self.unerase(w_dict.dstorage)) |
| 65 |
|
| 66 |
def getitem(self, w_dict, w_key): |
| 67 |
if self.is_correct_type(w_key): |
| 68 |
return self.getitem_unicode(w_dict, w_key) |
| 69 |
else: |
| 70 |
self.switch_to_unicode_strategy(w_dict) |
| 71 |
return w_dict.getitem(w_key) |
| 72 |
|
| 73 |
def getitem_unicode(self, w_dict, w_key): |
| 74 |
storage_w = self.unerase(w_dict.dstorage) |
| 75 |
if jit.isconstant(w_key): |
| 76 |
jit.promote(self) |
| 77 |
index = self.jsonmap.get_index(w_key) |
| 78 |
if index == -1: |
| 79 |
return None |
| 80 |
return storage_w[index] |
| 81 |
|
| 82 |
def setitem(self, w_dict, w_key, w_value): |
| 83 |
if self.is_correct_type(w_key): |
| 84 |
storage_w = self.unerase(w_dict.dstorage) |
| 85 |
index = self.jsonmap.get_index(w_key) |
| 86 |
if index != -1: |
| 87 |
storage_w[index] = w_value |
| 88 |
return |
| 89 |
self.switch_to_unicode_strategy(w_dict) |
| 90 |
w_dict.setitem(w_key, w_value) |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
def setdefault(self, w_dict, w_key, w_default): |
| 97 |
if self.is_correct_type(w_key): |
| 98 |
w_result = self.getitem_unicode(w_dict, w_key) |
| 99 |
if w_result is not None: |
| 100 |
return w_result |
| 101 |
self.switch_to_unicode_strategy(w_dict) |
| 102 |
return w_dict.setdefault(w_key, w_default) |
| 103 |
|
| 104 |
def delitem(self, w_dict, w_key): |
| 105 |
self.switch_to_unicode_strategy(w_dict) |
| 106 |
return w_dict.delitem(w_key) |
| 107 |
|
| 108 |
def popitem(self, w_dict): |
| 109 |
self.switch_to_unicode_strategy(w_dict) |
| 110 |
return w_dict.popitem() |
| 111 |
|
| 112 |
def switch_to_unicode_strategy(self, w_dict): |
| 113 |
storage = self._make_unicode_dict(w_dict) |
| 114 |
strategy = self.space.fromcache(UnicodeDictStrategy) |
| 115 |
w_dict.set_strategy(strategy) |
| 116 |
w_dict.dstorage = storage |
| 117 |
|
| 118 |
def _make_unicode_dict(self, w_dict): |
| 119 |
strategy = self.space.fromcache(UnicodeDictStrategy) |
| 120 |
values_w = self.unerase(w_dict.dstorage) |
| 121 |
storage = strategy.get_empty_storage() |
| 122 |
d_new = strategy.unerase(storage) |
| 123 |
keys_in_order = self.jsonmap.get_keys_in_order() |
| 124 |
assert len(keys_in_order) == len(values_w) |
| 125 |
for index, w_key in enumerate(keys_in_order): |
| 126 |
assert w_key is not None |
| 127 |
assert type(w_key) is self.space.UnicodeObjectCls |
| 128 |
d_new[w_key] = values_w[index] |
| 129 |
return storage |
| 130 |
|
| 131 |
def copy(self, w_dict): |
| 132 |
storage = self._make_unicode_dict(w_dict) |
| 133 |
strategy = self.space.fromcache(UnicodeDictStrategy) |
| 134 |
return W_DictObject(strategy.space, strategy, storage) |
| 135 |
|
| 136 |
def w_keys(self, w_dict): |
| 137 |
return self.space.newlist(self.jsonmap.get_keys_in_order()) |
| 138 |
|
| 139 |
def values(self, w_dict): |
| 140 |
return self.unerase(w_dict.dstorage)[:] |
| 141 |
|
| 142 |
def items(self, w_dict): |
| 143 |
space = self.space |
| 144 |
storage_w = self.unerase(w_dict.dstorage) |
| 145 |
res = [None] * len(storage_w) |
| 146 |
for index, w_key in enumerate(self.jsonmap.get_keys_in_order()): |
| 147 |
res[index] = space.newtuple2(w_key, storage_w[index]) |
| 148 |
return res |
| 149 |
|
| 150 |
def getiterkeys(self, w_dict): |
| 151 |
return iter(self.jsonmap.get_keys_in_order()) |
| 152 |
|
| 153 |
def getitervalues(self, w_dict): |
| 154 |
storage_w = self.unerase(w_dict.dstorage) |
| 155 |
return iter(storage_w) |
| 156 |
|
| 157 |
def getiteritems_with_hash(self, w_dict): |
| 158 |
storage_w = self.unerase(w_dict.dstorage) |
| 159 |
return ZipItemsWithHash(self.jsonmap.get_keys_in_order(), storage_w) |
| 160 |
|
| 161 |
|
| 162 |
class ZipItemsWithHash(object): |
| 163 |
def __init__(self, list1, list2): |
| 164 |
assert len(list1) == len(list2) |
| 165 |
self.list1 = list1 |
| 166 |
self.list2 = list2 |
| 167 |
self.i = 0 |
| 168 |
|
| 169 |
def __iter__(self): |
| 170 |
return self |
| 171 |
|
| 172 |
def next(self): |
| 173 |
i = self.i |
| 174 |
if i >= len(self.list1): |
| 175 |
raise StopIteration |
| 176 |
self.i = i + 1 |
| 177 |
w_key = self.list1[i] |
| 178 |
return (w_key, self.list2[i], w_key.hash_w()) |
| 179 |
|
| 180 |
|
| 181 |
create_iterator_classes(JsonDictStrategy) |