5
from io import StringIO
7
from distutils.core import Extension, Distribution
8
from distutils.command.build_ext import build_ext
9
from distutils import sysconfig
10
from distutils.tests.support import TempdirManager
13
from test import support
15
# http://bugs.python.org/issue4373
16
# Don't load the xx module more than once.
17
ALREADY_TESTED = False
19
def _get_source_filename():
20
srcdir = sysconfig.get_config_var('srcdir')
21
return os.path.join(srcdir, 'Modules', 'xxmodule.c')
23
class BuildExtTestCase(TempdirManager, unittest.TestCase):
25
# Create a simple test environment
26
# Note that we're making changes to sys.path
27
super(BuildExtTestCase, self).setUp()
28
self.tmp_dir = self.mkdtemp()
29
self.sys_path = sys.path[:]
30
sys.path.append(self.tmp_dir)
31
shutil.copy(_get_source_filename(), self.tmp_dir)
32
if sys.version > "2.6":
34
self.old_user_base = site.USER_BASE
35
site.USER_BASE = self.mkdtemp()
36
from distutils.command import build_ext
37
build_ext.USER_BASE = site.USER_BASE
39
def test_build_ext(self):
41
xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
42
xx_ext = Extension('xx', [xx_c])
43
dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]})
44
dist.package_dir = self.tmp_dir
47
# On Windows, we must build a debug version iff running
48
# a debug build of Python
49
cmd.debug = sys.executable.endswith("_d.exe")
50
cmd.build_lib = self.tmp_dir
51
cmd.build_temp = self.tmp_dir
53
old_stdout = sys.stdout
54
if not support.verbose:
55
# silence compiler output
56
sys.stdout = StringIO()
58
cmd.ensure_finalized()
61
sys.stdout = old_stdout
70
for attr in ('error', 'foo', 'new', 'roj'):
71
self.assert_(hasattr(xx, attr))
73
self.assertEquals(xx.foo(2, 5), 7)
74
self.assertEquals(xx.foo(13,15), 28)
75
self.assertEquals(xx.new().demo(), None)
76
doc = 'This is a template module just for instruction.'
77
self.assertEquals(xx.__doc__, doc)
78
self.assert_(isinstance(xx.Null(), xx.Null))
79
self.assert_(isinstance(xx.Str(), xx.Str))
82
# Get everything back to normal
84
sys.path = self.sys_path
85
if sys.version > "2.6":
87
site.USER_BASE = self.old_user_base
88
from distutils.command import build_ext
89
build_ext.USER_BASE = self.old_user_base
90
super(BuildExtTestCase, self).tearDown()
92
def test_solaris_enable_shared(self):
93
dist = Distribution({'name': 'xx'})
97
sys.platform = 'sunos' # fooling finalize_options
98
from distutils.sysconfig import _config_vars
99
old_var = _config_vars.get('Py_ENABLE_SHARED')
100
_config_vars['Py_ENABLE_SHARED'] = 1
102
cmd.ensure_finalized()
106
del _config_vars['Py_ENABLE_SHARED']
108
_config_vars['Py_ENABLE_SHARED'] = old_var
110
# make sur we get some lobrary dirs under solaris
111
self.assert_(len(cmd.library_dirs) > 0)
113
def test_user_site(self):
114
# site.USER_SITE was introduced in 2.6
115
if sys.version < '2.6':
119
dist = Distribution({'name': 'xx'})
120
cmd = build_ext(dist)
122
# making sure the suer option is there
123
options = [name for name, short, lable in
125
self.assert_('user' in options)
130
# setting user based lib and include
131
lib = os.path.join(site.USER_BASE, 'lib')
132
incl = os.path.join(site.USER_BASE, 'include')
137
cmd.ensure_finalized()
139
# see if include_dirs and library_dirs
141
self.assert_(lib in cmd.library_dirs)
142
self.assert_(incl in cmd.include_dirs)
145
src = _get_source_filename()
146
if not os.path.exists(src):
148
print('test_build_ext: Cannot find source code (test'
149
' must run in python build dir)')
150
return unittest.TestSuite()
151
else: return unittest.makeSuite(BuildExtTestCase)
153
if __name__ == '__main__':
154
support.run_unittest(test_suite())