1"""Tests for sysconfig.""" 2 3import unittest 4import sys 5import os 6import shutil 7import subprocess 8from copy import copy, deepcopy 9 10from test.test_support import run_unittest, TESTFN, unlink, get_attribute 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) 17import _osx_support 18 19class TestSysConfig(unittest.TestCase): 20 21 def setUp(self): 22 """Make a copy of sys.path""" 23 super(TestSysConfig, self).setUp() 24 self.sys_path = sys.path[:] 25 self.makefile = None 26 # patching os.uname 27 if hasattr(os, 'uname'): 28 self.uname = os.uname 29 self._uname = os.uname() 30 else: 31 self.uname = None 32 self._uname = None 33 os.uname = self._get_uname 34 # saving the environment 35 self.name = os.name 36 self.platform = sys.platform 37 self.version = sys.version 38 self.sep = os.sep 39 self.join = os.path.join 40 self.isabs = os.path.isabs 41 self.splitdrive = os.path.splitdrive 42 self._config_vars = copy(sysconfig._CONFIG_VARS) 43 self.old_environ = deepcopy(os.environ) 44 45 def tearDown(self): 46 """Restore sys.path""" 47 sys.path[:] = self.sys_path 48 if self.makefile is not None: 49 os.unlink(self.makefile) 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 = copy(self._config_vars) 63 for key, value in self.old_environ.items(): 64 if os.environ.get(key) != value: 65 os.environ[key] = value 66 67 for key in os.environ.keys(): 68 if key not in self.old_environ: 69 del os.environ[key] 70 71 super(TestSysConfig, self).tearDown() 72 73 def _set_uname(self, uname): 74 self._uname = 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 = wanted.items() 94 wanted.sort() 95 scheme = scheme.items() 96 scheme.sort() 97 self.assertEqual(scheme, wanted) 98 99 def test_get_path(self): 100 # xxx make real tests here 101 for scheme in _INSTALL_SCHEMES: 102 for name in _INSTALL_SCHEMES[scheme]: 103 res = get_path(name, scheme) 104 105 def test_get_config_vars(self): 106 cvars = get_config_vars() 107 self.assertIsInstance(cvars, dict) 108 self.assertTrue(cvars) 109 110 def test_get_platform(self): 111 # windows XP, 32bits 112 os.name = 'nt' 113 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) ' 114 '[MSC v.1310 32 bit (Intel)]') 115 sys.platform = 'win32' 116 self.assertEqual(get_platform(), 'win32') 117 118 # windows XP, amd64 119 os.name = 'nt' 120 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) ' 121 '[MSC v.1310 32 bit (Amd64)]') 122 sys.platform = 'win32' 123 self.assertEqual(get_platform(), 'win-amd64') 124 125 # windows XP, itanium 126 os.name = 'nt' 127 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) ' 128 '[MSC v.1310 32 bit (Itanium)]') 129 sys.platform = 'win32' 130 self.assertEqual(get_platform(), 'win-ia64') 131 132 # macbook 133 os.name = 'posix' 134 sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) ' 135 '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]') 136 sys.platform = 'darwin' 137 self._set_uname(('Darwin', 'macziade', '8.11.1', 138 ('Darwin Kernel Version 8.11.1: ' 139 'Wed Oct 10 18:23:28 PDT 2007; ' 140 'root:xnu-792.25.20~1/RELEASE_I386'), 'PowerPC')) 141 _osx_support._remove_original_values(get_config_vars()) 142 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3' 143 144 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g ' 145 '-fwrapv -O3 -Wall -Wstrict-prototypes') 146 147 maxint = sys.maxint 148 try: 149 sys.maxint = 2147483647 150 self.assertEqual(get_platform(), 'macosx-10.3-ppc') 151 sys.maxint = 9223372036854775807 152 self.assertEqual(get_platform(), 'macosx-10.3-ppc64') 153 finally: 154 sys.maxint = maxint 155 156 157 self._set_uname(('Darwin', 'macziade', '8.11.1', 158 ('Darwin Kernel Version 8.11.1: ' 159 'Wed Oct 10 18:23:28 PDT 2007; ' 160 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386')) 161 _osx_support._remove_original_values(get_config_vars()) 162 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3' 163 164 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g ' 165 '-fwrapv -O3 -Wall -Wstrict-prototypes') 166 167 maxint = sys.maxint 168 try: 169 sys.maxint = 2147483647 170 self.assertEqual(get_platform(), 'macosx-10.3-i386') 171 sys.maxint = 9223372036854775807 172 self.assertEqual(get_platform(), 'macosx-10.3-x86_64') 173 finally: 174 sys.maxint = maxint 175 176 # macbook with fat binaries (fat, universal or fat64) 177 _osx_support._remove_original_values(get_config_vars()) 178 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.4' 179 get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot ' 180 '/Developer/SDKs/MacOSX10.4u.sdk ' 181 '-fno-strict-aliasing -fno-common ' 182 '-dynamic -DNDEBUG -g -O3') 183 184 self.assertEqual(get_platform(), 'macosx-10.4-fat') 185 186 _osx_support._remove_original_values(get_config_vars()) 187 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot ' 188 '/Developer/SDKs/MacOSX10.4u.sdk ' 189 '-fno-strict-aliasing -fno-common ' 190 '-dynamic -DNDEBUG -g -O3') 191 192 self.assertEqual(get_platform(), 'macosx-10.4-intel') 193 194 _osx_support._remove_original_values(get_config_vars()) 195 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot ' 196 '/Developer/SDKs/MacOSX10.4u.sdk ' 197 '-fno-strict-aliasing -fno-common ' 198 '-dynamic -DNDEBUG -g -O3') 199 self.assertEqual(get_platform(), 'macosx-10.4-fat3') 200 201 _osx_support._remove_original_values(get_config_vars()) 202 get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot ' 203 '/Developer/SDKs/MacOSX10.4u.sdk ' 204 '-fno-strict-aliasing -fno-common ' 205 '-dynamic -DNDEBUG -g -O3') 206 self.assertEqual(get_platform(), 'macosx-10.4-universal') 207 208 _osx_support._remove_original_values(get_config_vars()) 209 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -isysroot ' 210 '/Developer/SDKs/MacOSX10.4u.sdk ' 211 '-fno-strict-aliasing -fno-common ' 212 '-dynamic -DNDEBUG -g -O3') 213 214 self.assertEqual(get_platform(), 'macosx-10.4-fat64') 215 216 for arch in ('ppc', 'i386', 'x86_64', 'ppc64'): 217 _osx_support._remove_original_values(get_config_vars()) 218 get_config_vars()['CFLAGS'] = ('-arch %s -isysroot ' 219 '/Developer/SDKs/MacOSX10.4u.sdk ' 220 '-fno-strict-aliasing -fno-common ' 221 '-dynamic -DNDEBUG -g -O3'%(arch,)) 222 223 self.assertEqual(get_platform(), 'macosx-10.4-%s'%(arch,)) 224 225 # linux debian sarge 226 os.name = 'posix' 227 sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) ' 228 '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]') 229 sys.platform = 'linux2' 230 self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7', 231 '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686')) 232 233 self.assertEqual(get_platform(), 'linux-i686') 234 235 # XXX more platforms to tests here 236 237 def test_get_config_h_filename(self): 238 config_h = sysconfig.get_config_h_filename() 239 self.assertTrue(os.path.isfile(config_h), config_h) 240 241 def test_get_scheme_names(self): 242 wanted = ('nt', 'nt_user', 'os2', 'os2_home', 'osx_framework_user', 243 'posix_home', 'posix_prefix', 'posix_user') 244 self.assertEqual(get_scheme_names(), wanted) 245 246 @unittest.skipIf(sys.platform.startswith('win'), 247 'Test is not Windows compatible') 248 def test_get_makefile_filename(self): 249 makefile = sysconfig.get_makefile_filename() 250 self.assertTrue(os.path.isfile(makefile), makefile) 251 # Issue 22199 252 self.assertEqual(sysconfig._get_makefile_filename(), makefile) 253 254 def test_symlink(self): 255 # Issue 7880 256 symlink = get_attribute(os, "symlink") 257 def get(python): 258 cmd = [python, '-c', 259 'import sysconfig; print sysconfig.get_platform()'] 260 p = subprocess.Popen(cmd, stdout=subprocess.PIPE) 261 return p.communicate() 262 real = os.path.realpath(sys.executable) 263 link = os.path.abspath(TESTFN) 264 symlink(real, link) 265 try: 266 self.assertEqual(get(real), get(link)) 267 finally: 268 unlink(link) 269 270 def test_user_similar(self): 271 # Issue #8759: make sure the posix scheme for the users 272 # is similar to the global posix_prefix one 273 base = get_config_var('base') 274 user = get_config_var('userbase') 275 # the global scheme mirrors the distinction between prefix and 276 # exec-prefix but not the user scheme, so we have to adapt the paths 277 # before comparing (issue #9100) 278 adapt = sys.prefix != sys.exec_prefix 279 for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'): 280 global_path = get_path(name, 'posix_prefix') 281 if adapt: 282 global_path = global_path.replace(sys.exec_prefix, sys.prefix) 283 base = base.replace(sys.exec_prefix, sys.prefix) 284 user_path = get_path(name, 'posix_user') 285 self.assertEqual(user_path, global_path.replace(base, user, 1)) 286 287 @unittest.skipUnless(sys.platform == "darwin", "test only relevant on MacOSX") 288 def test_platform_in_subprocess(self): 289 my_platform = sysconfig.get_platform() 290 291 # Test without MACOSX_DEPLOYMENT_TARGET in the environment 292 293 env = os.environ.copy() 294 if 'MACOSX_DEPLOYMENT_TARGET' in env: 295 del env['MACOSX_DEPLOYMENT_TARGET'] 296 297 with open('/dev/null', 'w') as devnull_fp: 298 p = subprocess.Popen([ 299 sys.executable, '-c', 300 'import sysconfig; print(sysconfig.get_platform())', 301 ], 302 stdout=subprocess.PIPE, 303 stderr=devnull_fp, 304 env=env) 305 test_platform = p.communicate()[0].strip() 306 test_platform = test_platform.decode('utf-8') 307 status = p.wait() 308 309 self.assertEqual(status, 0) 310 self.assertEqual(my_platform, test_platform) 311 312 313 # Test with MACOSX_DEPLOYMENT_TARGET in the environment, and 314 # using a value that is unlikely to be the default one. 315 env = os.environ.copy() 316 env['MACOSX_DEPLOYMENT_TARGET'] = '10.1' 317 318 p = subprocess.Popen([ 319 sys.executable, '-c', 320 'import sysconfig; print(sysconfig.get_platform())', 321 ], 322 stdout=subprocess.PIPE, 323 stderr=open('/dev/null'), 324 env=env) 325 test_platform = p.communicate()[0].strip() 326 test_platform = test_platform.decode('utf-8') 327 status = p.wait() 328 329 self.assertEqual(status, 0) 330 self.assertEqual(my_platform, test_platform) 331 332def test_main(): 333 run_unittest(TestSysConfig) 334 335if __name__ == "__main__": 336 test_main() 337