4
3
from io import StringIO
7
6
from distutils.core import Distribution
8
7
from distutils.command.build_ext import build_ext
9
8
from distutils import sysconfig
10
from distutils.tests.support import TempdirManager
11
from distutils.tests.support import LoggingSilencer
9
from distutils.tests.support import (TempdirManager, LoggingSilencer,
10
copy_xxmodule_c, fixup_build_ext)
12
11
from distutils.extension import Extension
13
12
from distutils.errors import (
14
13
CompileError, DistutilsPlatformError, DistutilsSetupError,
18
17
from test import support
19
from test.support import run_unittest
21
19
# http://bugs.python.org/issue4373
22
20
# Don't load the xx module more than once.
23
21
ALREADY_TESTED = False
25
def _get_source_filename():
26
# use installed copy if available
27
tests_f = os.path.join(os.path.dirname(__file__), 'xxmodule.c')
28
if os.path.exists(tests_f):
30
# otherwise try using copy from build directory
31
srcdir = sysconfig.get_config_var('srcdir')
32
return os.path.join(srcdir, 'Modules', 'xxmodule.c')
34
24
class BuildExtTestCase(TempdirManager,
41
31
self.tmp_dir = self.mkdtemp()
42
32
self.sys_path = sys.path, sys.path[:]
43
33
sys.path.append(self.tmp_dir)
44
filename = _get_source_filename()
45
if os.path.exists(filename):
46
shutil.copy(filename, self.tmp_dir)
47
34
if sys.version > "2.6":
49
36
self.old_user_base = site.USER_BASE
51
38
from distutils.command import build_ext
52
39
build_ext.USER_BASE = site.USER_BASE
54
def _fixup_command(self, cmd):
55
# When Python was build with --enable-shared, -L. is not good enough
56
# to find the libpython<blah>.so. This is because regrtest runs it
57
# under a tempdir, not in the top level where the .so lives. By the
58
# time we've gotten here, Python's already been chdir'd to the
61
# To further add to the fun, we can't just add library_dirs to the
62
# Extension() instance because that doesn't get plumbed through to the
63
# final compiler command.
64
if (sysconfig.get_config_var('Py_ENABLE_SHARED') and
65
not sys.platform.startswith('win')):
66
runshared = sysconfig.get_config_var('RUNSHARED')
68
cmd.library_dirs = ['.']
70
name, equals, value = runshared.partition('=')
71
cmd.library_dirs = value.split(os.pathsep)
73
41
def test_build_ext(self):
74
42
global ALREADY_TESTED
43
copy_xxmodule_c(self.tmp_dir)
75
44
xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
76
if not os.path.exists(xx_c):
78
45
xx_ext = Extension('xx', [xx_c])
79
46
dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]})
80
47
dist.package_dir = self.tmp_dir
81
48
cmd = build_ext(dist)
82
self._fixup_command(cmd)
84
# On Windows, we must build a debug version iff running
85
# a debug build of Python
86
cmd.debug = sys.executable.endswith("_d.exe")
87
50
cmd.build_lib = self.tmp_dir
88
51
cmd.build_temp = self.tmp_dir
177
140
# see if include_dirs and library_dirs
179
self.assertTrue(lib in cmd.library_dirs)
180
self.assertTrue(lib in cmd.rpath)
181
self.assertTrue(incl in cmd.include_dirs)
142
self.assertIn(lib, cmd.library_dirs)
143
self.assertIn(lib, cmd.rpath)
144
self.assertIn(incl, cmd.include_dirs)
183
146
def test_optional_extension(self):
334
297
dist = Distribution({'name': 'xx',
335
298
'ext_modules': [ext]})
336
299
cmd = build_ext(dist)
337
self._fixup_command(cmd)
338
301
cmd.ensure_finalized()
339
302
self.assertEqual(len(cmd.get_outputs()), 1)
342
cmd.debug = sys.executable.endswith("_d.exe")
344
304
cmd.build_lib = os.path.join(self.tmp_dir, 'build')
345
305
cmd.build_temp = os.path.join(self.tmp_dir, 'tempt')
520
480
def test_suite():
521
src = _get_source_filename()
522
if not os.path.exists(src):
524
print('test_build_ext: Cannot find source code (test'
525
' must run in python build dir)')
526
return unittest.TestSuite()
527
else: return unittest.makeSuite(BuildExtTestCase)
481
return unittest.makeSuite(BuildExtTestCase)
529
483
if __name__ == '__main__':
530
484
support.run_unittest(test_suite())