1# mode: run
2# tag: typeinference
3
4cimport cython
5
6
7def test_outer_inner_double():
8    """
9    >>> print(test_outer_inner_double())
10    double
11    """
12    x = 1.0
13    def inner():
14        nonlocal x
15        x = 2.0
16    inner()
17    assert x == 2.0, str(x)
18    return cython.typeof(x)
19
20
21def test_outer_inner_double_int():
22    """
23    >>> print(test_outer_inner_double_int())
24    ('double', 'double')
25    """
26    x = 1.0
27    y = 2
28    def inner():
29        nonlocal x, y
30        x = 1
31        y = 2.0
32    inner()
33    return cython.typeof(x), cython.typeof(y)
34
35
36def test_outer_inner_pyarg():
37    """
38    >>> print(test_outer_inner_pyarg())
39    2
40    long
41    """
42    x = 1
43    def inner(y):
44        return x + y
45    print inner(1)
46    return cython.typeof(x)
47
48
49def test_outer_inner_carg():
50    """
51    >>> print(test_outer_inner_carg())
52    2.0
53    long
54    """
55    x = 1
56    def inner(double y):
57        return x + y
58    print inner(1)
59    return cython.typeof(x)
60
61
62def test_outer_inner_incompatible():
63    """
64    >>> print(test_outer_inner_incompatible())
65    Python object
66    """
67    x = 1.0
68    def inner():
69        nonlocal x
70        x = 'test'
71    inner()
72    return cython.typeof(x)
73
74
75def test_outer_inner_ptr():
76    """
77    >>> print(test_outer_inner_ptr())
78    double *
79    """
80    x = 1.0
81    xptr_outer = &x
82    def inner():
83        nonlocal x
84        x = 1
85        xptr_inner = &x
86        assert cython.typeof(xptr_inner) == cython.typeof(xptr_outer), (
87            '%s != %s' % (cython.typeof(xptr_inner), cython.typeof(xptr_outer)))
88    inner()
89    return cython.typeof(xptr_outer)
90
91
92def test_outer_inner2_double():
93    """
94    >>> print(test_outer_inner2_double())
95    double
96    """
97    x = 1.0
98    def inner1():
99        nonlocal x
100        x = 2
101    def inner2():
102        nonlocal x
103        x = 3.0
104    inner1()
105    inner2()
106    return cython.typeof(x)
107