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