blob: 77c23648b18b3a9ee0f39aef8adeb4a718d8c1dc [file] [log] [blame]
Tarek Ziadéedacea32010-01-29 11:41:03 +00001import unittest
2import sys
Tarek Ziadéedacea32010-01-29 11:41:03 +00003import os
Florent Xiclunaa4707382010-03-11 00:05:17 +00004import subprocess
Georg Brandl89fad142010-03-14 10:23:39 +00005import shutil
Tarek Ziadéedacea32010-01-29 11:41:03 +00006from copy import copy, deepcopy
7
Tarek Ziadéa7514992010-05-25 09:44:36 +00008from test.support import (run_unittest, TESTFN, unlink, get_attribute,
Brian Curtin3b4499c2010-12-28 14:31:47 +00009 captured_stdout, skip_unless_symlink)
Tarek Ziadéedacea32010-01-29 11:41:03 +000010
11import sysconfig
12from sysconfig import (get_paths, get_platform, get_config_vars,
Tarek Ziade1231a4e2011-05-19 13:07:25 +020013 get_path, get_path_names, _SCHEMES,
Tarek Ziadébd797682010-02-02 23:16:13 +000014 _get_default_scheme, _expand_vars,
Tarek Ziadéa7514992010-05-25 09:44:36 +000015 get_scheme_names, get_config_var, _main)
Tarek Ziadéedacea32010-01-29 11:41:03 +000016
17class TestSysConfig(unittest.TestCase):
18
19 def setUp(self):
Tarek Ziadéedacea32010-01-29 11:41:03 +000020 super(TestSysConfig, self).setUp()
21 self.sys_path = sys.path[:]
22 self.makefile = None
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._uname = None
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 = copy(sysconfig._CONFIG_VARS)
Tarek Ziade1231a4e2011-05-19 13:07:25 +020040 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)
Tarek Ziadéedacea32010-01-29 11:41:03 +000047
48 def tearDown(self):
Tarek Ziadéedacea32010-01-29 11:41:03 +000049 sys.path[:] = self.sys_path
50 if self.makefile is not None:
51 os.unlink(self.makefile)
52 self._cleanup_testfn()
53 if self.uname is not None:
54 os.uname = self.uname
55 else:
56 del os.uname
57 os.name = self.name
58 sys.platform = self.platform
59 sys.version = self.version
60 os.sep = self.sep
61 os.path.join = self.join
62 os.path.isabs = self.isabs
63 os.path.splitdrive = self.splitdrive
64 sysconfig._CONFIG_VARS = copy(self._config_vars)
Tarek Ziade1231a4e2011-05-19 13:07:25 +020065 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)
Tarek Ziadéedacea32010-01-29 11:41:03 +000069
70 super(TestSysConfig, self).tearDown()
71
72 def _set_uname(self, uname):
73 self._uname = 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):
Tarek Ziade1231a4e2011-05-19 13:07:25 +020086 self.assertEqual(get_path_names(), _SCHEMES.options('posix_prefix'))
Tarek Ziadéedacea32010-01-29 11:41:03 +000087
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 = list(wanted.items())
93 wanted.sort()
94 scheme = list(scheme.items())
95 scheme.sort()
Ezio Melottib3aedd42010-11-20 19:04:17 +000096 self.assertEqual(scheme, wanted)
Tarek Ziadéedacea32010-01-29 11:41:03 +000097
98 def test_get_path(self):
99 # xxx make real tests here
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200100 for scheme in _SCHEMES:
101 for name in _SCHEMES[scheme]:
Tarek Ziadéedacea32010-01-29 11:41:03 +0000102 res = get_path(name, scheme)
103
104 def test_get_config_vars(self):
105 cvars = get_config_vars()
106 self.assertTrue(isinstance(cvars, dict))
107 self.assertTrue(cvars)
108
109 def test_get_platform(self):
110 # windows XP, 32bits
111 os.name = 'nt'
112 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
113 '[MSC v.1310 32 bit (Intel)]')
114 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000115 self.assertEqual(get_platform(), 'win32')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000116
117 # windows XP, amd64
118 os.name = 'nt'
119 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
120 '[MSC v.1310 32 bit (Amd64)]')
121 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000122 self.assertEqual(get_platform(), 'win-amd64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000123
124 # windows XP, itanium
125 os.name = 'nt'
126 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
127 '[MSC v.1310 32 bit (Itanium)]')
128 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000129 self.assertEqual(get_platform(), 'win-ia64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000130
131 # macbook
132 os.name = 'posix'
133 sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) '
134 '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]')
135 sys.platform = 'darwin'
136 self._set_uname(('Darwin', 'macziade', '8.11.1',
137 ('Darwin Kernel Version 8.11.1: '
138 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000139 'root:xnu-792.25.20~1/RELEASE_I386'), 'PowerPC'))
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200140 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000141
142 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
143 '-fwrapv -O3 -Wall -Wstrict-prototypes')
144
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000145 maxint = sys.maxsize
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000146 try:
147 sys.maxsize = 2147483647
Ezio Melottib3aedd42010-11-20 19:04:17 +0000148 self.assertEqual(get_platform(), 'macosx-10.3-ppc')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000149 sys.maxsize = 9223372036854775807
Ezio Melottib3aedd42010-11-20 19:04:17 +0000150 self.assertEqual(get_platform(), 'macosx-10.3-ppc64')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000151 finally:
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000152 sys.maxsize = maxint
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000153
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000154 self._set_uname(('Darwin', 'macziade', '8.11.1',
155 ('Darwin Kernel Version 8.11.1: '
156 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadéedacea32010-01-29 11:41:03 +0000157 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386'))
158 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200159 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Tarek Ziadéedacea32010-01-29 11:41:03 +0000160
161 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
162 '-fwrapv -O3 -Wall -Wstrict-prototypes')
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000163 maxint = sys.maxsize
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000164 try:
165 sys.maxsize = 2147483647
Ezio Melottib3aedd42010-11-20 19:04:17 +0000166 self.assertEqual(get_platform(), 'macosx-10.3-i386')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000167 sys.maxsize = 9223372036854775807
Ezio Melottib3aedd42010-11-20 19:04:17 +0000168 self.assertEqual(get_platform(), 'macosx-10.3-x86_64')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000169 finally:
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000170 sys.maxsize = maxint
Tarek Ziadéedacea32010-01-29 11:41:03 +0000171
172 # macbook with fat binaries (fat, universal or fat64)
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200173 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.4'
Tarek Ziadéedacea32010-01-29 11:41:03 +0000174 get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot '
175 '/Developer/SDKs/MacOSX10.4u.sdk '
176 '-fno-strict-aliasing -fno-common '
177 '-dynamic -DNDEBUG -g -O3')
178
Ezio Melottib3aedd42010-11-20 19:04:17 +0000179 self.assertEqual(get_platform(), 'macosx-10.4-fat')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000180
181 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot '
182 '/Developer/SDKs/MacOSX10.4u.sdk '
183 '-fno-strict-aliasing -fno-common '
184 '-dynamic -DNDEBUG -g -O3')
185
Ezio Melottib3aedd42010-11-20 19:04:17 +0000186 self.assertEqual(get_platform(), 'macosx-10.4-intel')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000187
188 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot '
189 '/Developer/SDKs/MacOSX10.4u.sdk '
190 '-fno-strict-aliasing -fno-common '
191 '-dynamic -DNDEBUG -g -O3')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000192 self.assertEqual(get_platform(), 'macosx-10.4-fat3')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000193
194 get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot '
195 '/Developer/SDKs/MacOSX10.4u.sdk '
196 '-fno-strict-aliasing -fno-common '
197 '-dynamic -DNDEBUG -g -O3')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000198 self.assertEqual(get_platform(), 'macosx-10.4-universal')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000199
200 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -isysroot '
201 '/Developer/SDKs/MacOSX10.4u.sdk '
202 '-fno-strict-aliasing -fno-common '
203 '-dynamic -DNDEBUG -g -O3')
204
Ezio Melottib3aedd42010-11-20 19:04:17 +0000205 self.assertEqual(get_platform(), 'macosx-10.4-fat64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000206
207 for arch in ('ppc', 'i386', 'x86_64', 'ppc64'):
208 get_config_vars()['CFLAGS'] = ('-arch %s -isysroot '
209 '/Developer/SDKs/MacOSX10.4u.sdk '
210 '-fno-strict-aliasing -fno-common '
211 '-dynamic -DNDEBUG -g -O3'%(arch,))
212
Ezio Melottib3aedd42010-11-20 19:04:17 +0000213 self.assertEqual(get_platform(), 'macosx-10.4-%s'%(arch,))
Tarek Ziadéedacea32010-01-29 11:41:03 +0000214
215 # linux debian sarge
216 os.name = 'posix'
217 sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) '
218 '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]')
219 sys.platform = 'linux2'
220 self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7',
221 '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686'))
222
Ezio Melottib3aedd42010-11-20 19:04:17 +0000223 self.assertEqual(get_platform(), 'linux-i686')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000224
225 # XXX more platforms to tests here
226
227 def test_get_config_h_filename(self):
228 config_h = sysconfig.get_config_h_filename()
229 self.assertTrue(os.path.isfile(config_h), config_h)
230
Barry Warsaw0646b4b2010-09-20 19:12:07 +0000231 @unittest.skipIf(sys.platform.startswith('win'),
232 'Test is not Windows compatible')
Barry Warsawebbef6f2010-09-20 15:29:53 +0000233 def test_get_makefile_filename(self):
234 makefile = sysconfig.get_makefile_filename()
235 self.assertTrue(os.path.isfile(makefile), makefile)
236
Tarek Ziadébd797682010-02-02 23:16:13 +0000237 def test_get_scheme_names(self):
Benjamin Peterson3c451e62010-05-08 15:51:23 +0000238 wanted = ('nt', 'nt_user', 'os2', 'os2_home', 'osx_framework_user',
239 'posix_home', 'posix_prefix', 'posix_user')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000240 self.assertEqual(get_scheme_names(), wanted)
Tarek Ziadébd797682010-02-02 23:16:13 +0000241
Brian Curtin3b4499c2010-12-28 14:31:47 +0000242 @skip_unless_symlink
Florent Xiclunaa4707382010-03-11 00:05:17 +0000243 def test_symlink(self):
Brian Curtind40e6f72010-07-08 21:39:08 +0000244 # 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":
Brian Curtin74e45612010-07-09 15:58:59 +0000247 os.environ["Path"] = "{};{}".format(
248 os.path.dirname(sys.executable), os.environ["Path"])
Brian Curtind40e6f72010-07-08 21:39:08 +0000249
Florent Xiclunaa4707382010-03-11 00:05:17 +0000250 # Issue 7880
Florent Xiclunaa4707382010-03-11 00:05:17 +0000251 def get(python):
252 cmd = [python, '-c',
Florent Xicluna01fe6102010-03-13 15:35:12 +0000253 'import sysconfig; print(sysconfig.get_platform())']
Brian Curtind40e6f72010-07-08 21:39:08 +0000254 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=os.environ)
Florent Xiclunaa4707382010-03-11 00:05:17 +0000255 return p.communicate()
256 real = os.path.realpath(sys.executable)
257 link = os.path.abspath(TESTFN)
Brian Curtind40e6f72010-07-08 21:39:08 +0000258 os.symlink(real, link)
Florent Xiclunaa4707382010-03-11 00:05:17 +0000259 try:
260 self.assertEqual(get(real), get(link))
261 finally:
262 unlink(link)
263
Tarek Ziadé06710a82010-05-19 22:25:00 +0000264 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 for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'):
270 global_path = get_path(name, 'posix_prefix')
271 user_path = get_path(name, 'posix_user')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000272 self.assertEqual(user_path, global_path.replace(base, user))
Tarek Ziadéedacea32010-01-29 11:41:03 +0000273
Tarek Ziadéa7514992010-05-25 09:44:36 +0000274 def test_main(self):
275 # just making sure _main() runs and returns things in the stdout
276 with captured_stdout() as output:
277 _main()
278 self.assertTrue(len(output.getvalue().split('\n')) > 0)
279
Brian Curtindb902ac2010-07-22 15:38:28 +0000280 @unittest.skipIf(sys.platform == "win32", "Does not apply to Windows")
Ronald Oussorenf4ebe2e2010-07-19 13:00:36 +0000281 def test_ldshared_value(self):
282 ldflags = sysconfig.get_config_var('LDFLAGS')
283 ldshared = sysconfig.get_config_var('LDSHARED')
284
285 self.assertIn(ldflags, ldshared)
286
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200287 @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
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200312 # Test with MACOSX_DEPLOYMENT_TARGET in the environment, and
313 # using a value that is unlikely to be the default one.
314 env = os.environ.copy()
315 env['MACOSX_DEPLOYMENT_TARGET'] = '10.1'
316
317 p = subprocess.Popen([
318 sys.executable, '-c',
319 'import sysconfig; print(sysconfig.get_platform())',
320 ],
321 stdout=subprocess.PIPE,
322 stderr=open('/dev/null'),
323 env=env)
324 test_platform = p.communicate()[0].strip()
325 test_platform = test_platform.decode('utf-8')
326 status = p.wait()
327
328 self.assertEqual(status, 0)
329 self.assertEqual(my_platform, test_platform)
330
331
Tarek Ziadéedacea32010-01-29 11:41:03 +0000332def test_main():
333 run_unittest(TestSysConfig)
334
335if __name__ == "__main__":
336 test_main()