1import numpy as np
2
3from numba.core.typing.typeof import typeof
4
5
6def pndindex(*args):
7    """ Provides an n-dimensional parallel iterator that generates index tuples
8    for each iteration point. Sequentially, pndindex is identical to np.ndindex.
9    """
10    return np.ndindex(*args)
11
12
13class prange(object):
14    """ Provides a 1D parallel iterator that generates a sequence of integers.
15    In non-parallel contexts, prange is identical to range.
16    """
17    def __new__(cls, *args):
18        return range(*args)
19
20
21def _gdb_python_call_gen(func_name, *args):
22    # generates a call to a function containing a compiled in gdb command,
23    # this is to make `numba.gdb*` work in the interpreter.
24    import numba
25    fn = getattr(numba, func_name)
26    argstr = ','.join(['"%s"' for _ in args]) % args
27    defn = """def _gdb_func_injection():\n\t%s(%s)\n
28    """ % (func_name, argstr)
29    l = {}
30    exec(defn, {func_name: fn}, l)
31    return numba.njit(l['_gdb_func_injection'])
32
33
34def gdb(*args):
35    """
36    Calling this function will invoke gdb and attach it to the current process
37    at the call site. Arguments are strings in the gdb command language syntax
38    which will be executed by gdb once initialisation has occurred.
39    """
40    _gdb_python_call_gen('gdb', *args)()
41
42
43def gdb_breakpoint():
44    """
45    Calling this function will inject a breakpoint at the call site that is
46    recognised by both `gdb` and `gdb_init`, this is to allow breaking at
47    multiple points. gdb will stop in the user defined code just after the frame
48    employed by the breakpoint returns.
49    """
50    _gdb_python_call_gen('gdb_breakpoint')()
51
52
53def gdb_init(*args):
54    """
55    Calling this function will invoke gdb and attach it to the current process
56    at the call site, then continue executing the process under gdb's control.
57    Arguments are strings in the gdb command language syntax which will be
58    executed by gdb once initialisation has occurred.
59    """
60    _gdb_python_call_gen('gdb_init', *args)()
61
62
63def literally(obj):
64    """Forces Numba to interpret *obj* as an Literal value.
65
66    *obj* must be either a literal or an argument of the caller function, where
67    the argument must be bound to a literal. The literal requirement
68    propagates up the call stack.
69
70    This function is intercepted by the compiler to alter the compilation
71    behavior to wrap the corresponding function parameters as ``Literal``.
72    It has **no effect** outside of nopython-mode (interpreter, and objectmode).
73
74    The current implementation detects literal arguments in two ways:
75
76    1. Scans for uses of ``literally`` via a compiler pass.
77    2. ``literally`` is overloaded to raise ``numba.errors.ForceLiteralArg``
78       to signal the dispatcher to treat the corresponding parameter
79       differently. This mode is to support indirect use (via a function call).
80
81    The execution semantic of this function is equivalent to an identity
82    function.
83
84    See :ghfile:`numba/tests/test_literal_dispatch.py` for examples.
85    """
86    return obj
87
88
89def literal_unroll(container):
90    return container
91
92
93__all__ = [
94    'typeof',
95    'prange',
96    'pndindex',
97    'gdb',
98    'gdb_breakpoint',
99    'gdb_init',
100    'literally',
101    'literal_unroll',
102]
103