Debian Codegraph @

pypy/doc/objspace-proxies.rst

raw
1 .. _tproxy:
2
3 Transparent Proxies (DEPRECATED)
4 --------------------------------
5
6 .. warning::
7
8 This is a feature that was tried experimentally long ago, and we
9 found no really good use cases. The basic functionality is still
10 there, but we don't recommend using it. Some of the examples below
11 might not work any more (e.g. you can't tproxy a list object any
12 more). The rest can be done by hacking in standard Python. If
13 anyone is interested in working on tproxy again, he is welcome, but
14 we don't regard this as an interesting extension.
15
16 PyPy's Transparent Proxies allow routing of operations on objects
17 to a callable. Application-level code can customize objects without
18 interfering with the type system - ``type(proxied_list) is list`` holds true
19 when :py:obj:`proxied_list` is a proxied built-in :py:class:`list` - while
20 giving you full control on all operations that are performed on the
21 :py:obj:`proxied_list`.
22
23 See [D12.1]_ for more context, motivation and usage of transparent proxies.
24
25
26 Example of the core mechanism
27 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28
29 The following example proxies a list and will return ``42`` on any addition
30 operations::
31
32 $ py.py --objspace-std-withtproxy
33 >>>> from __pypy__ import tproxy
34 >>>> def f(operation, *args, **kwargs):
35 >>>> if operation == '__add__':
36 >>>> return 42
37 >>>> raise AttributeError
38 >>>>
39 >>>> i = tproxy(list, f)
40 >>>> type(i)
41 list
42 >>>> i + 3
43 42
44
45
46 Example of recording all operations on builtins
47 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48
49 Suppose we want to have a list which stores all operations performed on
50 it for later analysis. We can use the :source:`lib_pypy/tputil.py` module to help
51 with transparently proxying builtin instances::
52
53 from tputil import make_proxy
54
55 history = []
56 def recorder(operation):
57 history.append(operation)
58 return operation.delegate()
59
60 >>>> l = make_proxy(recorder, obj=[])
61 >>>> type(l)
62 list
63 >>>> l.append(3)
64 >>>> len(l)
65 1
66 >>>> len(history)
67 2
68
69 ``make_proxy(recorder, obj=[])`` creates a transparent list
70 proxy that allows us to delegate operations to the :py:func:`recorder` function.
71 Calling ``type(l)`` does not lead to any operation being executed at all.
72
73 Note that :py:meth:`append` shows up as :py:meth:`__getattribute__` and that
74 ``type(l)`` does not show up at all - the type is the only aspect of the instance
75 which the proxy controller cannot change.
76
77
78 .. _transparent proxy builtins:
79
80 Transparent Proxy PyPy builtins and support
81 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
82
83 If you are using the `--objspace-std-withtproxy`_ option
84 the :doc:`__pypy__ <__pypy__-module>` module provides the following builtins:
85
86 .. py:function:: tproxy(type, controller)
87
88 Returns a proxy object representing the given type and forwarding all
89 operations on this type to the controller. On each operation,
90 ``controller(opname, *args, **kwargs)`` will be called.
91
92 .. py:function:: get_tproxy_controller(obj)
93
94 Returns the responsible controller for a given object. For non-proxied
95 objects :py:const:`None` is returned.
96
97 .. _--objspace-std-withtproxy: config/objspace.std.withtproxy.html
98
99
100 .. _tputil:
101
102 tputil helper module
103 ~~~~~~~~~~~~~~~~~~~~
104
105 The :source:`lib_pypy/tputil.py` module provides:
106
107 .. py:function:: make_proxy(controller, type, obj)
108
109 Creates a transparent proxy controlled by the given :py:obj:`controller`
110 callable. The proxy will appear as a completely regular instance of the given
111 type, but all operations on it are sent to the specified controller - which
112 receives a :py:class:`ProxyOperation` instance on each operation. If :py:obj:`type`
113 is not specified, it defaults to ``type(obj)`` if :py:obj:`obj` is specified.
114
115 ProxyOperation instances have the following attributes:
116
117 .. py:attribute:: proxyobj
118
119 The transparent proxy object of this operation.
120
121 .. py:attribute:: opname
122
123 The name of this operation.
124
125 .. py:attribute:: args
126
127 Any positional arguments for this operation.
128
129 .. py:attribute:: kwargs
130
131 Any keyword arguments for this operation.
132
133 .. py:attribute:: obj
134
135 (Only if provided to :py:func:`make_proxy`)
136
137 A concrete object.
138
139 .. py:method:: delegate
140
141 If a concrete object instance :py:obj:`obj` was specified in the call to
142 :py:func:`make_proxy`, then :py:meth:`proxyoperation.delegate` can be called
143 to delegate the operation to the object instance.
144
145
146 Further points of interest
147 ~~~~~~~~~~~~~~~~~~~~~~~~~~
148
149 A lot of tasks could be performed using transparent proxies, including,
150 but not limited to:
151
152 * Remote versions of objects, on which we can directly perform operations
153 (think about transparent distribution)
154
155 * Access to persistent storage such as a database (imagine an
156 SQL object mapper which looks like any other object).
157
158 * Access to external data structures, such as other languages, as normal
159 objects (of course some operations could raise exceptions, but
160 since operations are executed at the application level, that is not a major
161 problem)
162
163
164 Implementation Notes
165 ~~~~~~~~~~~~~~~~~~~~
166
167 PyPy's standard object space allows us to internally have multiple
168 implementations of a type and change the implementation at run-time, while
169 application-level code consistently sees the exact same type and object.
170 Multiple performance optimizations using these features have already been
171 implemented: :doc:`alternative object implementations <interpreter-optimizations>`.
172 Transparent Proxies use this architecture to provide control back to
173 application-level code.
174
175 Transparent proxies are implemented on top of the :ref:`standard object
176 space <standard-object-space>`, in :source:`pypy/objspace/std/proxyobject.py`,
177 :source:`pypy/objspace/std/proxyobject.py` and :source:`pypy/objspace/std/transparent.py`.
178 To use them you will need to pass a `--objspace-std-withtproxy`_ option to ``pypy``
179 or ``translate.py``. This registers implementations named :py:class:`W_TransparentXxx`
180 - which usually correspond to an appropriate :py:class:`W_XxxObject` - and
181 includes some interpreter hacks for objects that are too close to the interpreter
182 to be implemented in the standard object space. The types of objects that can
183 be proxied this way are user created classes & functions,
184 lists, dicts, exceptions, tracebacks and frames.
185
186 .. [D12.1] `High-Level Backends and Interpreter Feature Prototypes`, PyPy
187 EU-Report, 2007, https://foss.heptapod.net/pypy/extradoc/-/tree/branch/extradoc/eu-report/D12.1_H-L-Backends_and_Feature_Prototypes-2007-03-22.pdf