def test_proxy_bind_method(self): class A(object): pass def f(self): return 3 class AA(object): pass from __pypy__ import tproxy as proxy a = A() class X(object): def __init__(self, x): self.x = x def f(self, name, *args, **kwargs): return getattr(self.x, name)(*args, **kwargs) y = proxy(type(f), X(f).f) x = proxy(AA, X(a).f) AA.f = y assert x.f() == 3
def get_proxy(f): from __pypy__ import tproxy as proxy return proxy(type(f), Controller(f).perform)
def unwrap(self, data): """ Unwrap an object """ if data == 'n': return None tp_letter, obj_data = data tp = self.letter_types[tp_letter] if tp is None: return self.keeper.get_object(obj_data) elif tp is RemoteBase: return self.keeper.exported_types_reverse[obj_data] elif tp in self.immutable_primitives: return obj_data # this is the object elif tp is tuple: return tuple([self.unwrap(i) for i in obj_data]) elif tp in self.mutable_primitives: id = obj_data ro = RemoteBuiltinObject(self, id) self.keeper.register_remote_object(ro.perform, id) p = proxy(tp, ro.perform) ro.obj = p return p elif tp is Exception: cls_name, w_args = obj_data return getattr(exceptions, cls_name)(self.unwrap(w_args)) elif tp is exceptions: cls_name = obj_data return getattr(exceptions, cls_name) elif tp is types.MethodType: w_class, w_name, w_func, w_self = obj_data tp = self.unwrap(w_class) name = self.unwrap(w_name) self_ = self.unwrap(w_self) if self_ is not None: if tp is None: setattr(self_, name, classmethod(self.unwrap(w_func))) return getattr(self_, name) return getattr(tp, name).__get__(self_, tp) func = self.unwrap(w_func) setattr(tp, name, func) return getattr(tp, name) elif tp is type: if isinstance(obj_data, str): return self.letter_types[obj_data] id = obj_data return self.get_type(obj_data) elif tp is DataDescriptor: return faker.unwrap_getset_descriptor(self, obj_data) elif tp is NonDataDescriptor: return faker.unwrap_get_descriptor(self, obj_data) elif tp is object: # we need to create a proper type w_tp, id = obj_data real_tp = self.unwrap(w_tp) ro = RemoteObject(self, id) self.keeper.register_remote_object(ro.perform, id) p = proxy(real_tp, ro.perform) ro.obj = p return p else: raise NotImplementedError("Cannot unwrap %s" % (data,))