/
operatorimpl.py
45 lines (33 loc) · 1.25 KB
/
operatorimpl.py
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
"""
Definition of implementations for the `operator` module.
"""
import operator
from numba.targets.imputils import Registry
from numba.targets import builtins
from numba import types, utils, typing
registry = Registry()
lower = registry.lower
# Redirect the implementation of operator module functions to the
# the corresponding built-in operators.
def map_operator(name, inplace_name, op):
op_func = getattr(operator, name)
reverse_args = (op == 'in')
@lower(op_func, types.VarArg(types.Any))
def binop_impl(context, builder, sig, args):
if reverse_args:
args = args[::-1]
sig = typing.signature(sig.return_type, *sig.args[::-1])
impl = context.get_function(op, sig)
return impl(builder, args)
if inplace_name:
op_func = getattr(operator, inplace_name)
@lower(op_func, types.VarArg(types.Any))
def binop_inplace_impl(context, builder, sig, args):
first = sig.args[0]
if first.mutable:
impl = context.get_function(op + '=', sig)
else:
impl = context.get_function(op, sig)
return impl(builder, args)
for name, inplace_name, op in utils.operator_map:
map_operator(name, inplace_name, op)