File: operators.py

package info (click to toggle)
python-enaml 0.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,284 kB
  • sloc: python: 31,443; cpp: 4,499; makefile: 140; javascript: 68; lisp: 53; sh: 20
file content (348 lines) | stat: -rw-r--r-- 9,743 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#------------------------------------------------------------------------------
# Copyright (c) 2013-2025, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from contextlib import contextmanager
from types import FunctionType

import bytecode as bc

from .code_tracing import inject_tracing, inject_inversion
from .expression_engine import HandlerPair
from .standard_handlers import (
    StandardReadHandler, StandardWriteHandler, StandardTracedReadHandler,
    StandardInvertedWriteHandler
)


def optimize_locals(bytecode):
    """ Optimize the bytecode for fast locals access.

    All STORE_NAME opcodes will be replaced with STORE_FAST. Names
    which are stored and then loaded via LOAD_NAME are rewritten to
    LOAD_FAST and DELETE_NAME is rewritten to DELETE_FAST. This
    transformation is performed in-place.

    Parameters
    ----------
    bytecode : bc.Bytecode
        Bytecode to modify.

    """
    fast_locals = set()
    for instr in bytecode:
        # Filter out SetLineno and Label
        if not isinstance(instr, bc.Instr):
            continue
        if instr.name == "STORE_NAME":
            fast_locals.add(instr.arg)
            instr.name = "STORE_FAST"
    for instr in bytecode:
        # Filter out SetLineno and Label
        if not isinstance(instr, bc.Instr):
            continue
        i_name, i_arg = instr.name, instr.arg
        if i_name == "LOAD_NAME" and i_arg in fast_locals:
            instr.name = "LOAD_FAST"
        elif i_name == "DELETE_NAME" and i_arg in fast_locals:
            instr.name = "DELETE_FAST"
    bytecode.update_flags()


def gen_simple(code, f_globals):
    """ Generate a simple function from a code object.

    Parameters
    ----------
    code : CodeType
        The code object created by the Enaml compiler.

    f_globals : dict
        The global scope for the returned function.

    Returns
    -------
    result : FunctionType
        A new function with optimized local variable access.

    """
    bc_code = bc.Bytecode.from_code(code)
    optimize_locals(bc_code)
    bc_code.flags ^= (bc_code.flags & bc.CompilerFlags.NEWLOCALS)
    new_code = bc_code.to_code()
    return FunctionType(new_code, f_globals)


def gen_tracer(code, f_globals):
    """ Generate a trace function from a code object.

    Parameters
    ----------
    code : CodeType
        The code object created by the Enaml compiler.

    f_globals : dict
        The global scope for the returned function.

    Returns
    -------
    result : FunctionType
        A new function with optimized local variable access
        and instrumentation for invoking a code tracer.

    """
    bc_code = bc.Bytecode.from_code(code)
    optimize_locals(bc_code)
    bc_code = inject_tracing(bc_code)
    bc_code.flags ^= (bc_code.flags & bc.CompilerFlags.NEWLOCALS)
    bc_code.argnames = ['_[tracer]'] + bc_code.argnames
    bc_code.argcount += 1
    new_code = bc_code.to_code()
    return FunctionType(new_code, f_globals)


def gen_inverter(code, f_globals):
    """ Generate an inverter function from a code object.

    Parameters
    ----------
    code : CodeType
        The code object created by the Enaml compiler.

    f_globals : dict
        The global scope for the returned function.

    Returns
    -------
    result : FunctionType
        A new function with optimized local variable access
        and instrumentation for inverting the operation.

    """
    bc_code = bc.Bytecode.from_code(code)
    optimize_locals(bc_code)
    bc_code = inject_inversion(bc_code)
    bc_code.flags ^= (bc_code.flags & bc.CompilerFlags.NEWLOCALS)
    bc_code.argnames = ['_[inverter]', '_[value]'] + bc_code.argnames
    bc_code.argcount += 2
    new_code = bc_code.to_code()
    return FunctionType(new_code, f_globals)


def op_simple(code, scope_key, f_globals):
    """ The default Enaml operator function for the `=` operator.

    This operator generates a simple function with optimized local
    access and hooks it up to a StandardReadHandler. This operator
    does not support write semantics.

    Parameters
    ----------
    code : CodeType
        The code object created by the Enaml compiler.

    scope_key : object
        The block scope key created by the Enaml compiler.

    f_globals : dict
        The global scope for the for code execution.

    Returns
    -------
    result : HandlerPair
        A pair with the reader set to a StandardReadHandler.

    """
    func = gen_simple(code, f_globals)
    reader = StandardReadHandler(func=func, scope_key=scope_key)
    return HandlerPair(reader=reader)


