1# mode: run
2# tag: builtins, locals, dir
3
4def get_locals(x, *args, **kwds):
5    """
6    >>> sorted( get_locals(1,2,3, k=5).items() )
7    [('args', (2, 3)), ('kwds', {'k': 5}), ('x', 1), ('y', 'hi'), ('z', 5)]
8    """
9    cdef int z = 5
10    y = "hi"
11    return locals()
12
13def get_vars(x, *args, **kwds):
14    """
15    >>> sorted( get_vars(1,2,3, k=5).items() )
16    [('args', (2, 3)), ('kwds', {'k': 5}), ('x', 1), ('y', 'hi'), ('z', 5)]
17    """
18    cdef int z = 5
19    y = "hi"
20    return vars()
21
22def get_dir(x, *args, **kwds):
23    """
24    >>> sorted( get_dir(1,2,3, k=5) )
25    ['args', 'kwds', 'x', 'y', 'z']
26    """
27    cdef int z = 5
28    y = "hi"
29    return dir()
30
31def in_locals(x, *args, **kwds):
32    """
33    >>> in_locals('z')
34    True
35    >>> in_locals('args')
36    True
37    >>> in_locals('X')
38    False
39    """
40    cdef int z = 5
41    y = "hi"
42    return x in locals()
43
44def in_dir(x, *args, **kwds):
45    """
46    >>> in_dir('z')
47    True
48    >>> in_dir('args')
49    True
50    >>> in_dir('X')
51    False
52    """
53    cdef int z = 5
54    y = "hi"
55    return x in dir()
56
57def in_vars(x, *args, **kwds):
58    """
59    >>> in_vars('z')
60    True
61    >>> in_vars('args')
62    True
63    >>> in_vars('X')
64    False
65    """
66    cdef int z = 5
67    y = "hi"
68    return x in vars()
69
70def sorted(it):
71    l = list(it)
72    l.sort()
73    return l
74
75def locals_ctype():
76    """
77    >>> locals_ctype()
78    False
79    """
80    cdef int *p = NULL
81    return 'p' in locals()
82
83def locals_ctype_inferred():
84    """
85    >>> locals_ctype_inferred()
86    False
87    """
88    cdef int *p = NULL
89    b = p
90    return 'b' in locals()
91
92
93def pass_on_locals(f):
94    """
95    >>> def print_locals(l, **kwargs):
96    ...     print(sorted(l))
97
98    >>> pass_on_locals(print_locals)
99    ['f']
100    ['f']
101    ['f']
102    """
103    f(locals())
104    f(l=locals())
105    f(l=locals(), a=1)
106
107
108def buffers_in_locals(object[char, ndim=1] a):
109    """
110    >>> sorted(buffers_in_locals(b'abcdefg'))
111    ['a', 'b']
112    """
113    cdef object[unsigned char, ndim=1] b = a
114
115    return locals()
116