| 1 | """ |
| 2 | Read-only proxy for mappings. |
| 3 | |
| 4 | Its main use is as the return type of cls.__dict__. |
| 5 | """ |
| 6 | |
| 7 | from pypy.interpreter.baseobjspace import W_Root |
| 8 | from pypy.interpreter.error import oefmt |
| 9 | from pypy.interpreter.gateway import unwrap_spec, WrappedDefault |
| 10 | from pypy.interpreter.typedef import TypeDef, interp2app |
| 11 | from pypy.objspace.std.util import generic_alias_class_getitem |
| 12 | |
| 13 | class W_DictProxyObject(W_Root): |
| 14 | "Read-only proxy for mappings." |
| 15 | |
| 16 | def __init__(self, w_mapping): |
| 17 | self.w_mapping = w_mapping |
| 18 | |
| 19 | @staticmethod |
| 20 | def descr_new(space, w_type, w_mapping): |
| 21 | if (not space.lookup(w_mapping, "__getitem__") or |
| 22 | space.isinstance_w(w_mapping, space.w_list) or |
| 23 | space.isinstance_w(w_mapping, space.w_tuple)): |
| 24 | raise oefmt(space.w_TypeError, |
| 25 | "mappingproxy() argument must be a mapping, not %T", |
| 26 | w_mapping) |
| 27 | return W_DictProxyObject(w_mapping) |
| 28 | |
| 29 | def descr_init(self, space, __args__): |
| 30 | pass |
| 31 | |
| 32 | def descr_len(self, space): |
| 33 | return space.len(self.w_mapping) |
| 34 | |
| 35 | def descr_getitem(self, space, w_key): |
| 36 | return space.getitem(self.w_mapping, w_key) |
| 37 | |
| 38 | def descr_contains(self, space, w_key): |
| 39 | return space.contains(self.w_mapping, w_key) |
| 40 | |
| 41 | def descr_iter(self, space): |
| 42 | return space.iter(self.w_mapping) |
| 43 | |
| 44 | def descr_str(self, space): |
| 45 | return space.str(self.w_mapping) |
| 46 | |
| 47 | def descr_repr(self, space): |
| 48 | return space.newtext(b"mappingproxy(%s)" % |
| 49 | (space.utf8_w(space.repr(self.w_mapping)),)) |
| 50 | |
| 51 | def descr_or(self, space, w_other): |
| 52 | if isinstance(w_other, W_DictProxyObject): |
| 53 | w_other = w_other.w_mapping |
| 54 | if not space.isinstance_w(w_other, space.w_dict): |
| 55 | return space.w_NotImplemented |
| 56 | w_copyself = self.copy_w(space) |
| 57 | space.call_method(w_copyself, "update", w_other) |
| 58 | return w_copyself |
| 59 | |
| 60 | def descr_ror(self, space, w_other): |
| 61 | if isinstance(w_other, W_DictProxyObject): |
| 62 | w_other = w_other.w_mapping |
| 63 | if not space.isinstance_w(w_other, space.w_dict): |
| 64 | return space.w_NotImplemented |
| 65 | return space.call_method(w_other, "__or__", self.w_mapping) |
| 66 | |
| 67 | def descr_ior(self, space, w_other): |
| 68 | raise oefmt(space.w_TypeError, |
| 69 | "'|=' is not supported by mappingproxy; use '|' instead") |
| 70 | |
| 71 | @unwrap_spec(w_default=WrappedDefault(None)) |
| 72 | def get_w(self, space, w_key, w_default): |
| 73 | return space.call_method(self.w_mapping, "get", w_key, w_default) |
| 74 | |
| 75 | def keys_w(self, space): |
| 76 | return space.call_method(self.w_mapping, "keys") |
| 77 | |
| 78 | def values_w(self, space): |
| 79 | return space.call_method(self.w_mapping, "values") |
| 80 | |
| 81 | def items_w(self, space): |
| 82 | return space.call_method(self.w_mapping, "items") |
| 83 | |
| 84 | def copy_w(self, space): |
| 85 | return space.call_method(self.w_mapping, "copy") |
| 86 | |
| 87 | def descr_reversed(self, space): |
| 88 | return space.call_method(self.w_mapping, "__reversed__") |
| 89 | |
| 90 | cmp_methods = {} |
| 91 | def make_cmp_method(op): |
| 92 | def descr_op(self, space, w_other): |
| 93 | return getattr(space, op)(self.w_mapping, w_other) |
| 94 | descr_name = 'descr_' + op |
| 95 | descr_op.__name__ = descr_name |
| 96 | setattr(W_DictProxyObject, descr_name, descr_op) |
| 97 | cmp_methods['__%s__' % op] = interp2app(getattr(W_DictProxyObject, descr_name)) |
| 98 | |
| 99 | for op in ['eq', 'ne', 'gt', 'ge', 'lt', 'le']: |
| 100 | make_cmp_method(op) |
| 101 | |
| 102 | |
| 103 | W_DictProxyObject.typedef = TypeDef( |
| 104 | 'mappingproxy', |
| 105 | __new__=interp2app(W_DictProxyObject.descr_new), |
| 106 | __init__=interp2app(W_DictProxyObject.descr_init), |
| 107 | __len__=interp2app(W_DictProxyObject.descr_len), |
| 108 | __getitem__=interp2app(W_DictProxyObject.descr_getitem), |
| 109 | __contains__=interp2app(W_DictProxyObject.descr_contains), |
| 110 | __iter__=interp2app(W_DictProxyObject.descr_iter), |
| 111 | __str__=interp2app(W_DictProxyObject.descr_str), |
| 112 | __repr__=interp2app(W_DictProxyObject.descr_repr), |
| 113 | __or__=interp2app(W_DictProxyObject.descr_or), |
| 114 | __ror__=interp2app(W_DictProxyObject.descr_ror), |
| 115 | __ior__=interp2app(W_DictProxyObject.descr_ior), |
| 116 | __reversed__ = interp2app(W_DictProxyObject.descr_reversed), |
| 117 | __class_getitem__ = interp2app( |
| 118 | generic_alias_class_getitem, as_classmethod=True), |
| 119 | get=interp2app(W_DictProxyObject.get_w), |
| 120 | keys=interp2app(W_DictProxyObject.keys_w), |
| 121 | values=interp2app(W_DictProxyObject.values_w), |
| 122 | items=interp2app(W_DictProxyObject.items_w), |
| 123 | copy=interp2app(W_DictProxyObject.copy_w), |
| 124 | **cmp_methods |
| 125 | ) |
| 126 | |
| 127 | def _set_flag_map_or_seq(space): |
| 128 | w_type = space.gettypeobject(W_DictProxyObject.typedef) |
| 129 | w_type.flag_map_or_seq = 'M' |