1# mode: run
2# tag: import
3
4"""
5PYTHON setup.py build_ext -i
6PYTHON test_relative_import.py
7"""
8
9######## setup.py ########
10
11from Cython.Build.Dependencies import cythonize
12from distutils.core import setup
13
14setup(
15  ext_modules = cythonize("*/*.pyx"),
16)
17
18
19######## test_relative_import.py ########
20
21from relimport.testmod import test_relative, test_absolute
22a, bmod, afunc, bfunc = test_relative()
23
24try:
25    test_absolute()
26except ImportError:
27    pass
28else:
29    assert False, "absolute import succeeded"
30
31import relimport.a
32import relimport.bmod
33import relimport.testmod
34
35assert relimport.a == a
36assert relimport.bmod == bmod
37assert afunc() == 'a', afunc
38assert bfunc() == 'b', bfunc
39
40
41######## relimport/__init__.py ########
42
43######## relimport/a.pyx ########
44
45def afunc(): return 'a'
46
47
48######## relimport/bmod.pyx ########
49
50def bfunc(): return 'b'
51
52
53######## relimport/testmod.pyx ########
54# cython: language_level=3
55
56from relimport import a as global_a, bmod as global_bmod
57
58from . import *
59
60assert a is global_a, a
61assert bmod is global_bmod, bmod
62
63def test_relative():
64    from . import a, bmod
65    from . import (a, bmod)
66    from . import (a, bmod,)
67    from .a import afunc
68    from .bmod import bfunc
69
70    assert afunc() == 'a', afunc()
71    assert bfunc() == 'b', bfunc()
72
73    return a, bmod, afunc, bfunc
74
75def test_absolute():
76    import bmod
77