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