| 1 |
""" transparent.py - Several transparent proxy helpers |
| 2 |
""" |
| 3 |
from pypy.interpreter import gateway |
| 4 |
from pypy.interpreter.error import OperationError, oefmt |
| 5 |
from pypy.interpreter.typedef import Function, PyTraceback, \ |
| 6 |
PyFrame, PyCode |
| 7 |
from pypy.interpreter.generator import GeneratorIterator |
| 8 |
from pypy.objspace.std.proxyobject import W_Transparent, W_TransparentBaseException |
| 9 |
from pypy.objspace.std.typeobject import W_TypeObject |
| 10 |
from pypy.module.exceptions import interp_exceptions |
| 11 |
from rpython.rlib.unroll import unrolling_iterable |
| 12 |
|
| 13 |
|
| 14 |
class W_TransparentFunction(W_Transparent): |
| 15 |
typedef = Function.typedef |
| 16 |
|
| 17 |
class W_TransparentTraceback(W_Transparent): |
| 18 |
typedef = PyTraceback.typedef |
| 19 |
|
| 20 |
class W_TransparentCode(W_Transparent): |
| 21 |
typedef = PyCode.typedef |
| 22 |
|
| 23 |
class W_TransparentFrame(W_Transparent): |
| 24 |
typedef = PyFrame.typedef |
| 25 |
|
| 26 |
class W_TransparentGenerator(W_Transparent): |
| 27 |
typedef = GeneratorIterator.typedef |
| 28 |
|
| 29 |
|
| 30 |
class TypeCache(object): |
| 31 |
def __init__(self): |
| 32 |
self.cache = [] |
| 33 |
|
| 34 |
def _freeze_(self): |
| 35 |
self.cache = unrolling_iterable(self.cache) |
| 36 |
return True |
| 37 |
|
| 38 |
type_cache = TypeCache() |
| 39 |
|
| 40 |
|
| 41 |
def setup(space): |
| 42 |
"""Add proxy functions to the __pypy__ module.""" |
| 43 |
w___pypy__ = space.getbuiltinmodule("__pypy__") |
| 44 |
space.setattr(w___pypy__, space.newtext('tproxy'), app_proxy.spacebind(space)) |
| 45 |
space.setattr(w___pypy__, space.newtext('get_tproxy_controller'), |
| 46 |
app_proxy_controller.spacebind(space)) |
| 47 |
|
| 48 |
|
| 49 |
def proxy(space, w_type, w_controller): |
| 50 |
"""tproxy(typ, controller) -> obj |
| 51 |
Return something that looks like it is of type typ. Its behaviour is |
| 52 |
completely controlled by the controller.""" |
| 53 |
if not space.is_true(space.callable(w_controller)): |
| 54 |
raise oefmt(space.w_TypeError, "controller should be function") |
| 55 |
|
| 56 |
if isinstance(w_type, W_TypeObject): |
| 57 |
if space.issubtype_w(w_type, space.gettypeobject(Function.typedef)): |
| 58 |
return W_TransparentFunction(space, w_type, w_controller) |
| 59 |
if space.issubtype_w(w_type, space.gettypeobject(PyTraceback.typedef)): |
| 60 |
return W_TransparentTraceback(space, w_type, w_controller) |
| 61 |
if space.issubtype_w(w_type, space.gettypeobject(PyFrame.typedef)): |
| 62 |
return W_TransparentFrame(space, w_type, w_controller) |
| 63 |
if space.issubtype_w(w_type, space.gettypeobject(GeneratorIterator.typedef)): |
| 64 |
return W_TransparentGenerator(space, w_type, w_controller) |
| 65 |
if space.issubtype_w(w_type, space.gettypeobject(PyCode.typedef)): |
| 66 |
return W_TransparentCode(space, w_type, w_controller) |
| 67 |
if space.issubtype_w(w_type, space.gettypeobject(interp_exceptions.W_BaseException.typedef)): |
| 68 |
return W_TransparentBaseException(space, w_type, w_controller) |
| 69 |
if w_type.layout.typedef is space.w_object.layout.typedef: |
| 70 |
return W_Transparent(space, w_type, w_controller) |
| 71 |
else: |
| 72 |
raise oefmt(space.w_TypeError, "type expected as first argument") |
| 73 |
w_lookup = w_type |
| 74 |
for k, v in type_cache.cache: |
| 75 |
if w_lookup == k: |
| 76 |
return v(space, w_type, w_controller) |
| 77 |
raise oefmt(space.w_TypeError, "'%N' object could not be wrapped", w_type) |
| 78 |
|
| 79 |
def register_proxyable(space, cls): |
| 80 |
tpdef = cls.typedef |
| 81 |
class W_TransparentUserCreated(W_Transparent): |
| 82 |
typedef = tpdef |
| 83 |
type_cache.cache.append((space.gettypeobject(tpdef), W_TransparentUserCreated)) |
| 84 |
|
| 85 |
def proxy_controller(space, w_object): |
| 86 |
"""get_tproxy_controller(obj) -> controller |
| 87 |
If obj is really a transparent proxy, return its controller. Otherwise return |
| 88 |
None.""" |
| 89 |
if isinstance(w_object, W_Transparent): |
| 90 |
return w_object.w_controller |
| 91 |
|
| 92 |
|
| 93 |
return None |
| 94 |
|
| 95 |
app_proxy = gateway.interp2app(proxy) |
| 96 |
app_proxy_controller = gateway.interp2app(proxy_controller) |