blob: a2e6fbcfd6a85b12b3c77fffb00e4b5514d34e2c [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
Éric Araujo6ebea152011-10-08 02:57:45 +02006from copy import copy
Tarek Ziadéedacea32010-01-29 11:41:03 +00007
Éric Araujode504552011-10-08 01:55:07 +02008from test.support import (run_unittest, TESTFN, unlink,
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
Éric Araujof46676d2011-05-26 16:35:14 +020017
Tarek Ziadéedacea32010-01-29 11:41:03 +000018class TestSysConfig(unittest.TestCase):
19
20 def setUp(self):
Tarek Ziadéedacea32010-01-29 11:41:03 +000021 super(TestSysConfig, self).setUp()
22 self.sys_path = sys.path[:]
Tarek Ziadéedacea32010-01-29 11:41:03 +000023 # 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
Benjamin Petersond710d142012-01-04 10:12:14 -060039 self._config_vars = sysconfig._CONFIG_VARS
40 sysconfig._CONFIG_VARS = copy(sysconfig._CONFIG_VARS)
Tarek Ziade1231a4e2011-05-19 13:07:25 +020041 self._added_envvars = []
42 self._changed_envvars = []
Éric Araujoc1b7e7f2011-09-18 23:12:30 +020043 for var in ('MACOSX_DEPLOYMENT_TARGET', 'PATH'):
Tarek Ziade1231a4e2011-05-19 13:07:25 +020044 if var in os.environ:
45 self._changed_envvars.append((var, os.environ[var]))
46 else:
47 self._added_envvars.append(var)
Tarek Ziadéedacea32010-01-29 11:41:03 +000048
49 def tearDown(self):
Tarek Ziadéedacea32010-01-29 11:41:03 +000050 sys.path[:] = self.sys_path
Tarek Ziadéedacea32010-01-29 11:41:03 +000051 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
Benjamin Petersond710d142012-01-04 10:12:14 -060063 sysconfig._CONFIG_VARS = self._config_vars
Tarek Ziade1231a4e2011-05-19 13:07:25 +020064 for var, value in self._changed_envvars:
65 os.environ[var] = value
66 for var in self._added_envvars:
67 os.environ.pop(var, None)
Tarek Ziadéedacea32010-01-29 11:41:03 +000068
69 super(TestSysConfig, self).tearDown()
70
71 def _set_uname(self, uname):
72 self._uname = uname
73
74 def _get_uname(self):
75 return self._uname
76
77 def _cleanup_testfn(self):
78 path = TESTFN
79 if os.path.isfile(path):
80 os.remove(path)
81 elif os.path.isdir(path):
82 shutil.rmtree(path)
83
84 def test_get_path_names(self):
Tarek Ziade1231a4e2011-05-19 13:07:25 +020085 self.assertEqual(get_path_names(), _SCHEMES.options('posix_prefix'))
Tarek Ziadéedacea32010-01-29 11:41:03 +000086
87 def test_get_paths(self):
88 scheme = get_paths()
89 default_scheme = _get_default_scheme()
90 wanted = _expand_vars(default_scheme, None)
Éric Araujoc1b7e7f2011-09-18 23:12:30 +020091 wanted = sorted(wanted.items())
92 scheme = sorted(scheme.items())
Ezio Melottib3aedd42010-11-20 19:04:17 +000093 self.assertEqual(scheme, wanted)
Tarek Ziadéedacea32010-01-29 11:41:03 +000094
95 def test_get_path(self):
Éric Araujoc1b7e7f2011-09-18 23:12:30 +020096 # XXX make real tests here
Tarek Ziade1231a4e2011-05-19 13:07:25 +020097 for scheme in _SCHEMES:
98 for name in _SCHEMES[scheme]:
Tarek Ziadéedacea32010-01-29 11:41:03 +000099 res = get_path(name, scheme)
100
101 def test_get_config_vars(self):
102 cvars = get_config_vars()
Éric Araujoc1b7e7f2011-09-18 23:12:30 +0200103 self.assertIsInstance(cvars, dict)
Tarek Ziadéedacea32010-01-29 11:41:03 +0000104 self.assertTrue(cvars)
105
106 def test_get_platform(self):
107 # windows XP, 32bits
108 os.name = 'nt'
109 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
110 '[MSC v.1310 32 bit (Intel)]')
111 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000112 self.assertEqual(get_platform(), 'win32')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000113
114 # windows XP, amd64
115 os.name = 'nt'
116 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
117 '[MSC v.1310 32 bit (Amd64)]')
118 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000119 self.assertEqual(get_platform(), 'win-amd64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000120
121 # windows XP, itanium
122 os.name = 'nt'
123 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
124 '[MSC v.1310 32 bit (Itanium)]')
125 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000126 self.assertEqual(get_platform(), 'win-ia64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000127
128 # macbook
129 os.name = 'posix'
130 sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) '
131 '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]')
132 sys.platform = 'darwin'
133 self._set_uname(('Darwin', 'macziade', '8.11.1',
134 ('Darwin Kernel Version 8.11.1: '
135 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000136 'root:xnu-792.25.20~1/RELEASE_I386'), 'PowerPC'))
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200137 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000138
139 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
140 '-fwrapv -O3 -Wall -Wstrict-prototypes')
141
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000142 maxint = sys.maxsize
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000143 try:
144 sys.maxsize = 2147483647
Ezio Melottib3aedd42010-11-20 19:04:17 +0000145 self.assertEqual(get_platform(), 'macosx-10.3-ppc')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000146 sys.maxsize = 9223372036854775807
Ezio Melottib3aedd42010-11-20 19:04:17 +0000147 self.assertEqual(get_platform(), 'macosx-10.3-ppc64')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000148 finally:
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000149 sys.maxsize = maxint
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000150
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000151 self._set_uname(('Darwin', 'macziade', '8.11.1',
152 ('Darwin Kernel Version 8.11.1: '
153 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadéedacea32010-01-29 11:41:03 +0000154 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386'))
155 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200156 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Tarek Ziadéedacea32010-01-29 11:41:03 +0000157
158 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
159 '-fwrapv -O3 -Wall -Wstrict-prototypes')
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000160 maxint = sys.maxsize
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000161 try:
162 sys.maxsize = 2147483647
Ezio Melottib3aedd42010-11-20 19:04:17 +0000163 self.assertEqual(get_platform(), 'macosx-10.3-i386')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000164 sys.maxsize = 9223372036854775807
Ezio Melottib3aedd42010-11-20 19:04:17 +0000165 self.assertEqual(get_platform(), 'macosx-10.3-x86_64')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000166 finally:
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000167 sys.maxsize = maxint
Tarek Ziadéedacea32010-01-29 11:41:03 +0000168
169 # macbook with fat binaries (fat, universal or fat64)
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200170 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.4'
Tarek Ziadéedacea32010-01-29 11:41:03 +0000171 get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot '
172 '/Developer/SDKs/MacOSX10.4u.sdk '
173 '-fno-strict-aliasing -fno-common '
174 '-dynamic -DNDEBUG -g -O3')
175
Ezio Melottib3aedd42010-11-20 19:04:17 +0000176 self.assertEqual(get_platform(), 'macosx-10.4-fat')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000177
178 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot '
179 '/Developer/SDKs/MacOSX10.4u.sdk '
180 '-fno-strict-aliasing -fno-common '
181 '-dynamic -DNDEBUG -g -O3')
182
Ezio Melottib3aedd42010-11-20 19:04:17 +0000183 self.assertEqual(get_platform(), 'macosx-10.4-intel')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000184
185 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot '
186 '/Developer/SDKs/MacOSX10.4u.sdk '
187 '-fno-strict-aliasing -fno-common '
188 '-dynamic -DNDEBUG -g -O3')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000189 self.assertEqual(get_platform(), 'macosx-10.4-fat3')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000190
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')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000195 self.assertEqual(get_platform(), 'macosx-10.4-universal')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000196
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
Ezio Melottib3aedd42010-11-20 19:04:17 +0000202 self.assertEqual(get_platform(), 'macosx-10.4-fat64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000203
204 for arch in ('ppc', 'i386', 'x86_64', 'ppc64'):
205 get_config_vars()['CFLAGS'] = ('-arch %s -isysroot '
206 '/Developer/SDKs/MacOSX10.4u.sdk '
207 '-fno-strict-aliasing -fno-common '
Éric Araujof46676d2011-05-26 16:35:14 +0200208 '-dynamic -DNDEBUG -g -O3' % arch)
Tarek Ziadéedacea32010-01-29 11:41:03 +0000209
Éric Araujof46676d2011-05-26 16:35:14 +0200210 self.assertEqual(get_platform(), 'macosx-10.4-%s' % arch)
Tarek Ziadéedacea32010-01-29 11:41:03 +0000211
212 # linux debian sarge
213 os.name = 'posix'
214 sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) '
215 '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]')
216 sys.platform = 'linux2'
217 self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7',
218 '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686'))
219
Ezio Melottib3aedd42010-11-20 19:04:17 +0000220 self.assertEqual(get_platform(), 'linux-i686')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000221
222 # XXX more platforms to tests here
223
224 def test_get_config_h_filename(self):
225 config_h = sysconfig.get_config_h_filename()
226 self.assertTrue(os.path.isfile(config_h), config_h)
227
Tarek Ziadébd797682010-02-02 23:16:13 +0000228 def test_get_scheme_names(self):
Benjamin Peterson3c451e62010-05-08 15:51:23 +0000229 wanted = ('nt', 'nt_user', 'os2', 'os2_home', 'osx_framework_user',
230 'posix_home', 'posix_prefix', 'posix_user')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000231 self.assertEqual(get_scheme_names(), wanted)
Tarek Ziadébd797682010-02-02 23:16:13 +0000232
Brian Curtin3b4499c2010-12-28 14:31:47 +0000233 @skip_unless_symlink
Florent Xiclunaa4707382010-03-11 00:05:17 +0000234 def test_symlink(self):
Brian Curtind40e6f72010-07-08 21:39:08 +0000235 # On Windows, the EXE needs to know where pythonXY.dll is at so we have
236 # to add the directory to the path.
237 if sys.platform == "win32":
Éric Araujoc1b7e7f2011-09-18 23:12:30 +0200238 os.environ["PATH"] = "{};{}".format(
239 os.path.dirname(sys.executable), os.environ["PATH"])
Brian Curtind40e6f72010-07-08 21:39:08 +0000240
Florent Xiclunaa4707382010-03-11 00:05:17 +0000241 # Issue 7880
Florent Xiclunaa4707382010-03-11 00:05:17 +0000242 def get(python):
243 cmd = [python, '-c',
Florent Xicluna01fe6102010-03-13 15:35:12 +0000244 'import sysconfig; print(sysconfig.get_platform())']
Brian Curtind40e6f72010-07-08 21:39:08 +0000245 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=os.environ)
Florent Xiclunaa4707382010-03-11 00:05:17 +0000246 return p.communicate()
247 real = os.path.realpath(sys.executable)
248 link = os.path.abspath(TESTFN)
Brian Curtind40e6f72010-07-08 21:39:08 +0000249 os.symlink(real, link)
Florent Xiclunaa4707382010-03-11 00:05:17 +0000250 try:
251 self.assertEqual(get(real), get(link))
252 finally:
253 unlink(link)
254
Tarek Ziadé06710a82010-05-19 22:25:00 +0000255 def test_user_similar(self):
Éric Araujo48e484f2011-08-31 16:48:17 +0200256 # Issue #8759: make sure the posix scheme for the users
Tarek Ziadé06710a82010-05-19 22:25:00 +0000257 # is similar to the global posix_prefix one
258 base = get_config_var('base')
259 user = get_config_var('userbase')
Éric Araujode504552011-10-08 01:55:07 +0200260 # the global scheme mirrors the distinction between prefix and
261 # exec-prefix but not the user scheme, so we have to adapt the paths
262 # before comparing (issue #9100)
263 adapt = sys.prefix != sys.exec_prefix
Tarek Ziadé06710a82010-05-19 22:25:00 +0000264 for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'):
265 global_path = get_path(name, 'posix_prefix')
Éric Araujode504552011-10-08 01:55:07 +0200266 if adapt:
267 global_path = global_path.replace(sys.exec_prefix, sys.prefix)
268 base = base.replace(sys.exec_prefix, sys.prefix)
Tarek Ziadé06710a82010-05-19 22:25:00 +0000269 user_path = get_path(name, 'posix_user')
Éric Araujo48e484f2011-08-31 16:48:17 +0200270 self.assertEqual(user_path, global_path.replace(base, user, 1))
Tarek Ziadéedacea32010-01-29 11:41:03 +0000271
Tarek Ziadéa7514992010-05-25 09:44:36 +0000272 def test_main(self):
273 # just making sure _main() runs and returns things in the stdout
274 with captured_stdout() as output:
275 _main()
276 self.assertTrue(len(output.getvalue().split('\n')) > 0)
277
Brian Curtindb902ac2010-07-22 15:38:28 +0000278 @unittest.skipIf(sys.platform == "win32", "Does not apply to Windows")
Ronald Oussorenf4ebe2e2010-07-19 13:00:36 +0000279 def test_ldshared_value(self):
280 ldflags = sysconfig.get_config_var('LDFLAGS')
281 ldshared = sysconfig.get_config_var('LDSHARED')
282
283 self.assertIn(ldflags, ldshared)
284
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200285 @unittest.skipUnless(sys.platform == "darwin", "test only relevant on MacOSX")
286 def test_platform_in_subprocess(self):
287 my_platform = sysconfig.get_platform()
288
289 # Test without MACOSX_DEPLOYMENT_TARGET in the environment
290
291 env = os.environ.copy()
292 if 'MACOSX_DEPLOYMENT_TARGET' in env:
293 del env['MACOSX_DEPLOYMENT_TARGET']
294
295 with open('/dev/null', 'w') as devnull_fp:
296 p = subprocess.Popen([
297 sys.executable, '-c',
298 'import sysconfig; print(sysconfig.get_platform())',
299 ],
300 stdout=subprocess.PIPE,
301 stderr=devnull_fp,
302 env=env)
303 test_platform = p.communicate()[0].strip()
304 test_platform = test_platform.decode('utf-8')
305 status = p.wait()
306
307 self.assertEqual(status, 0)
308 self.assertEqual(my_platform, test_platform)
309
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200310 # Test with MACOSX_DEPLOYMENT_TARGET in the environment, and
311 # using a value that is unlikely to be the default one.
312 env = os.environ.copy()
313 env['MACOSX_DEPLOYMENT_TARGET'] = '10.1'
314
Brett Cannona4265542011-08-04 21:34:52 -0700315 with open('/dev/null') as dev_null:
316 p = subprocess.Popen([
317 sys.executable, '-c',
318 'import sysconfig; print(sysconfig.get_platform())',
319 ],
320 stdout=subprocess.PIPE,
321 stderr=dev_null,
322 env=env)
323 test_platform = p.communicate()[0].strip()
324 test_platform = test_platform.decode('utf-8')
325 status = p.wait()
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200326
Brett Cannona4265542011-08-04 21:34:52 -0700327 self.assertEqual(status, 0)
328 self.assertEqual(my_platform, test_platform)
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200329
330
Victor Stinner1273b7c2011-05-24 23:37:07 +0200331class MakefileTests(unittest.TestCase):
Éric Araujof46676d2011-05-26 16:35:14 +0200332
Victor Stinner1273b7c2011-05-24 23:37:07 +0200333 @unittest.skipIf(sys.platform.startswith('win'),
334 'Test is not Windows compatible')
335 def test_get_makefile_filename(self):
336 makefile = sysconfig.get_makefile_filename()
337 self.assertTrue(os.path.isfile(makefile), makefile)
338
339 def test_parse_makefile(self):
340 self.addCleanup(unlink, TESTFN)
341 with open(TESTFN, "w") as makefile:
342 print("var1=a$(VAR2)", file=makefile)
343 print("VAR2=b$(var3)", file=makefile)
344 print("var3=42", file=makefile)
345 print("var4=$/invalid", file=makefile)
346 print("var5=dollar$$5", file=makefile)
347 vars = sysconfig._parse_makefile(TESTFN)
348 self.assertEqual(vars, {
349 'var1': 'ab42',
350 'VAR2': 'b42',
351 'var3': 42,
352 'var4': '$/invalid',
353 'var5': 'dollar$5',
354 })
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200355
356
Tarek Ziadéedacea32010-01-29 11:41:03 +0000357def test_main():
Victor Stinner1273b7c2011-05-24 23:37:07 +0200358 run_unittest(TestSysConfig, MakefileTests)
Tarek Ziadéedacea32010-01-29 11:41:03 +0000359
360if __name__ == "__main__":
361 test_main()