1import unittest 2import sys 3import os 4import subprocess 5import shutil 6from copy import copy 7 8from test.support import (run_unittest, 9 import_module, TESTFN, unlink, check_warnings, 10 captured_stdout, skip_unless_symlink, change_cwd) 11 12import sysconfig 13from sysconfig import (get_paths, get_platform, get_config_vars, 14 get_path, get_path_names, _INSTALL_SCHEMES, 15 _get_default_scheme, _expand_vars, 16 get_scheme_names, get_config_var, _main) 17import _osx_support 18 19class TestSysConfig(unittest.TestCase): 20 21 def setUp(self): 22 super(TestSysConfig, self).setUp() 23 self.sys_path = sys.path[:] 24 # patching os.uname 25 if hasattr(os, 'uname'): 26 self.uname = os.uname 27 self._uname = os.uname() 28 else: 29 self.uname = None 30 self._set_uname(('',)*5) 31 os.uname = self._get_uname 32 # saving the environment 33 self.name = os.name 34 self.platform = sys.platform 35 self.version = sys.version 36 self.sep = os.sep 37 self.join = os.path.join 38 self.isabs = os.path.isabs 39 self.splitdrive = os.path.splitdrive 40 self._config_vars = sysconfig._CONFIG_VARS, copy(sysconfig._CONFIG_VARS) 41 self._added_envvars = [] 42 self._changed_envvars = [] 43 for var in ('MACOSX_DEPLOYMENT_TARGET', 'PATH'): 44 if var in os.environ: 45 self._changed_envvars.append((var, os.environ[var])) 46 else: 47 self._added_envvars.append(var) 48 49 def tearDown(self): 50 sys.path[:] = self.sys_path 51 self._cleanup_testfn() 52 if self.uname is not None: 53 os.uname = self.uname 54 else: 55 del os.uname 56 os.name = self.name 57 sys.platform = self.platform 58 sys.version = self.version 59 os.sep = self.sep 60 os.path.join = self.join 61 os.path.isabs = self.isabs 62 os.path.splitdrive = self.splitdrive 63 sysconfig._CONFIG_VARS = self._config_vars[0] 64 sysconfig._CONFIG_VARS.clear() 65 sysconfig._CONFIG_VARS.update(self._config_vars[1]) 66 for var, value in self._changed_envvars: 67 os.environ[var] = value 68 for var in self._added_envvars: 69 os.environ.pop(var, None) 70 71 super(TestSysConfig, self).tearDown() 72 73 def _set_uname(self, uname): 74 self._uname = os.uname_result(uname) 75 76 def _get_uname(self): 77 return self._uname 78 79 def _cleanup_testfn(self): 80 path = TESTFN 81 if os.path.isfile(path): 82 os.remove(path) 83 elif os.path.isdir(path): 84 shutil.rmtree(path) 85 86 def test_get_path_names(self): 87 self.assertEqual(get_path_names(), sysconfig._SCHEME_KEYS) 88 89 def test_get_paths(self): 90 scheme = get_paths() 91 default_scheme = _get_default_scheme() 92 wanted = _expand_vars(default_scheme, None) 93 wanted = sorted(wanted.items()) 94 scheme = sorted(scheme.items()) 95 self.assertEqual(scheme, wanted) 96 97 def test_get_path(self): 98 # XXX make real tests here 99 for scheme in _INSTALL_SCHEMES: 100 for name in _INSTALL_SCHEMES[scheme]: 101 res = get_path(name, scheme) 102 103 def test_get_config_vars(self): 104 cvars = get_config_vars() 105 self.assertIsInstance(cvars, dict) 106 self.assertTrue(cvars) 107 108 def test_get_platform(self): 109 # windows XP, 32bits 110 os.name = 'nt' 111 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) ' 112 '[MSC v.1310 32 bit (Intel)]') 113 sys.platform = 'win32' 114 self.assertEqual(get_platform(), 'win32') 115 116 # windows XP, amd64 117 os.name = 'nt' 118 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) ' 119 '[MSC v.1310 32 bit (Amd64)]') 120 sys.platform = 'win32' 121 self.assertEqual(get_platform(), 'win-amd64') 122 123 # windows XP, itanium 124 os.name = 'nt' 125 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) ' 126 '[MSC v.1310 32 bit (Itanium)]') 127 sys.platform = 'win32' 128 self.assertEqual(get_platform(), 'win-ia64') 129 130 # macbook 131 os.name = 'posix' 132 sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) ' 133 '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]') 134 sys.platform = 'darwin' 135 self._set_uname(('Darwin', 'macziade', '8.11.1', 136 ('Darwin Kernel Version 8.11.1: ' 137 'Wed Oct 10 18:23:28 PDT 2007; ' 138 'root:xnu-792.25.20~1/RELEASE_I386'), 'PowerPC')) 139 _osx_support._remove_original_values(get_config_vars()) 140 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3' 141 142 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g ' 143 '-fwrapv -O3 -Wall -Wstrict-prototypes') 144 145 maxint = sys.maxsize 146 try: 147 sys.maxsize = 2147483647 148 self.assertEqual(get_platform(), 'macosx-10.3-ppc') 149 sys.maxsize = 9223372036854775807 150 self.assertEqual(get_platform(), 'macosx-10.3-ppc64') 151 finally: 152 sys.maxsize = maxint 153 154 self._set_uname(('Darwin', 'macziade', '8.11.1', 155 ('Darwin Kernel Version 8.11.1: ' 156 'Wed Oct 10 18:23:28 PDT 2007; ' 157 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386')) 158 _osx_support._remove_original_values(get_config_vars()) 159 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3' 160 161 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g ' 162 '-fwrapv -O3 -Wall -Wstrict-prototypes') 163 maxint = sys.maxsize 164 try: 165 sys.maxsize = 2147483647 166 self.assertEqual(get_platform(), 'macosx-10.3-i386') 167 sys.maxsize = 9223372036854775807 168 self.assertEqual(get_platform(), 'macosx-10.3-x86_64') 169 finally: 170 sys.maxsize = maxint 171 172 # macbook with fat binaries (fat, universal or fat64) 173 _osx_support._remove_original_values(get_config_vars()) 174 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.4' 175 get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot ' 176 '/Developer/SDKs/MacOSX10.4u.sdk ' 177 '-fno-strict-aliasing -fno-common ' 178 '-dynamic -DNDEBUG -g -O3') 179 180 self.assertEqual(get_platform(), 'macosx-10.4-fat') 181 182 _osx_support._remove_original_values(get_config_vars()) 183 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot ' 184 '/Developer/SDKs/MacOSX10.4u.sdk ' 185 '-fno-strict-aliasing -fno-common ' 186 '-dynamic -DNDEBUG -g -O3') 187 188 self.assertEqual(get_platform(), 'macosx-10.4-intel') 189 190 _osx_support._remove_original_values(get_config_vars()) 191 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot ' 192 '/Developer/SDKs/MacOSX10.4u.sdk ' 193 '-fno-strict-aliasing -fno-common ' 194 '-dynamic -DNDEBUG -g -O3') 195 self.assertEqual(get_platform(), 'macosx-10.4-fat3') 196 197 _osx_support._remove_original_values(get_config_vars()) 198 get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot ' 199 '/Developer/SDKs/MacOSX10.4u.sdk ' 200 '-fno-strict-aliasing -fno-common ' 201 '-dynamic -DNDEBUG -g -O3') 202 self.assertEqual(get_platform(), 'macosx-10.4-universal') 203 204 _osx_support._remove_original_values(get_config_vars()) 205 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -isysroot ' 206 '/Developer/SDKs/MacOSX10.4u.sdk ' 207 '-fno-strict-aliasing -fno-common ' 208 '-dynamic -DNDEBUG -g -O3') 209 210 self.assertEqual(get_platform(), 'macosx-10.4-fat64') 211 212 for arch in ('ppc', 'i386', 'x86_64', 'ppc64'): 213 _osx_support._remove_original_values(get_config_vars()) 214 get_config_vars()['CFLAGS'] = ('-arch %s -isysroot ' 215 '/Developer/SDKs/MacOSX10.4u.sdk ' 216 '-fno-strict-aliasing -fno-common ' 217 '-dynamic -DNDEBUG -g -O3' % arch) 218 219 self.assertEqual(get_platform(), 'macosx-10.4-%s' % arch) 220 221 # linux debian sarge 222 os.name = 'posix' 223 sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) ' 224 '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]') 225 sys.platform = 'linux2' 226 self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7', 227 '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686')) 228 229 self.assertEqual(get_platform(), 'linux-i686') 230 231 # XXX more platforms to tests here 232 233 def test_get_config_h_filename(self): 234 config_h = sysconfig.get_config_h_filename() 235 self.assertTrue(os.path.isfile(config_h), config_h) 236 237 def test_get_scheme_names(self): 238 wanted = ('nt', 'nt_user', 'osx_framework_user', 239 'posix_home', 'posix_prefix', 'posix_user') 240 self.assertEqual(get_scheme_names(), wanted) 241 242 @skip_unless_symlink 243 def test_symlink(self): 244 # On Windows, the EXE needs to know where pythonXY.dll is at so we have 245 # to add the directory to the path. 246 if sys.platform == "win32": 247 os.environ["PATH"] = "{};{}".format( 248 os.path.dirname(sys.executable), os.environ["PATH"]) 249 250 # Issue 7880 251 def get(python): 252 cmd = [python, '-c', 253 'import sysconfig; print(sysconfig.get_platform())'] 254 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=os.environ) 255 return p.communicate() 256 real = os.path.realpath(sys.executable) 257 link = os.path.abspath(TESTFN) 258 os.symlink(real, link) 259 try: 260 self.assertEqual(get(real), get(link)) 261 finally: 262 unlink(link) 263 264 def test_user_similar(self): 265 # Issue #8759: make sure the posix scheme for the users 266 # is similar to the global posix_prefix one 267 base = get_config_var('base') 268 user = get_config_var('userbase') 269 # the global scheme mirrors the distinction between prefix and 270 # exec-prefix but not the user scheme, so we have to adapt the paths 271 # before comparing (issue #9100) 272 adapt = sys.base_prefix != sys.base_exec_prefix 273 for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'): 274 global_path = get_path(name, 'posix_prefix') 275 if adapt: 276 global_path = global_path.replace(sys.exec_prefix, sys.base_prefix) 277 base = base.replace(sys.exec_prefix, sys.base_prefix) 278 elif sys.base_prefix != sys.prefix: 279 # virtual environment? Likewise, we have to adapt the paths 280 # before comparing 281 global_path = global_path.replace(sys.base_prefix, sys.prefix) 282 base = base.replace(sys.base_prefix, sys.prefix) 283 user_path = get_path(name, 'posix_user') 284 self.assertEqual(user_path, global_path.replace(base, user, 1)) 285 286 def test_main(self): 287 # just making sure _main() runs and returns things in the stdout 288 with captured_stdout() as output: 289 _main() 290 self.assertTrue(len(output.getvalue().split('\n')) > 0) 291 292 @unittest.skipIf(sys.platform == "win32", "Does not apply to Windows") 293 def test_ldshared_value(self): 294 ldflags = sysconfig.get_config_var('LDFLAGS') 295 ldshared = sysconfig.get_config_var('LDSHARED') 296 297 self.assertIn(ldflags, ldshared) 298 299 @unittest.skipUnless(sys.platform == "darwin", "test only relevant on MacOSX") 300 def test_platform_in_subprocess(self): 301 my_platform = sysconfig.get_platform() 302 303 # Test without MACOSX_DEPLOYMENT_TARGET in the environment 304 305 env = os.environ.copy() 306 if 'MACOSX_DEPLOYMENT_TARGET' in env: 307 del env['MACOSX_DEPLOYMENT_TARGET'] 308 309 p = subprocess.Popen([ 310 sys.executable, '-c', 311 'import sysconfig; print(sysconfig.get_platform())', 312 ], 313 stdout=subprocess.PIPE, 314 stderr=subprocess.DEVNULL, 315 env=env) 316 test_platform = p.communicate()[0].strip() 317 test_platform = test_platform.decode('utf-8') 318 status = p.wait() 319 320 self.assertEqual(status, 0) 321 self.assertEqual(my_platform, test_platform) 322 323 # Test with MACOSX_DEPLOYMENT_TARGET in the environment, and 324 # using a value that is unlikely to be the default one. 325 env = os.environ.copy() 326 env['MACOSX_DEPLOYMENT_TARGET'] = '10.1' 327 328 p = subprocess.Popen([ 329 sys.executable, '-c', 330 'import sysconfig; print(sysconfig.get_platform())', 331 ], 332 stdout=subprocess.PIPE, 333 stderr=subprocess.DEVNULL, 334 env=env) 335 test_platform = p.communicate()[0].strip() 336 test_platform = test_platform.decode('utf-8') 337 status = p.wait() 338 339 self.assertEqual(status, 0) 340 self.assertEqual(my_platform, test_platform) 341 342 def test_srcdir(self): 343 # See Issues #15322, #15364. 344 srcdir = sysconfig.get_config_var('srcdir') 345 346 self.assertTrue(os.path.isabs(srcdir), srcdir) 347 self.assertTrue(os.path.isdir(srcdir), srcdir) 348 349 if sysconfig._PYTHON_BUILD: 350 # The python executable has not been installed so srcdir 351 # should be a full source checkout. 352 Python_h = os.path.join(srcdir, 'Include', 'Python.h') 353 self.assertTrue(os.path.exists(Python_h), Python_h) 354 self.assertTrue(sysconfig._is_python_source_dir(srcdir)) 355 elif os.name == 'posix': 356 makefile_dir = os.path.dirname(sysconfig.get_makefile_filename()) 357 # Issue #19340: srcdir has been realpath'ed already 358 makefile_dir = os.path.realpath(makefile_dir) 359 self.assertEqual(makefile_dir, srcdir) 360 361 def test_srcdir_independent_of_cwd(self): 362 # srcdir should be independent of the current working directory 363 # See Issues #15322, #15364. 364 srcdir = sysconfig.get_config_var('srcdir') 365 with change_cwd(os.pardir): 366 srcdir2 = sysconfig.get_config_var('srcdir') 367 self.assertEqual(srcdir, srcdir2) 368 369 @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None, 370 'EXT_SUFFIX required for this test') 371 def test_SO_deprecation(self): 372 self.assertWarns(DeprecationWarning, 373 sysconfig.get_config_var, 'SO') 374 375 @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None, 376 'EXT_SUFFIX required for this test') 377 def test_SO_value(self): 378 with check_warnings(('', DeprecationWarning)): 379 self.assertEqual(sysconfig.get_config_var('SO'), 380 sysconfig.get_config_var('EXT_SUFFIX')) 381 382 @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None, 383 'EXT_SUFFIX required for this test') 384 def test_SO_in_vars(self): 385 vars = sysconfig.get_config_vars() 386 self.assertIsNotNone(vars['SO']) 387 self.assertEqual(vars['SO'], vars['EXT_SUFFIX']) 388 389 @unittest.skipUnless(sys.platform == 'linux' and 390 hasattr(sys.implementation, '_multiarch'), 391 'multiarch-specific test') 392 def test_triplet_in_ext_suffix(self): 393 ctypes = import_module('ctypes') 394 import platform, re 395 machine = platform.machine() 396 suffix = sysconfig.get_config_var('EXT_SUFFIX') 397 if re.match('(aarch64|arm|mips|ppc|powerpc|s390|sparc)', machine): 398 self.assertTrue('linux' in suffix, suffix) 399 if re.match('(i[3-6]86|x86_64)$', machine): 400 if ctypes.sizeof(ctypes.c_char_p()) == 4: 401 self.assertTrue(suffix.endswith('i386-linux-gnu.so') or 402 suffix.endswith('x86_64-linux-gnux32.so'), 403 suffix) 404 else: # 8 byte pointer size 405 self.assertTrue(suffix.endswith('x86_64-linux-gnu.so'), suffix) 406 407 @unittest.skipUnless(sys.platform == 'darwin', 'OS X-specific test') 408 def test_osx_ext_suffix(self): 409 suffix = sysconfig.get_config_var('EXT_SUFFIX') 410 self.assertTrue(suffix.endswith('-darwin.so'), suffix) 411 412class MakefileTests(unittest.TestCase): 413 414 @unittest.skipIf(sys.platform.startswith('win'), 415 'Test is not Windows compatible') 416 def test_get_makefile_filename(self): 417 makefile = sysconfig.get_makefile_filename() 418 self.assertTrue(os.path.isfile(makefile), makefile) 419 420 def test_parse_makefile(self): 421 self.addCleanup(unlink, TESTFN) 422 with open(TESTFN, "w") as makefile: 423 print("var1=a$(VAR2)", file=makefile) 424 print("VAR2=b$(var3)", file=makefile) 425 print("var3=42", file=makefile) 426 print("var4=$/invalid", file=makefile) 427 print("var5=dollar$$5", file=makefile) 428 print("var6=${var3}/lib/python3.5/config-$(VAR2)$(var5)" 429 "-x86_64-linux-gnu", file=makefile) 430 vars = sysconfig._parse_makefile(TESTFN) 431 self.assertEqual(vars, { 432 'var1': 'ab42', 433 'VAR2': 'b42', 434 'var3': 42, 435 'var4': '$/invalid', 436 'var5': 'dollar$5', 437 'var6': '42/lib/python3.5/config-b42dollar$5-x86_64-linux-gnu', 438 }) 439 440 441def test_main(): 442 run_unittest(TestSysConfig, MakefileTests) 443 444if __name__ == "__main__": 445 test_main() 446