blob: e4a02391af0e6ac349a1f624654d788ad7819ffa [file] [log] [blame]
Georg Brandlb533e262008-05-25 18:19:30 +00001import sys
2import os
Tarek Ziadéd7b5f662009-02-13 16:23:57 +00003import tempfile
Georg Brandlb533e262008-05-25 18:19:30 +00004import shutil
5from io import StringIO
6
7from distutils.core import Extension, Distribution
8from distutils.command.build_ext import build_ext
9from distutils import sysconfig
Tarek Ziadéc1375d52009-02-14 14:35:51 +000010from distutils.tests.support import TempdirManager
Tarek Ziadéb2e36f12009-03-31 22:37:55 +000011from distutils.tests.support import LoggingSilencer
12from distutils.extension import Extension
13from distutils.errors import UnknownFileError
Tarek Ziadé30911292009-03-31 22:50:54 +000014from distutils.errors import CompileError
Georg Brandlb533e262008-05-25 18:19:30 +000015
16import unittest
17from test import support
18
Christian Heimes3e7e0692008-11-25 21:21:32 +000019# http://bugs.python.org/issue4373
20# Don't load the xx module more than once.
21ALREADY_TESTED = False
22
Neil Schemenauerd8f63bb2009-02-06 21:42:05 +000023def _get_source_filename():
24 srcdir = sysconfig.get_config_var('srcdir')
25 return os.path.join(srcdir, 'Modules', 'xxmodule.c')
26
Tarek Ziadéb2e36f12009-03-31 22:37:55 +000027class BuildExtTestCase(TempdirManager,
28 LoggingSilencer,
29 unittest.TestCase):
Georg Brandlb533e262008-05-25 18:19:30 +000030 def setUp(self):
31 # Create a simple test environment
32 # Note that we're making changes to sys.path
Tarek Ziadé38e3d512009-02-27 12:58:56 +000033 super(BuildExtTestCase, self).setUp()
Tarek Ziadéc1375d52009-02-14 14:35:51 +000034 self.tmp_dir = self.mkdtemp()
Georg Brandlb533e262008-05-25 18:19:30 +000035 self.sys_path = sys.path[:]
36 sys.path.append(self.tmp_dir)
Neil Schemenauerd8f63bb2009-02-06 21:42:05 +000037 shutil.copy(_get_source_filename(), self.tmp_dir)
Tarek Ziadé38e3d512009-02-27 12:58:56 +000038 if sys.version > "2.6":
39 import site
40 self.old_user_base = site.USER_BASE
41 site.USER_BASE = self.mkdtemp()
42 from distutils.command import build_ext
43 build_ext.USER_BASE = site.USER_BASE
Georg Brandlb533e262008-05-25 18:19:30 +000044
45 def test_build_ext(self):
Christian Heimes3e7e0692008-11-25 21:21:32 +000046 global ALREADY_TESTED
Georg Brandlb533e262008-05-25 18:19:30 +000047 xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
48 xx_ext = Extension('xx', [xx_c])
49 dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]})
50 dist.package_dir = self.tmp_dir
51 cmd = build_ext(dist)
Thomas Heller84b7f0c2008-05-26 11:51:44 +000052 if os.name == "nt":
53 # On Windows, we must build a debug version iff running
54 # a debug build of Python
55 cmd.debug = sys.executable.endswith("_d.exe")
Georg Brandlb533e262008-05-25 18:19:30 +000056 cmd.build_lib = self.tmp_dir
57 cmd.build_temp = self.tmp_dir
58
59 old_stdout = sys.stdout
60 if not support.verbose:
61 # silence compiler output
62 sys.stdout = StringIO()
63 try:
64 cmd.ensure_finalized()
65 cmd.run()
66 finally:
67 sys.stdout = old_stdout
68
Christian Heimes3e7e0692008-11-25 21:21:32 +000069 if ALREADY_TESTED:
70 return
71 else:
72 ALREADY_TESTED = True
73
Georg Brandlb533e262008-05-25 18:19:30 +000074 import xx
75
76 for attr in ('error', 'foo', 'new', 'roj'):
77 self.assert_(hasattr(xx, attr))
78
79 self.assertEquals(xx.foo(2, 5), 7)
80 self.assertEquals(xx.foo(13,15), 28)
81 self.assertEquals(xx.new().demo(), None)
82 doc = 'This is a template module just for instruction.'
83 self.assertEquals(xx.__doc__, doc)
84 self.assert_(isinstance(xx.Null(), xx.Null))
85 self.assert_(isinstance(xx.Str(), xx.Str))
86
87 def tearDown(self):
88 # Get everything back to normal
89 support.unload('xx')
90 sys.path = self.sys_path
Tarek Ziadé38e3d512009-02-27 12:58:56 +000091 if sys.version > "2.6":
92 import site
93 site.USER_BASE = self.old_user_base
94 from distutils.command import build_ext
95 build_ext.USER_BASE = self.old_user_base
96 super(BuildExtTestCase, self).tearDown()
Georg Brandlb533e262008-05-25 18:19:30 +000097
Tarek Ziadé5874ef12009-02-05 22:56:14 +000098 def test_solaris_enable_shared(self):
99 dist = Distribution({'name': 'xx'})
100 cmd = build_ext(dist)
101 old = sys.platform
102
103 sys.platform = 'sunos' # fooling finalize_options
104 from distutils.sysconfig import _config_vars
105 old_var = _config_vars.get('Py_ENABLE_SHARED')
106 _config_vars['Py_ENABLE_SHARED'] = 1
107 try:
108 cmd.ensure_finalized()
109 finally:
110 sys.platform = old
111 if old_var is None:
112 del _config_vars['Py_ENABLE_SHARED']
113 else:
114 _config_vars['Py_ENABLE_SHARED'] = old_var
115
116 # make sur we get some lobrary dirs under solaris
117 self.assert_(len(cmd.library_dirs) > 0)
118
Tarek Ziadé38e3d512009-02-27 12:58:56 +0000119 def test_user_site(self):
120 # site.USER_SITE was introduced in 2.6
121 if sys.version < '2.6':
122 return
123
124 import site
125 dist = Distribution({'name': 'xx'})
126 cmd = build_ext(dist)
127
Tarek Ziadébe720e02009-05-09 11:55:12 +0000128 # making sure the user option is there
Tarek Ziadé38e3d512009-02-27 12:58:56 +0000129 options = [name for name, short, lable in
130 cmd.user_options]
131 self.assert_('user' in options)
132
133 # setting a value
134 cmd.user = 1
135
136 # setting user based lib and include
137 lib = os.path.join(site.USER_BASE, 'lib')
138 incl = os.path.join(site.USER_BASE, 'include')
139 os.mkdir(lib)
140 os.mkdir(incl)
141
142 # let's run finalize
143 cmd.ensure_finalized()
144
145 # see if include_dirs and library_dirs
146 # were set
147 self.assert_(lib in cmd.library_dirs)
Tarek Ziadébe720e02009-05-09 11:55:12 +0000148 self.assert_(lib in cmd.rpath)
Tarek Ziadé38e3d512009-02-27 12:58:56 +0000149 self.assert_(incl in cmd.include_dirs)
150
Tarek Ziadéb2e36f12009-03-31 22:37:55 +0000151 def test_optional_extension(self):
152
153 # this extension will fail, but let's ignore this failure
154 # with the optional argument.
155 modules = [Extension('foo', ['xxx'], optional=False)]
156 dist = Distribution({'name': 'xx', 'ext_modules': modules})
157 cmd = build_ext(dist)
158 cmd.ensure_finalized()
Tarek Ziadé30911292009-03-31 22:50:54 +0000159 self.assertRaises((UnknownFileError, CompileError),
160 cmd.run) # should raise an error
Tarek Ziadéb2e36f12009-03-31 22:37:55 +0000161
162 modules = [Extension('foo', ['xxx'], optional=True)]
163 dist = Distribution({'name': 'xx', 'ext_modules': modules})
164 cmd = build_ext(dist)
165 cmd.ensure_finalized()
166 cmd.run() # should pass
167
Georg Brandlb533e262008-05-25 18:19:30 +0000168def test_suite():
Neil Schemenauerd8f63bb2009-02-06 21:42:05 +0000169 src = _get_source_filename()
170 if not os.path.exists(src):
Georg Brandlb533e262008-05-25 18:19:30 +0000171 if support.verbose:
Neil Schemenauerd8f63bb2009-02-06 21:42:05 +0000172 print('test_build_ext: Cannot find source code (test'
173 ' must run in python build dir)')
Georg Brandlb533e262008-05-25 18:19:30 +0000174 return unittest.TestSuite()
175 else: return unittest.makeSuite(BuildExtTestCase)
176
177if __name__ == '__main__':
178 support.run_unittest(test_suite())