| 1 | """ transparent list implementation |
| 2 | """ |
| 3 | from pypy.interpreter import baseobjspace |
| 4 | from pypy.interpreter.error import OperationError, oefmt |
| 5 | from pypy.module.exceptions import interp_exceptions |
| 6 | |
| 7 | def transparent_class(name, BaseCls): |
| 8 | class W_Transparent(BaseCls): |
| 9 | def __init__(self, space, w_type, w_controller): |
| 10 | self.w_type = w_type |
| 11 | self.w_controller = w_controller |
| 12 | |
| 13 | def descr_call_mismatch(self, space, name, reqcls, args): |
| 14 | args_w = args.arguments_w[:] |
| 15 | args_w[0] = space.newtext(name) |
| 16 | args = args.replace_arguments(args_w) |
| 17 | return space.call_args(self.w_controller, args) |
| 18 | |
| 19 | def getclass(self, space): |
| 20 | return self.w_type |
| 21 | |
| 22 | def setclass(self, space, w_subtype): |
| 23 | raise oefmt(space.w_TypeError, |
| 24 | "You cannot override __class__ for transparent " |
| 25 | "proxies") |
| 26 | |
| 27 | def getdictvalue(self, space, attr): |
| 28 | try: |
| 29 | return space.call_function(self.w_controller, space.newtext('__getattribute__'), |
| 30 | space.newtext(attr)) |
| 31 | except OperationError as e: |
| 32 | if not e.match(space, space.w_AttributeError): |
| 33 | raise |
| 34 | return None |
| 35 | |
| 36 | def setdictvalue(self, space, attr, w_value): |
| 37 | try: |
| 38 | space.call_function(self.w_controller, space.newtext('__setattr__'), |
| 39 | space.newtext(attr), w_value) |
| 40 | return True |
| 41 | except OperationError as e: |
| 42 | if not e.match(space, space.w_AttributeError): |
| 43 | raise |
| 44 | return False |
| 45 | |
| 46 | def deldictvalue(self, space, attr): |
| 47 | try: |
| 48 | space.call_function(self.w_controller, space.newtext('__delattr__'), |
| 49 | space.newtext(attr)) |
| 50 | return True |
| 51 | except OperationError as e: |
| 52 | if not e.match(space, space.w_AttributeError): |
| 53 | raise |
| 54 | return False |
| 55 | |
| 56 | def getdict(self, space): |
| 57 | return self.getdictvalue(space, '__dict__') |
| 58 | |
| 59 | def setdict(self, space, w_dict): |
| 60 | if not self.setdictvalue(space, '__dict__', w_dict): |
| 61 | baseobjspace.W_Root.setdict(self, space, w_dict) |
| 62 | |
| 63 | W_Transparent.__name__ = name |
| 64 | return W_Transparent |
| 65 | |
| 66 | W_Transparent = transparent_class('W_Transparent', baseobjspace.W_Root) |
| 67 | W_TransparentBaseException = transparent_class('W_TransparentBaseException', interp_exceptions.W_BaseException) |