| 1 | n/a | import sys | 
|---|
| 2 | n/a | import os | 
|---|
| 3 | n/a | from io import StringIO | 
|---|
| 4 | n/a | import textwrap | 
|---|
| 5 | n/a |  | 
|---|
| 6 | n/a | from distutils.core import Distribution | 
|---|
| 7 | n/a | from distutils.command.build_ext import build_ext | 
|---|
| 8 | n/a | from distutils import sysconfig | 
|---|
| 9 | n/a | from distutils.tests.support import (TempdirManager, LoggingSilencer, | 
|---|
| 10 | n/a |                                      copy_xxmodule_c, fixup_build_ext) | 
|---|
| 11 | n/a | from distutils.extension import Extension | 
|---|
| 12 | n/a | from distutils.errors import ( | 
|---|
| 13 | n/a |     CompileError, DistutilsPlatformError, DistutilsSetupError, | 
|---|
| 14 | n/a |     UnknownFileError) | 
|---|
| 15 | n/a |  | 
|---|
| 16 | n/a | import unittest | 
|---|
| 17 | n/a | from test import support | 
|---|
| 18 | n/a |  | 
|---|
| 19 | n/a | # http://bugs.python.org/issue4373 | 
|---|
| 20 | n/a | # Don't load the xx module more than once. | 
|---|
| 21 | n/a | ALREADY_TESTED = False | 
|---|
| 22 | n/a |  | 
|---|
| 23 | n/a |  | 
|---|
| 24 | n/a | class BuildExtTestCase(TempdirManager, | 
|---|
| 25 | n/a |                        LoggingSilencer, | 
|---|
| 26 | n/a |                        unittest.TestCase): | 
|---|
| 27 | n/a |     def setUp(self): | 
|---|
| 28 | n/a |         # Create a simple test environment | 
|---|
| 29 | n/a |         # Note that we're making changes to sys.path | 
|---|
| 30 | n/a |         super(BuildExtTestCase, self).setUp() | 
|---|
| 31 | n/a |         self.tmp_dir = self.mkdtemp() | 
|---|
| 32 | n/a |         self.sys_path = sys.path, sys.path[:] | 
|---|
| 33 | n/a |         sys.path.append(self.tmp_dir) | 
|---|
| 34 | n/a |         import site | 
|---|
| 35 | n/a |         self.old_user_base = site.USER_BASE | 
|---|
| 36 | n/a |         site.USER_BASE = self.mkdtemp() | 
|---|
| 37 | n/a |         from distutils.command import build_ext | 
|---|
| 38 | n/a |         build_ext.USER_BASE = site.USER_BASE | 
|---|
| 39 | n/a |  | 
|---|
| 40 | n/a |     def build_ext(self, *args, **kwargs): | 
|---|
| 41 | n/a |         return build_ext(*args, **kwargs) | 
|---|
| 42 | n/a |  | 
|---|
| 43 | n/a |     def test_build_ext(self): | 
|---|
| 44 | n/a |         cmd = support.missing_compiler_executable() | 
|---|
| 45 | n/a |         if cmd is not None: | 
|---|
| 46 | n/a |             self.skipTest('The %r command is not found' % cmd) | 
|---|
| 47 | n/a |         global ALREADY_TESTED | 
|---|
| 48 | n/a |         copy_xxmodule_c(self.tmp_dir) | 
|---|
| 49 | n/a |         xx_c = os.path.join(self.tmp_dir, 'xxmodule.c') | 
|---|
| 50 | n/a |         xx_ext = Extension('xx', [xx_c]) | 
|---|
| 51 | n/a |         dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]}) | 
|---|
| 52 | n/a |         dist.package_dir = self.tmp_dir | 
|---|
| 53 | n/a |         cmd = self.build_ext(dist) | 
|---|
| 54 | n/a |         fixup_build_ext(cmd) | 
|---|
| 55 | n/a |         cmd.build_lib = self.tmp_dir | 
|---|
| 56 | n/a |         cmd.build_temp = self.tmp_dir | 
|---|
| 57 | n/a |  | 
|---|
| 58 | n/a |         old_stdout = sys.stdout | 
|---|
| 59 | n/a |         if not support.verbose: | 
|---|
| 60 | n/a |             # silence compiler output | 
|---|
| 61 | n/a |             sys.stdout = StringIO() | 
|---|
| 62 | n/a |         try: | 
|---|
| 63 | n/a |             cmd.ensure_finalized() | 
|---|
| 64 | n/a |             cmd.run() | 
|---|
| 65 | n/a |         finally: | 
|---|
| 66 | n/a |             sys.stdout = old_stdout | 
|---|
| 67 | n/a |  | 
|---|
| 68 | n/a |         if ALREADY_TESTED: | 
|---|
| 69 | n/a |             self.skipTest('Already tested in %s' % ALREADY_TESTED) | 
|---|
| 70 | n/a |         else: | 
|---|
| 71 | n/a |             ALREADY_TESTED = type(self).__name__ | 
|---|
| 72 | n/a |  | 
|---|
| 73 | n/a |         import xx | 
|---|
| 74 | n/a |  | 
|---|
| 75 | n/a |         for attr in ('error', 'foo', 'new', 'roj'): | 
|---|
| 76 | n/a |             self.assertTrue(hasattr(xx, attr)) | 
|---|
| 77 | n/a |  | 
|---|
| 78 | n/a |         self.assertEqual(xx.foo(2, 5), 7) | 
|---|
| 79 | n/a |         self.assertEqual(xx.foo(13,15), 28) | 
|---|
| 80 | n/a |         self.assertEqual(xx.new().demo(), None) | 
|---|
| 81 | n/a |         if support.HAVE_DOCSTRINGS: | 
|---|
| 82 | n/a |             doc = 'This is a template module just for instruction.' | 
|---|
| 83 | n/a |             self.assertEqual(xx.__doc__, doc) | 
|---|
| 84 | n/a |         self.assertIsInstance(xx.Null(), xx.Null) | 
|---|
| 85 | n/a |         self.assertIsInstance(xx.Str(), xx.Str) | 
|---|
| 86 | n/a |  | 
|---|
| 87 | n/a |     def tearDown(self): | 
|---|
| 88 | n/a |         # Get everything back to normal | 
|---|
| 89 | n/a |         support.unload('xx') | 
|---|
| 90 | n/a |         sys.path = self.sys_path[0] | 
|---|
| 91 | n/a |         sys.path[:] = self.sys_path[1] | 
|---|
| 92 | n/a |         import site | 
|---|
| 93 | n/a |         site.USER_BASE = self.old_user_base | 
|---|
| 94 | n/a |         from distutils.command import build_ext | 
|---|
| 95 | n/a |         build_ext.USER_BASE = self.old_user_base | 
|---|
| 96 | n/a |         super(BuildExtTestCase, self).tearDown() | 
|---|
| 97 | n/a |  | 
|---|
| 98 | n/a |     def test_solaris_enable_shared(self): | 
|---|
| 99 | n/a |         dist = Distribution({'name': 'xx'}) | 
|---|
| 100 | n/a |         cmd = self.build_ext(dist) | 
|---|
| 101 | n/a |         old = sys.platform | 
|---|
| 102 | n/a |  | 
|---|
| 103 | n/a |         sys.platform = 'sunos' # fooling finalize_options | 
|---|
| 104 | n/a |         from distutils.sysconfig import  _config_vars | 
|---|
| 105 | n/a |         old_var = _config_vars.get('Py_ENABLE_SHARED') | 
|---|
| 106 | n/a |         _config_vars['Py_ENABLE_SHARED'] = 1 | 
|---|
| 107 | n/a |         try: | 
|---|
| 108 | n/a |             cmd.ensure_finalized() | 
|---|
| 109 | n/a |         finally: | 
|---|
| 110 | n/a |             sys.platform = old | 
|---|
| 111 | n/a |             if old_var is None: | 
|---|
| 112 | n/a |                 del _config_vars['Py_ENABLE_SHARED'] | 
|---|
| 113 | n/a |             else: | 
|---|
| 114 | n/a |                 _config_vars['Py_ENABLE_SHARED'] = old_var | 
|---|
| 115 | n/a |  | 
|---|
| 116 | n/a |         # make sure we get some library dirs under solaris | 
|---|
| 117 | n/a |         self.assertGreater(len(cmd.library_dirs), 0) | 
|---|
| 118 | n/a |  | 
|---|
| 119 | n/a |     def test_user_site(self): | 
|---|
| 120 | n/a |         import site | 
|---|
| 121 | n/a |         dist = Distribution({'name': 'xx'}) | 
|---|
| 122 | n/a |         cmd = self.build_ext(dist) | 
|---|
| 123 | n/a |  | 
|---|
| 124 | n/a |         # making sure the user option is there | 
|---|
| 125 | n/a |         options = [name for name, short, lable in | 
|---|
| 126 | n/a |                    cmd.user_options] | 
|---|
| 127 | n/a |         self.assertIn('user', options) | 
|---|
| 128 | n/a |  | 
|---|
| 129 | n/a |         # setting a value | 
|---|
| 130 | n/a |         cmd.user = 1 | 
|---|
| 131 | n/a |  | 
|---|
| 132 | n/a |         # setting user based lib and include | 
|---|
| 133 | n/a |         lib = os.path.join(site.USER_BASE, 'lib') | 
|---|
| 134 | n/a |         incl = os.path.join(site.USER_BASE, 'include') | 
|---|
| 135 | n/a |         os.mkdir(lib) | 
|---|
| 136 | n/a |         os.mkdir(incl) | 
|---|
| 137 | n/a |  | 
|---|
| 138 | n/a |         # let's run finalize | 
|---|
| 139 | n/a |         cmd.ensure_finalized() | 
|---|
| 140 | n/a |  | 
|---|
| 141 | n/a |         # see if include_dirs and library_dirs | 
|---|
| 142 | n/a |         # were set | 
|---|
| 143 | n/a |         self.assertIn(lib, cmd.library_dirs) | 
|---|
| 144 | n/a |         self.assertIn(lib, cmd.rpath) | 
|---|
| 145 | n/a |         self.assertIn(incl, cmd.include_dirs) | 
|---|
| 146 | n/a |  | 
|---|
| 147 | n/a |     def test_optional_extension(self): | 
|---|
| 148 | n/a |  | 
|---|
| 149 | n/a |         # this extension will fail, but let's ignore this failure | 
|---|
| 150 | n/a |         # with the optional argument. | 
|---|
| 151 | n/a |         modules = [Extension('foo', ['xxx'], optional=False)] | 
|---|
| 152 | n/a |         dist = Distribution({'name': 'xx', 'ext_modules': modules}) | 
|---|
| 153 | n/a |         cmd = self.build_ext(dist) | 
|---|
| 154 | n/a |         cmd.ensure_finalized() | 
|---|
| 155 | n/a |         self.assertRaises((UnknownFileError, CompileError), | 
|---|
| 156 | n/a |                           cmd.run)  # should raise an error | 
|---|
| 157 | n/a |  | 
|---|
| 158 | n/a |         modules = [Extension('foo', ['xxx'], optional=True)] | 
|---|
| 159 | n/a |         dist = Distribution({'name': 'xx', 'ext_modules': modules}) | 
|---|
| 160 | n/a |         cmd = self.build_ext(dist) | 
|---|
| 161 | n/a |         cmd.ensure_finalized() | 
|---|
| 162 | n/a |         cmd.run()  # should pass | 
|---|
| 163 | n/a |  | 
|---|
| 164 | n/a |     def test_finalize_options(self): | 
|---|
| 165 | n/a |         # Make sure Python's include directories (for Python.h, pyconfig.h, | 
|---|
| 166 | n/a |         # etc.) are in the include search path. | 
|---|
| 167 | n/a |         modules = [Extension('foo', ['xxx'], optional=False)] | 
|---|
| 168 | n/a |         dist = Distribution({'name': 'xx', 'ext_modules': modules}) | 
|---|
| 169 | n/a |         cmd = self.build_ext(dist) | 
|---|
| 170 | n/a |         cmd.finalize_options() | 
|---|
| 171 | n/a |  | 
|---|
| 172 | n/a |         py_include = sysconfig.get_python_inc() | 
|---|
| 173 | n/a |         self.assertIn(py_include, cmd.include_dirs) | 
|---|
| 174 | n/a |  | 
|---|
| 175 | n/a |         plat_py_include = sysconfig.get_python_inc(plat_specific=1) | 
|---|
| 176 | n/a |         self.assertIn(plat_py_include, cmd.include_dirs) | 
|---|
| 177 | n/a |  | 
|---|
| 178 | n/a |         # make sure cmd.libraries is turned into a list | 
|---|
| 179 | n/a |         # if it's a string | 
|---|
| 180 | n/a |         cmd = self.build_ext(dist) | 
|---|
| 181 | n/a |         cmd.libraries = 'my_lib, other_lib lastlib' | 
|---|
| 182 | n/a |         cmd.finalize_options() | 
|---|
| 183 | n/a |         self.assertEqual(cmd.libraries, ['my_lib', 'other_lib', 'lastlib']) | 
|---|
| 184 | n/a |  | 
|---|
| 185 | n/a |         # make sure cmd.library_dirs is turned into a list | 
|---|
| 186 | n/a |         # if it's a string | 
|---|
| 187 | n/a |         cmd = self.build_ext(dist) | 
|---|
| 188 | n/a |         cmd.library_dirs = 'my_lib_dir%sother_lib_dir' % os.pathsep | 
|---|
| 189 | n/a |         cmd.finalize_options() | 
|---|
| 190 | n/a |         self.assertIn('my_lib_dir', cmd.library_dirs) | 
|---|
| 191 | n/a |         self.assertIn('other_lib_dir', cmd.library_dirs) | 
|---|
| 192 | n/a |  | 
|---|
| 193 | n/a |         # make sure rpath is turned into a list | 
|---|
| 194 | n/a |         # if it's a string | 
|---|
| 195 | n/a |         cmd = self.build_ext(dist) | 
|---|
| 196 | n/a |         cmd.rpath = 'one%stwo' % os.pathsep | 
|---|
| 197 | n/a |         cmd.finalize_options() | 
|---|
| 198 | n/a |         self.assertEqual(cmd.rpath, ['one', 'two']) | 
|---|
| 199 | n/a |  | 
|---|
| 200 | n/a |         # make sure cmd.link_objects is turned into a list | 
|---|
| 201 | n/a |         # if it's a string | 
|---|
| 202 | n/a |         cmd = build_ext(dist) | 
|---|
| 203 | n/a |         cmd.link_objects = 'one two,three' | 
|---|
| 204 | n/a |         cmd.finalize_options() | 
|---|
| 205 | n/a |         self.assertEqual(cmd.link_objects, ['one', 'two', 'three']) | 
|---|
| 206 | n/a |  | 
|---|
| 207 | n/a |         # XXX more tests to perform for win32 | 
|---|
| 208 | n/a |  | 
|---|
| 209 | n/a |         # make sure define is turned into 2-tuples | 
|---|
| 210 | n/a |         # strings if they are ','-separated strings | 
|---|
| 211 | n/a |         cmd = self.build_ext(dist) | 
|---|
| 212 | n/a |         cmd.define = 'one,two' | 
|---|
| 213 | n/a |         cmd.finalize_options() | 
|---|
| 214 | n/a |         self.assertEqual(cmd.define, [('one', '1'), ('two', '1')]) | 
|---|
| 215 | n/a |  | 
|---|
| 216 | n/a |         # make sure undef is turned into a list of | 
|---|
| 217 | n/a |         # strings if they are ','-separated strings | 
|---|
| 218 | n/a |         cmd = self.build_ext(dist) | 
|---|
| 219 | n/a |         cmd.undef = 'one,two' | 
|---|
| 220 | n/a |         cmd.finalize_options() | 
|---|
| 221 | n/a |         self.assertEqual(cmd.undef, ['one', 'two']) | 
|---|
| 222 | n/a |  | 
|---|
| 223 | n/a |         # make sure swig_opts is turned into a list | 
|---|
| 224 | n/a |         cmd = self.build_ext(dist) | 
|---|
| 225 | n/a |         cmd.swig_opts = None | 
|---|
| 226 | n/a |         cmd.finalize_options() | 
|---|
| 227 | n/a |         self.assertEqual(cmd.swig_opts, []) | 
|---|
| 228 | n/a |  | 
|---|
| 229 | n/a |         cmd = self.build_ext(dist) | 
|---|
| 230 | n/a |         cmd.swig_opts = '1 2' | 
|---|
| 231 | n/a |         cmd.finalize_options() | 
|---|
| 232 | n/a |         self.assertEqual(cmd.swig_opts, ['1', '2']) | 
|---|
| 233 | n/a |  | 
|---|
| 234 | n/a |     def test_check_extensions_list(self): | 
|---|
| 235 | n/a |         dist = Distribution() | 
|---|
| 236 | n/a |         cmd = self.build_ext(dist) | 
|---|
| 237 | n/a |         cmd.finalize_options() | 
|---|
| 238 | n/a |  | 
|---|
| 239 | n/a |         #'extensions' option must be a list of Extension instances | 
|---|
| 240 | n/a |         self.assertRaises(DistutilsSetupError, | 
|---|
| 241 | n/a |                           cmd.check_extensions_list, 'foo') | 
|---|
| 242 | n/a |  | 
|---|
| 243 | n/a |         # each element of 'ext_modules' option must be an | 
|---|
| 244 | n/a |         # Extension instance or 2-tuple | 
|---|
| 245 | n/a |         exts = [('bar', 'foo', 'bar'), 'foo'] | 
|---|
| 246 | n/a |         self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts) | 
|---|
| 247 | n/a |  | 
|---|
| 248 | n/a |         # first element of each tuple in 'ext_modules' | 
|---|
| 249 | n/a |         # must be the extension name (a string) and match | 
|---|
| 250 | n/a |         # a python dotted-separated name | 
|---|
| 251 | n/a |         exts = [('foo-bar', '')] | 
|---|
| 252 | n/a |         self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts) | 
|---|
| 253 | n/a |  | 
|---|
| 254 | n/a |         # second element of each tuple in 'ext_modules' | 
|---|
| 255 | n/a |         # must be a dictionary (build info) | 
|---|
| 256 | n/a |         exts = [('foo.bar', '')] | 
|---|
| 257 | n/a |         self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts) | 
|---|
| 258 | n/a |  | 
|---|
| 259 | n/a |         # ok this one should pass | 
|---|
| 260 | n/a |         exts = [('foo.bar', {'sources': [''], 'libraries': 'foo', | 
|---|
| 261 | n/a |                              'some': 'bar'})] | 
|---|
| 262 | n/a |         cmd.check_extensions_list(exts) | 
|---|
| 263 | n/a |         ext = exts[0] | 
|---|
| 264 | n/a |         self.assertIsInstance(ext, Extension) | 
|---|
| 265 | n/a |  | 
|---|
| 266 | n/a |         # check_extensions_list adds in ext the values passed | 
|---|
| 267 | n/a |         # when they are in ('include_dirs', 'library_dirs', 'libraries' | 
|---|
| 268 | n/a |         # 'extra_objects', 'extra_compile_args', 'extra_link_args') | 
|---|
| 269 | n/a |         self.assertEqual(ext.libraries, 'foo') | 
|---|
| 270 | n/a |         self.assertFalse(hasattr(ext, 'some')) | 
|---|
| 271 | n/a |  | 
|---|
| 272 | n/a |         # 'macros' element of build info dict must be 1- or 2-tuple | 
|---|
| 273 | n/a |         exts = [('foo.bar', {'sources': [''], 'libraries': 'foo', | 
|---|
| 274 | n/a |                 'some': 'bar', 'macros': [('1', '2', '3'), 'foo']})] | 
|---|
| 275 | n/a |         self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts) | 
|---|
| 276 | n/a |  | 
|---|
| 277 | n/a |         exts[0][1]['macros'] = [('1', '2'), ('3',)] | 
|---|
| 278 | n/a |         cmd.check_extensions_list(exts) | 
|---|
| 279 | n/a |         self.assertEqual(exts[0].undef_macros, ['3']) | 
|---|
| 280 | n/a |         self.assertEqual(exts[0].define_macros, [('1', '2')]) | 
|---|
| 281 | n/a |  | 
|---|
| 282 | n/a |     def test_get_source_files(self): | 
|---|
| 283 | n/a |         modules = [Extension('foo', ['xxx'], optional=False)] | 
|---|
| 284 | n/a |         dist = Distribution({'name': 'xx', 'ext_modules': modules}) | 
|---|
| 285 | n/a |         cmd = self.build_ext(dist) | 
|---|
| 286 | n/a |         cmd.ensure_finalized() | 
|---|
| 287 | n/a |         self.assertEqual(cmd.get_source_files(), ['xxx']) | 
|---|
| 288 | n/a |  | 
|---|
| 289 | n/a |     def test_compiler_option(self): | 
|---|
| 290 | n/a |         # cmd.compiler is an option and | 
|---|
| 291 | n/a |         # should not be overridden by a compiler instance | 
|---|
| 292 | n/a |         # when the command is run | 
|---|
| 293 | n/a |         dist = Distribution() | 
|---|
| 294 | n/a |         cmd = self.build_ext(dist) | 
|---|
| 295 | n/a |         cmd.compiler = 'unix' | 
|---|
| 296 | n/a |         cmd.ensure_finalized() | 
|---|
| 297 | n/a |         cmd.run() | 
|---|
| 298 | n/a |         self.assertEqual(cmd.compiler, 'unix') | 
|---|
| 299 | n/a |  | 
|---|
| 300 | n/a |     def test_get_outputs(self): | 
|---|
| 301 | n/a |         cmd = support.missing_compiler_executable() | 
|---|
| 302 | n/a |         if cmd is not None: | 
|---|
| 303 | n/a |             self.skipTest('The %r command is not found' % cmd) | 
|---|
| 304 | n/a |         tmp_dir = self.mkdtemp() | 
|---|
| 305 | n/a |         c_file = os.path.join(tmp_dir, 'foo.c') | 
|---|
| 306 | n/a |         self.write_file(c_file, 'void PyInit_foo(void) {}\n') | 
|---|
| 307 | n/a |         ext = Extension('foo', [c_file], optional=False) | 
|---|
| 308 | n/a |         dist = Distribution({'name': 'xx', | 
|---|
| 309 | n/a |                              'ext_modules': [ext]}) | 
|---|
| 310 | n/a |         cmd = self.build_ext(dist) | 
|---|
| 311 | n/a |         fixup_build_ext(cmd) | 
|---|
| 312 | n/a |         cmd.ensure_finalized() | 
|---|
| 313 | n/a |         self.assertEqual(len(cmd.get_outputs()), 1) | 
|---|
| 314 | n/a |  | 
|---|
| 315 | n/a |         cmd.build_lib = os.path.join(self.tmp_dir, 'build') | 
|---|
| 316 | n/a |         cmd.build_temp = os.path.join(self.tmp_dir, 'tempt') | 
|---|
| 317 | n/a |  | 
|---|
| 318 | n/a |         # issue #5977 : distutils build_ext.get_outputs | 
|---|
| 319 | n/a |         # returns wrong result with --inplace | 
|---|
| 320 | n/a |         other_tmp_dir = os.path.realpath(self.mkdtemp()) | 
|---|
| 321 | n/a |         old_wd = os.getcwd() | 
|---|
| 322 | n/a |         os.chdir(other_tmp_dir) | 
|---|
| 323 | n/a |         try: | 
|---|
| 324 | n/a |             cmd.inplace = 1 | 
|---|
| 325 | n/a |             cmd.run() | 
|---|
| 326 | n/a |             so_file = cmd.get_outputs()[0] | 
|---|
| 327 | n/a |         finally: | 
|---|
| 328 | n/a |             os.chdir(old_wd) | 
|---|
| 329 | n/a |         self.assertTrue(os.path.exists(so_file)) | 
|---|
| 330 | n/a |         ext_suffix = sysconfig.get_config_var('EXT_SUFFIX') | 
|---|
| 331 | n/a |         self.assertTrue(so_file.endswith(ext_suffix)) | 
|---|
| 332 | n/a |         so_dir = os.path.dirname(so_file) | 
|---|
| 333 | n/a |         self.assertEqual(so_dir, other_tmp_dir) | 
|---|
| 334 | n/a |  | 
|---|
| 335 | n/a |         cmd.inplace = 0 | 
|---|
| 336 | n/a |         cmd.compiler = None | 
|---|
| 337 | n/a |         cmd.run() | 
|---|
| 338 | n/a |         so_file = cmd.get_outputs()[0] | 
|---|
| 339 | n/a |         self.assertTrue(os.path.exists(so_file)) | 
|---|
| 340 | n/a |         self.assertTrue(so_file.endswith(ext_suffix)) | 
|---|
| 341 | n/a |         so_dir = os.path.dirname(so_file) | 
|---|
| 342 | n/a |         self.assertEqual(so_dir, cmd.build_lib) | 
|---|
| 343 | n/a |  | 
|---|
| 344 | n/a |         # inplace = 0, cmd.package = 'bar' | 
|---|
| 345 | n/a |         build_py = cmd.get_finalized_command('build_py') | 
|---|
| 346 | n/a |         build_py.package_dir = {'': 'bar'} | 
|---|
| 347 | n/a |         path = cmd.get_ext_fullpath('foo') | 
|---|
| 348 | n/a |         # checking that the last directory is the build_dir | 
|---|
| 349 | n/a |         path = os.path.split(path)[0] | 
|---|
| 350 | n/a |         self.assertEqual(path, cmd.build_lib) | 
|---|
| 351 | n/a |  | 
|---|
| 352 | n/a |         # inplace = 1, cmd.package = 'bar' | 
|---|
| 353 | n/a |         cmd.inplace = 1 | 
|---|
| 354 | n/a |         other_tmp_dir = os.path.realpath(self.mkdtemp()) | 
|---|
| 355 | n/a |         old_wd = os.getcwd() | 
|---|
| 356 | n/a |         os.chdir(other_tmp_dir) | 
|---|
| 357 | n/a |         try: | 
|---|
| 358 | n/a |             path = cmd.get_ext_fullpath('foo') | 
|---|
| 359 | n/a |         finally: | 
|---|
| 360 | n/a |             os.chdir(old_wd) | 
|---|
| 361 | n/a |         # checking that the last directory is bar | 
|---|
| 362 | n/a |         path = os.path.split(path)[0] | 
|---|
| 363 | n/a |         lastdir = os.path.split(path)[-1] | 
|---|
| 364 | n/a |         self.assertEqual(lastdir, 'bar') | 
|---|
| 365 | n/a |  | 
|---|
| 366 | n/a |     def test_ext_fullpath(self): | 
|---|
| 367 | n/a |         ext = sysconfig.get_config_var('EXT_SUFFIX') | 
|---|
| 368 | n/a |         # building lxml.etree inplace | 
|---|
| 369 | n/a |         #etree_c = os.path.join(self.tmp_dir, 'lxml.etree.c') | 
|---|
| 370 | n/a |         #etree_ext = Extension('lxml.etree', [etree_c]) | 
|---|
| 371 | n/a |         #dist = Distribution({'name': 'lxml', 'ext_modules': [etree_ext]}) | 
|---|
| 372 | n/a |         dist = Distribution() | 
|---|
| 373 | n/a |         cmd = self.build_ext(dist) | 
|---|
| 374 | n/a |         cmd.inplace = 1 | 
|---|
| 375 | n/a |         cmd.distribution.package_dir = {'': 'src'} | 
|---|
| 376 | n/a |         cmd.distribution.packages = ['lxml', 'lxml.html'] | 
|---|
| 377 | n/a |         curdir = os.getcwd() | 
|---|
| 378 | n/a |         wanted = os.path.join(curdir, 'src', 'lxml', 'etree' + ext) | 
|---|
| 379 | n/a |         path = cmd.get_ext_fullpath('lxml.etree') | 
|---|
| 380 | n/a |         self.assertEqual(wanted, path) | 
|---|
| 381 | n/a |  | 
|---|
| 382 | n/a |         # building lxml.etree not inplace | 
|---|
| 383 | n/a |         cmd.inplace = 0 | 
|---|
| 384 | n/a |         cmd.build_lib = os.path.join(curdir, 'tmpdir') | 
|---|
| 385 | n/a |         wanted = os.path.join(curdir, 'tmpdir', 'lxml', 'etree' + ext) | 
|---|
| 386 | n/a |         path = cmd.get_ext_fullpath('lxml.etree') | 
|---|
| 387 | n/a |         self.assertEqual(wanted, path) | 
|---|
| 388 | n/a |  | 
|---|
| 389 | n/a |         # building twisted.runner.portmap not inplace | 
|---|
| 390 | n/a |         build_py = cmd.get_finalized_command('build_py') | 
|---|
| 391 | n/a |         build_py.package_dir = {} | 
|---|
| 392 | n/a |         cmd.distribution.packages = ['twisted', 'twisted.runner.portmap'] | 
|---|
| 393 | n/a |         path = cmd.get_ext_fullpath('twisted.runner.portmap') | 
|---|
| 394 | n/a |         wanted = os.path.join(curdir, 'tmpdir', 'twisted', 'runner', | 
|---|
| 395 | n/a |                               'portmap' + ext) | 
|---|
| 396 | n/a |         self.assertEqual(wanted, path) | 
|---|
| 397 | n/a |  | 
|---|
| 398 | n/a |         # building twisted.runner.portmap inplace | 
|---|
| 399 | n/a |         cmd.inplace = 1 | 
|---|
| 400 | n/a |         path = cmd.get_ext_fullpath('twisted.runner.portmap') | 
|---|
| 401 | n/a |         wanted = os.path.join(curdir, 'twisted', 'runner', 'portmap' + ext) | 
|---|
| 402 | n/a |         self.assertEqual(wanted, path) | 
|---|
| 403 | n/a |  | 
|---|
| 404 | n/a |  | 
|---|
| 405 | n/a |     @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for MacOSX') | 
|---|
| 406 | n/a |     def test_deployment_target_default(self): | 
|---|
| 407 | n/a |         # Issue 9516: Test that, in the absence of the environment variable, | 
|---|
| 408 | n/a |         # an extension module is compiled with the same deployment target as | 
|---|
| 409 | n/a |         #  the interpreter. | 
|---|
| 410 | n/a |         self._try_compile_deployment_target('==', None) | 
|---|
| 411 | n/a |  | 
|---|
| 412 | n/a |     @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for MacOSX') | 
|---|
| 413 | n/a |     def test_deployment_target_too_low(self): | 
|---|
| 414 | n/a |         # Issue 9516: Test that an extension module is not allowed to be | 
|---|
| 415 | n/a |         # compiled with a deployment target less than that of the interpreter. | 
|---|
| 416 | n/a |         self.assertRaises(DistutilsPlatformError, | 
|---|
| 417 | n/a |             self._try_compile_deployment_target, '>', '10.1') | 
|---|
| 418 | n/a |  | 
|---|
| 419 | n/a |     @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for MacOSX') | 
|---|
| 420 | n/a |     def test_deployment_target_higher_ok(self): | 
|---|
| 421 | n/a |         # Issue 9516: Test that an extension module can be compiled with a | 
|---|
| 422 | n/a |         # deployment target higher than that of the interpreter: the ext | 
|---|
| 423 | n/a |         # module may depend on some newer OS feature. | 
|---|
| 424 | n/a |         deptarget = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') | 
|---|
| 425 | n/a |         if deptarget: | 
|---|
| 426 | n/a |             # increment the minor version number (i.e. 10.6 -> 10.7) | 
|---|
| 427 | n/a |             deptarget = [int(x) for x in deptarget.split('.')] | 
|---|
| 428 | n/a |             deptarget[-1] += 1 | 
|---|
| 429 | n/a |             deptarget = '.'.join(str(i) for i in deptarget) | 
|---|
| 430 | n/a |             self._try_compile_deployment_target('<', deptarget) | 
|---|
| 431 | n/a |  | 
|---|
| 432 | n/a |     def _try_compile_deployment_target(self, operator, target): | 
|---|
| 433 | n/a |         orig_environ = os.environ | 
|---|
| 434 | n/a |         os.environ = orig_environ.copy() | 
|---|
| 435 | n/a |         self.addCleanup(setattr, os, 'environ', orig_environ) | 
|---|
| 436 | n/a |  | 
|---|
| 437 | n/a |         if target is None: | 
|---|
| 438 | n/a |             if os.environ.get('MACOSX_DEPLOYMENT_TARGET'): | 
|---|
| 439 | n/a |                 del os.environ['MACOSX_DEPLOYMENT_TARGET'] | 
|---|
| 440 | n/a |         else: | 
|---|
| 441 | n/a |             os.environ['MACOSX_DEPLOYMENT_TARGET'] = target | 
|---|
| 442 | n/a |  | 
|---|
| 443 | n/a |         deptarget_c = os.path.join(self.tmp_dir, 'deptargetmodule.c') | 
|---|
| 444 | n/a |  | 
|---|
| 445 | n/a |         with open(deptarget_c, 'w') as fp: | 
|---|
| 446 | n/a |             fp.write(textwrap.dedent('''\ | 
|---|
| 447 | n/a |                 #include <AvailabilityMacros.h> | 
|---|
| 448 | n/a |  | 
|---|
| 449 | n/a |                 int dummy; | 
|---|
| 450 | n/a |  | 
|---|
| 451 | n/a |                 #if TARGET %s MAC_OS_X_VERSION_MIN_REQUIRED | 
|---|
| 452 | n/a |                 #else | 
|---|
| 453 | n/a |                 #error "Unexpected target" | 
|---|
| 454 | n/a |                 #endif | 
|---|
| 455 | n/a |  | 
|---|
| 456 | n/a |             ''' % operator)) | 
|---|
| 457 | n/a |  | 
|---|
| 458 | n/a |         # get the deployment target that the interpreter was built with | 
|---|
| 459 | n/a |         target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') | 
|---|
| 460 | n/a |         target = tuple(map(int, target.split('.')[0:2])) | 
|---|
| 461 | n/a |         # format the target value as defined in the Apple | 
|---|
| 462 | n/a |         # Availability Macros.  We can't use the macro names since | 
|---|
| 463 | n/a |         # at least one value we test with will not exist yet. | 
|---|
| 464 | n/a |         if target[1] < 10: | 
|---|
| 465 | n/a |             # for 10.1 through 10.9.x -> "10n0" | 
|---|
| 466 | n/a |             target = '%02d%01d0' % target | 
|---|
| 467 | n/a |         else: | 
|---|
| 468 | n/a |             # for 10.10 and beyond -> "10nn00" | 
|---|
| 469 | n/a |             target = '%02d%02d00' % target | 
|---|
| 470 | n/a |         deptarget_ext = Extension( | 
|---|
| 471 | n/a |             'deptarget', | 
|---|
| 472 | n/a |             [deptarget_c], | 
|---|
| 473 | n/a |             extra_compile_args=['-DTARGET=%s'%(target,)], | 
|---|
| 474 | n/a |         ) | 
|---|
| 475 | n/a |         dist = Distribution({ | 
|---|
| 476 | n/a |             'name': 'deptarget', | 
|---|
| 477 | n/a |             'ext_modules': [deptarget_ext] | 
|---|
| 478 | n/a |         }) | 
|---|
| 479 | n/a |         dist.package_dir = self.tmp_dir | 
|---|
| 480 | n/a |         cmd = self.build_ext(dist) | 
|---|
| 481 | n/a |         cmd.build_lib = self.tmp_dir | 
|---|
| 482 | n/a |         cmd.build_temp = self.tmp_dir | 
|---|
| 483 | n/a |  | 
|---|
| 484 | n/a |         try: | 
|---|
| 485 | n/a |             old_stdout = sys.stdout | 
|---|
| 486 | n/a |             if not support.verbose: | 
|---|
| 487 | n/a |                 # silence compiler output | 
|---|
| 488 | n/a |                 sys.stdout = StringIO() | 
|---|
| 489 | n/a |             try: | 
|---|
| 490 | n/a |                 cmd.ensure_finalized() | 
|---|
| 491 | n/a |                 cmd.run() | 
|---|
| 492 | n/a |             finally: | 
|---|
| 493 | n/a |                 sys.stdout = old_stdout | 
|---|
| 494 | n/a |  | 
|---|
| 495 | n/a |         except CompileError: | 
|---|
| 496 | n/a |             self.fail("Wrong deployment target during compilation") | 
|---|
| 497 | n/a |  | 
|---|
| 498 | n/a |  | 
|---|
| 499 | n/a | class ParallelBuildExtTestCase(BuildExtTestCase): | 
|---|
| 500 | n/a |  | 
|---|
| 501 | n/a |     def build_ext(self, *args, **kwargs): | 
|---|
| 502 | n/a |         build_ext = super().build_ext(*args, **kwargs) | 
|---|
| 503 | n/a |         build_ext.parallel = True | 
|---|
| 504 | n/a |         return build_ext | 
|---|
| 505 | n/a |  | 
|---|
| 506 | n/a |  | 
|---|
| 507 | n/a | def test_suite(): | 
|---|
| 508 | n/a |     suite = unittest.TestSuite() | 
|---|
| 509 | n/a |     suite.addTest(unittest.makeSuite(BuildExtTestCase)) | 
|---|
| 510 | n/a |     suite.addTest(unittest.makeSuite(ParallelBuildExtTestCase)) | 
|---|
| 511 | n/a |     return suite | 
|---|
| 512 | n/a |  | 
|---|
| 513 | n/a | if __name__ == '__main__': | 
|---|
| 514 | n/a |     support.run_unittest(__name__) | 
|---|