1 """Tests for sysconfig.""" 2 3 import unittest 4 import sys 5 import os 6 import shutil 7 import subprocess 8 from copy import copy, deepcopy 9 10 from test.test_support import run_unittest, TESTFN, unlink, get_attribute 11 12 import sysconfig 13 from 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) 17 import _osx_support 18 19 class 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 def test_symlink(self): 247 # Issue 7880 248 symlink = get_attribute(os, "symlink") 249 def get(python): 250 cmd = [python, '-c', 251 'import sysconfig; print sysconfig.get_platform()'] 252 p = subprocess.Popen(cmd, stdout=subprocess.PIPE) 253 return p.communicate() 254 real = os.path.realpath(sys.executable) 255 link = os.path.abspath(TESTFN) 256 symlink(real, link) 257 try: 258 self.assertEqual(get(real), get(link)) 259 finally: 260 unlink(link) 261 262 def test_user_similar(self): 263 # Issue #8759: make sure the posix scheme for the users 264 # is similar to the global posix_prefix one 265 base = get_config_var('base') 266 user = get_config_var('userbase') 267 # the global scheme mirrors the distinction between prefix and 268 # exec-prefix but not the user scheme, so we have to adapt the paths 269 # before comparing (issue #9100) 270 adapt = sys.prefix != sys.exec_prefix 271 for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'): 272 global_path = get_path(name, 'posix_prefix') 273 if adapt: 274 global_path = global_path.replace(sys.exec_prefix, sys.prefix) 275 base = base.replace(sys.exec_prefix, sys.prefix) 276 user_path = get_path(name, 'posix_user') 277 self.assertEqual(user_path, global_path.replace(base, user, 1)) 278 279 @unittest.skipUnless(sys.platform == "darwin", "test only relevant on MacOSX") 280 def test_platform_in_subprocess(self): 281 my_platform = sysconfig.get_platform() 282 283 # Test without MACOSX_DEPLOYMENT_TARGET in the environment 284 285 env = os.environ.copy() 286 if 'MACOSX_DEPLOYMENT_TARGET' in env: 287 del env['MACOSX_DEPLOYMENT_TARGET'] 288 289 with open('/dev/null', 'w') as devnull_fp: 290 p = subprocess.Popen([ 291 sys.executable, '-c', 292 'import sysconfig; print(sysconfig.get_platform())', 293 ], 294 stdout=subprocess.PIPE, 295 stderr=devnull_fp, 296 env=env) 297 test_platform = p.communicate()[0].strip() 298 test_platform = test_platform.decode('utf-8') 299 status = p.wait() 300 301 self.assertEqual(status, 0) 302 self.assertEqual(my_platform, test_platform) 303 304 305 # Test with MACOSX_DEPLOYMENT_TARGET in the environment, and 306 # using a value that is unlikely to be the default one. 307 env = os.environ.copy() 308 env['MACOSX_DEPLOYMENT_TARGET'] = '10.1' 309 310 p = subprocess.Popen([ 311 sys.executable, '-c', 312 'import sysconfig; print(sysconfig.get_platform())', 313 ], 314 stdout=subprocess.PIPE, 315 stderr=open('/dev/null'), 316 env=env) 317 test_platform = p.communicate()[0].strip() 318 test_platform = test_platform.decode('utf-8') 319 status = p.wait() 320 321 self.assertEqual(status, 0) 322 self.assertEqual(my_platform, test_platform) 323 324 def test_main(): 325 run_unittest(TestSysConfig) 326 327 if __name__ == "__main__": 328 test_main() 329