2
""" transparent.py - Several transparent proxy helpers
5
from pypy.interpreter import gateway
6
from pypy.interpreter.function import Function
7
from pypy.interpreter.error import OperationError
8
from pypy.objspace.std.proxyobject import *
9
from pypy.objspace.std.typeobject import W_TypeObject
11
def proxy(space, w_type, w_controller):
12
"""tproxy(typ, controller) -> obj
13
Return something that looks like it is of type typ. Its behaviour is
14
completely controlled by the controller."""
15
from pypy.interpreter.typedef import Function, PyTraceback, PyFrame, \
16
PyCode, GeneratorIterator
18
if not space.is_true(space.callable(w_controller)):
19
raise OperationError(space.w_TypeError, space.wrap("controller should be function"))
21
if isinstance(w_type, W_TypeObject):
22
if space.is_true(space.issubtype(w_type, space.w_list)):
23
return W_TransparentList(space, w_type, w_controller)
24
if space.is_true(space.issubtype(w_type, space.w_dict)):
25
return W_TransparentDict(space, w_type, w_controller)
26
if space.is_true(space.issubtype(w_type, space.gettypeobject(Function.typedef))):
27
return W_TransparentFunction(space, w_type, w_controller)
28
if space.is_true(space.issubtype(w_type, space.gettypeobject(PyTraceback.typedef))):
29
return W_TransparentTraceback(space, w_type, w_controller)
30
if space.is_true(space.issubtype(w_type, space.gettypeobject(PyFrame.typedef))):
31
return W_TransparentFrame(space, w_type, w_controller)
32
if space.is_true(space.issubtype(w_type, space.gettypeobject(GeneratorIterator.typedef))):
33
return W_TransparentGenerator(space, w_type, w_controller)
34
if space.is_true(space.issubtype(w_type, space.gettypeobject(PyCode.typedef))):
35
return W_TransparentCode(space, w_type, w_controller)
36
if w_type.instancetypedef is space.w_object.instancetypedef:
37
return W_Transparent(space, w_type, w_controller)
39
raise OperationError(space.w_TypeError, space.wrap("type expected as first argument"))
40
#return type_cache[w_type or w_type.w_bestbase]
41
raise OperationError(space.w_TypeError, space.wrap("Object type %s could not "\
42
"be wrapped (YET)" % w_type.getname(space, "?")))
44
def proxy_controller(space, w_object):
45
"""get_tproxy_controller(obj) -> controller
46
If obj is really a transparent proxy, return its controller. Otherwise return
48
if isinstance(w_object, W_Transparent):
49
return w_object.w_controller
50
if isinstance(w_object, W_TransparentObject):
51
return w_object.w_controller
54
app_proxy = gateway.interp2app(proxy, unwrap_spec=[gateway.ObjSpace, gateway.W_Root, \
56
app_proxy_controller = gateway.interp2app(proxy_controller, unwrap_spec=[gateway.ObjSpace, gateway.W_Root])