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 PythonSymlink) 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 # macbook 124 os.name = 'posix' 125 sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) ' 126 '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]') 127 sys.platform = 'darwin' 128 self._set_uname(('Darwin', 'macziade', '8.11.1', 129 ('Darwin Kernel Version 8.11.1: ' 130 'Wed Oct 10 18:23:28 PDT 2007; ' 131 'root:xnu-792.25.20~1/RELEASE_I386'), 'PowerPC')) 132 _osx_support._remove_original_values(get_config_vars()) 133 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3' 134 135 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g ' 136 '-fwrapv -O3 -Wall -Wstrict-prototypes') 137 138 maxint = sys.maxsize 139 try: 140 sys.maxsize = 2147483647 141 self.assertEqual(get_platform(), 'macosx-10.3-ppc') 142 sys.maxsize = 9223372036854775807 143 self.assertEqual(get_platform(), 'macosx-10.3-ppc64') 144 finally: 145 sys.maxsize = maxint 146 147 self._set_uname(('Darwin', 'macziade', '8.11.1', 148 ('Darwin Kernel Version 8.11.1: ' 149 'Wed Oct 10 18:23:28 PDT 2007; ' 150 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386')) 151 _osx_support._remove_original_values(get_config_vars()) 152 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3' 153 154 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g ' 155 '-fwrapv -O3 -Wall -Wstrict-prototypes') 156 maxint = sys.maxsize 157 try: 158 sys.maxsize = 2147483647 159 self.assertEqual(get_platform(), 'macosx-10.3-i386') 160 sys.maxsize = 9223372036854775807 161 self.assertEqual(get_platform(), 'macosx-10.3-x86_64') 162 finally: 163 sys.maxsize = maxint 164 165 # macbook with fat binaries (fat, universal or fat64) 166 _osx_support._remove_original_values(get_config_vars()) 167 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.4' 168 get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot ' 169 '/Developer/SDKs/MacOSX10.4u.sdk ' 170 '-fno-strict-aliasing -fno-common ' 171 '-dynamic -DNDEBUG -g -O3') 172 173 self.assertEqual(get_platform(), 'macosx-10.4-fat') 174 175 _osx_support._remove_original_values(get_config_vars()) 176 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot ' 177 '/Developer/SDKs/MacOSX10.4u.sdk ' 178 '-fno-strict-aliasing -fno-common ' 179 '-dynamic -DNDEBUG -g -O3') 180 181 self.assertEqual(get_platform(), 'macosx-10.4-intel') 182 183 _osx_support._remove_original_values(get_config_vars()) 184 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot ' 185 '/Developer/SDKs/MacOSX10.4u.sdk ' 186 '-fno-strict-aliasing -fno-common ' 187 '-dynamic -DNDEBUG -g -O3') 188 self.assertEqual(get_platform(), 'macosx-10.4-fat3') 189 190 _osx_support._remove_original_values(get_config_vars()) 191 get_config_vars()['CFLAGS'] = ('-arch ppc64 -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-universal') 196 197 _osx_support._remove_original_values(get_config_vars()) 198 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -isysroot ' 199 '/Developer/SDKs/MacOSX10.4u.sdk ' 200 '-fno-strict-aliasing -fno-common ' 201 '-dynamic -DNDEBUG -g -O3') 202 203 self.assertEqual(get_platform(), 'macosx-10.4-fat64') 204 205 for arch in ('ppc', 'i386', 'x86_64', 'ppc64'): 206 _osx_support._remove_original_values(get_config_vars()) 207 get_config_vars()['CFLAGS'] = ('-arch %s -isysroot ' 208 '/Developer/SDKs/MacOSX10.4u.sdk ' 209 '-fno-strict-aliasing -fno-common ' 210 '-dynamic -DNDEBUG -g -O3' % arch) 211 212 self.assertEqual(get_platform(), 'macosx-10.4-%s' % arch) 213 214 # linux debian sarge 215 os.name = 'posix' 216 sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) ' 217 '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]') 218 sys.platform = 'linux2' 219 self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7', 220 '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686')) 221 222 self.assertEqual(get_platform(), 'linux-i686') 223 224 # XXX more platforms to tests here 225 226 def test_get_config_h_filename(self): 227 config_h = sysconfig.get_config_h_filename() 228 self.assertTrue(os.path.isfile(config_h), config_h) 229 230 def test_get_scheme_names(self): 231 wanted = ('nt', 'nt_user', 'osx_framework_user', 232 'posix_home', 'posix_prefix', 'posix_user') 233 self.assertEqual(get_scheme_names(), wanted) 234 235 @skip_unless_symlink 236 def test_symlink(self): # Issue 7880 237 with PythonSymlink() as py: 238 cmd = "-c", "import sysconfig; print(sysconfig.get_platform())" 239 self.assertEqual(py.call_real(*cmd), py.call_link(*cmd)) 240 241 def test_user_similar(self): 242 # Issue #8759: make sure the posix scheme for the users 243 # is similar to the global posix_prefix one 244 base = get_config_var('base') 245 user = get_config_var('userbase') 246 # the global scheme mirrors the distinction between prefix and 247 # exec-prefix but not the user scheme, so we have to adapt the paths 248 # before comparing (issue #9100) 249 adapt = sys.base_prefix != sys.base_exec_prefix 250 for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'): 251 global_path = get_path(name, 'posix_prefix') 252 if adapt: 253 global_path = global_path.replace(sys.exec_prefix, sys.base_prefix) 254 base = base.replace(sys.exec_prefix, sys.base_prefix) 255 elif sys.base_prefix != sys.prefix: 256 # virtual environment? Likewise, we have to adapt the paths 257 # before comparing 258 global_path = global_path.replace(sys.base_prefix, sys.prefix) 259 base = base.replace(sys.base_prefix, sys.prefix) 260 user_path = get_path(name, 'posix_user') 261 self.assertEqual(user_path, global_path.replace(base, user, 1)) 262 263 def test_main(self): 264 # just making sure _main() runs and returns things in the stdout 265 with captured_stdout() as output: 266 _main() 267 self.assertTrue(len(output.getvalue().split('\n')) > 0) 268 269 @unittest.skipIf(sys.platform == "win32", "Does not apply to Windows") 270 def test_ldshared_value(self): 271 ldflags = sysconfig.get_config_var('LDFLAGS') 272 ldshared = sysconfig.get_config_var('LDSHARED') 273 274 self.assertIn(ldflags, ldshared) 275 276 @unittest.skipUnless(sys.platform == "darwin", "test only relevant on MacOSX") 277 def test_platform_in_subprocess(self): 278 my_platform = sysconfig.get_platform() 279 280 # Test without MACOSX_DEPLOYMENT_TARGET in the environment 281 282 env = os.environ.copy() 283 if 'MACOSX_DEPLOYMENT_TARGET' in env: 284 del env['MACOSX_DEPLOYMENT_TARGET'] 285 286 p = subprocess.Popen([ 287 sys.executable, '-c', 288 'import sysconfig; print(sysconfig.get_platform())', 289 ], 290 stdout=subprocess.PIPE, 291 stderr=subprocess.DEVNULL, 292 env=env) 293 test_platform = p.communicate()[0].strip() 294 test_platform = test_platform.decode('utf-8') 295 status = p.wait() 296 297 self.assertEqual(status, 0) 298 self.assertEqual(my_platform, test_platform) 299 300 # Test with MACOSX_DEPLOYMENT_TARGET in the environment, and 301 # using a value that is unlikely to be the default one. 302 env = os.environ.copy() 303 env['MACOSX_DEPLOYMENT_TARGET'] = '10.1' 304 305 p = subprocess.Popen([ 306 sys.executable, '-c', 307 'import sysconfig; print(sysconfig.get_platform())', 308 ], 309 stdout=subprocess.PIPE, 310 stderr=subprocess.DEVNULL, 311 env=env) 312 test_platform = p.communicate()[0].strip() 313 test_platform = test_platform.decode('utf-8') 314 status = p.wait() 315 316 self.assertEqual(status, 0) 317 self.assertEqual(my_platform, test_platform) 318 319 def test_srcdir(self): 320 # See Issues #15322, #15364. 321 srcdir = sysconfig.get_config_var('srcdir') 322 323 self.assertTrue(os.path.isabs(srcdir), srcdir) 324 self.assertTrue(os.path.isdir(srcdir), srcdir) 325 326 if sysconfig._PYTHON_BUILD: 327 # The python executable has not been installed so srcdir 328 # should be a full source checkout. 329 Python_h = os.path.join(srcdir, 'Include', 'Python.h') 330 self.assertTrue(os.path.exists(Python_h), Python_h) 331 self.assertTrue(sysconfig._is_python_source_dir(srcdir)) 332 elif os.name == 'posix': 333 makefile_dir = os.path.dirname(sysconfig.get_makefile_filename()) 334 # Issue #19340: srcdir has been realpath'ed already 335 makefile_dir = os.path.realpath(makefile_dir) 336 self.assertEqual(makefile_dir, srcdir) 337 338 def test_srcdir_independent_of_cwd(self): 339 # srcdir should be independent of the current working directory 340 # See Issues #15322, #15364. 341 srcdir = sysconfig.get_config_var('srcdir') 342 with change_cwd(os.pardir): 343 srcdir2 = sysconfig.get_config_var('srcdir') 344 self.assertEqual(srcdir, srcdir2) 345 346 @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None, 347 'EXT_SUFFIX required for this test') 348 def test_SO_deprecation(self): 349 self.assertWarns(DeprecationWarning, 350 sysconfig.get_config_var, 'SO') 351 352 @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None, 353 'EXT_SUFFIX required for this test') 354 def test_SO_value(self): 355 with check_warnings(('', DeprecationWarning)): 356 self.assertEqual(sysconfig.get_config_var('SO'), 357 sysconfig.get_config_var('EXT_SUFFIX')) 358 359 @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None, 360 'EXT_SUFFIX required for this test') 361 def test_SO_in_vars(self): 362 vars = sysconfig.get_config_vars() 363 self.assertIsNotNone(vars['SO']) 364 self.assertEqual(vars['SO'], vars['EXT_SUFFIX']) 365 366 @unittest.skipUnless(sys.platform == 'linux' and 367 hasattr(sys.implementation, '_multiarch'), 368 'multiarch-specific test') 369 def test_triplet_in_ext_suffix(self): 370 ctypes = import_module('ctypes') 371 import platform, re 372 machine = platform.machine() 373 suffix = sysconfig.get_config_var('EXT_SUFFIX') 374 if re.match('(aarch64|arm|mips|ppc|powerpc|s390|sparc)', machine): 375 self.assertTrue('linux' in suffix, suffix) 376 if re.match('(i[3-6]86|x86_64)$', machine): 377 if ctypes.sizeof(ctypes.c_char_p()) == 4: 378 self.assertTrue(suffix.endswith('i386-linux-gnu.so') or 379 suffix.endswith('x86_64-linux-gnux32.so'), 380 suffix) 381 else: # 8 byte pointer size 382 self.assertTrue(suffix.endswith('x86_64-linux-gnu.so'), suffix) 383 384 @unittest.skipUnless(sys.platform == 'darwin', 'OS X-specific test') 385 def test_osx_ext_suffix(self): 386 suffix = sysconfig.get_config_var('EXT_SUFFIX') 387 self.assertTrue(suffix.endswith('-darwin.so'), suffix) 388 389class MakefileTests(unittest.TestCase): 390 391 @unittest.skipIf(sys.platform.startswith('win'), 392 'Test is not Windows compatible') 393 def test_get_makefile_filename(self): 394 makefile = sysconfig.get_makefile_filename() 395 self.assertTrue(os.path.isfile(makefile), makefile) 396 397 def test_parse_makefile(self): 398 self.addCleanup(unlink, TESTFN) 399 with open(TESTFN, "w") as makefile: 400 print("var1=a$(VAR2)", file=makefile) 401 print("VAR2=b$(var3)", file=makefile) 402 print("var3=42", file=makefile) 403 print("var4=$/invalid", file=makefile) 404 print("var5=dollar$$5", file=makefile) 405 print("var6=${var3}/lib/python3.5/config-$(VAR2)$(var5)" 406 "-x86_64-linux-gnu", file=makefile) 407 vars = sysconfig._parse_makefile(TESTFN) 408 self.assertEqual(vars, { 409 'var1': 'ab42', 410 'VAR2': 'b42', 411 'var3': 42, 412 'var4': '$/invalid', 413 'var5': 'dollar$5', 414 'var6': '42/lib/python3.5/config-b42dollar$5-x86_64-linux-gnu', 415 }) 416 417 418if __name__ == "__main__": 419 unittest.main() 420