def op_notify(code, scope_key, f_globals):
    """ The default Enaml operator function for the `::` operator.

    This operator generates a simple function with optimized local
    access and hooks it up to a StandardWriteHandler. This operator
    does not support read semantics.

    Parameters
    ----------
    code : CodeType
        The code object created by the Enaml compiler.

    scope_key : object
        The block scope key created by the Enaml compiler.

    f_globals : dict
        The global scope for the for code execution.

    Returns
    -------
    result : HandlerPair
        A pair with the writer set to a StandardWriteHandler.

    """
    func = gen_simple(code, f_globals)
    writer = StandardWriteHandler(func=func, scope_key=scope_key)
    return HandlerPair(writer=writer)


def op_subscribe(code, scope_key, f_globals):
    """ The default Enaml operator function for the `<<` operator.

    This operator generates a tracer function with optimized local
    access and hooks it up to a StandardTracedReadHandler. This
    operator does not support write semantics.

    Parameters
    ----------
    code : CodeType
        The code object created by the Enaml compiler.

    scope_key : object
        The block scope key created by the Enaml compiler.

    f_globals : dict
        The global scope for the for code execution.

    Returns
    -------
    result : HandlerPair
        A pair with the reader set to a StandardTracedReadHandler.

    """
    func = gen_tracer(code, f_globals)
    reader = StandardTracedReadHandler(func=func, scope_key=scope_key)
    return HandlerPair(reader=reader)


def op_update(code, scope_key, f_globals):
    """ The default Enaml operator function for the `>>` operator.

    This operator generates a inverter function with optimized local
    access and hooks it up to a StandardInvertedWriteHandler. This
    operator does not support read semantics.

    Parameters
    ----------
    code : CodeType
        The code object created by the Enaml compiler.

    scope_key : object
        The block scope key created by the Enaml compiler.

    f_globals : dict
        The global scope for the for code execution.

    Returns
    -------
    result : HandlerPair
        A pair with the writer set to a StandardInvertedWriteHandler.

    """
    func = gen_inverter(code, f_globals)
    writer = StandardInvertedWriteHandler(func=func, scope_key=scope_key)
    return HandlerPair(writer=writer)


def op_delegate(code, scope_key, f_globals):
    """ The default Enaml operator function for the `:=` operator.

    This operator combines the '<<' and the '>>' operators into a
    single operator. It supports both read and write semantics.

    Parameters
    ----------
    code : CodeType
        The code object created by the Enaml compiler.

    scope_key : object
        The block scope key created by the Enaml compiler.

    f_globals : dict
        The global scope for the for code execution.

    Returns
    -------
    result : HandlerPair
        A pair with the reader set to a StandardTracedReadHandler and
        the writer set to a StandardInvertedWriteHandler.

    """
    p1 = op_subscribe(code, scope_key, f_globals)
    p2 = op_update(code, scope_key, f_globals)
    return HandlerPair(reader=p1.reader, writer=p2.writer)


DEFAULT_OPERATORS = {
    '=': op_simple,
    '::': op_notify,
    '>>': op_update,
    '<<': op_subscribe,
    ':=': op_delegate,
}


#: The internal stack of operators pushed by the operator context.
__operator_stack = []


@contextmanager
def operator_context(ops, union=False):
    """ Push operators onto the stack for the duration of the context.

    Parameters
    ----------
    ops : dict
        The dictionary of operators to push onto the stack.

    union : bool, optional
        Whether or to union the operators with the existing operators
        on the top of the stack. The default is False.

    """
    if union:
        new = dict(__get_operators())
        new.update(ops)
        ops = new
    __operator_stack.append(ops)
    yield
    __operator_stack.pop()


def __get_default_operators():
    """ Set the default operators.

    This function is for internal use only and may disappear at any time.

    """
    return DEFAULT_OPERATORS


def __set_default_operators(ops):
    """ Set the default operators.

    This function is for internal use only and may disappear at any time.

    """
    global DEFAULT_OPERATORS
    DEFAULT_OPERATORS = ops


def __get_operators():
    """ An internal routine used to get the operators for a given class.

    Operators resolution is performed in the following order:

        - The operators on the top of the operators stack.
        - The default operators via __get_default_operators()

    This function may disappear at any time.

    """
    if __operator_stack:
        return __operator_stack[-1]
    return __get_default_operators()