File: closure_inlining.pyx

package info (click to toggle)
cython 3.0.11%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 19,092 kB
  • sloc: python: 83,539; ansic: 18,831; cpp: 1,402; xml: 1,031; javascript: 511; makefile: 403; sh: 204; sed: 11
file content (176 lines) | stat: -rw-r--r-- 3,941 bytes parent folder | download | duplicates (8)
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
# cython: optimize.inline_defnode_calls=True
# mode: run
cimport cython

@cython.test_fail_if_path_exists('//SimpleCallNode')
@cython.test_assert_path_exists('//InlinedDefNodeCallNode')
def simple_noargs():
    """
    >>> simple_noargs()
    123
    """
    def inner():
        return 123
    return inner()


@cython.test_fail_if_path_exists('//SimpleCallNode')
@cython.test_assert_path_exists('//InlinedDefNodeCallNode')
def test_coerce(a, int b):
    """
    >>> test_coerce(2, 2)
    4
    """
    def inner(int a, b):
        return a * b
    return inner(a, b)


cdef class Foo(object):
    def __repr__(self):
        return '<Foo>'


@cython.test_fail_if_path_exists('//SimpleCallNode')
@cython.test_assert_path_exists('//InlinedDefNodeCallNode')
def test_func_signature(a):
    """
    >>> test_func_signature(Foo())
    <Foo>
    >>> test_func_signature(123)
    Traceback (most recent call last):
    TypeError: Cannot convert int to closure_inlining.Foo
    """

    def inner(Foo a):
        return a
    return inner(a)

@cython.test_fail_if_path_exists('//SimpleCallNode')
@cython.test_assert_path_exists('//InlinedDefNodeCallNode')
def test_func_signature2(a, b):
    """
    >>> test_func_signature2(Foo(), 123)
    (<Foo>, 123)
    >>> test_func_signature2(321, 123)
    Traceback (most recent call last):
    TypeError: Cannot convert int to closure_inlining.Foo
    """

    def inner(Foo a, b):
        return a, b
    return inner(a, b)

# Starred args and default values are not yet supported for inlining
@cython.test_assert_path_exists('//SimpleCallNode')
def test_defaults(a, b):
    """
    >>> test_defaults(1, 2)
    (1, 2, 123)
    """
    def inner(a, b=b, c=123):
        return a, b, c
    return inner(a)

@cython.test_assert_path_exists('//SimpleCallNode')
def test_kwonly_args(a, b):
    """
    >>> test_kwonly_args(1, 2)
    (1, 2, 123)
    """
    def inner(a, b=b, *, c=123):
        return a, b, c
    return inner(a)

@cython.test_assert_path_exists('//SimpleCallNode')
def test_kwonly_args_missing(a, b):
    """
    >>> test_kwonly_args_missing(1, 2)
    Traceback (most recent call last):
    TypeError: inner() needs keyword-only argument c
    """
    def inner(a, b=b, *, c):
        return a, b, c
    return inner(a)

@cython.test_assert_path_exists('//SimpleCallNode')
def test_starred(a):
    """
    >>> test_starred(123)
    (123, (), {})
    """
    def inner(a, *args, **kwargs):
        return a, args, kwargs
    return inner(a)


def test_global_calls_still_work():
    """
    >>> global_call_result
    123
    """
    return 123

global_call_result = test_global_calls_still_work()


@cython.test_fail_if_path_exists(
    '//InlinedDefNodeCallNode//SimpleCallNode')
@cython.test_assert_path_exists(
    '//InlinedDefNodeCallNode',
    '//InlinedDefNodeCallNode[@function_name.name = "call"]',
    '//InlinedDefNodeCallNode//InlinedDefNodeCallNode')
def test_sideeffect_call_order():
    """
    >>> test_sideeffect_call_order()
    [2, 4, 5]
    """
    L = []
    def sideeffect(x):
        L.append(x)
        return x
    def call(x1, x2, x3, x4, x5):
        pass
    call(1, sideeffect(2), 3, sideeffect(4), sideeffect(5))
    return L


def test_redef(redefine):
    """
    >>> test_redef(False)
    1
    >>> test_redef(True)
    2
    """
    def inner():
        return 1
    def inner2():
        return 2
    def redef():
        nonlocal inner
        inner = inner2
    if redefine:
        redef()
        assert inner == inner2
    else:
        assert inner != inner2
    return inner()


def test_with_statement():
    """
    >>> test_with_statement()
    enter
    running
    exit
    """
    def make_context_manager():
        class CM(object):
            def __enter__(self):
                print "enter"
            def __exit__(self, *args):
                print "exit"
        return CM()

    with make_context_manager():
        print "running"