blob: 3cb63ed8c4b517329be16e06e53e0fa4adadc64f [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,
Éric Araujoec177c12012-06-24 03:27:43 -040013 get_path, get_path_names, _INSTALL_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
Éric Araujoec177c12012-06-24 03:27:43 -040039 self._config_vars = sysconfig._CONFIG_VARS, copy(sysconfig._CONFIG_VARS)
Tarek Ziade1231a4e2011-05-19 13:07:25 +020040 self._added_envvars = []
41 self._changed_envvars = []
Éric Araujoc1b7e7f2011-09-18 23:12:30 +020042 for var in ('MACOSX_DEPLOYMENT_TARGET', 'PATH'):
Tarek Ziade1231a4e2011-05-19 13:07:25 +020043 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
Tarek Ziadéedacea32010-01-29 11:41:03 +000050 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
Éric Araujoec177c12012-06-24 03:27:43 -040062 sysconfig._CONFIG_VARS = self._config_vars[0]
63 sysconfig._CONFIG_VARS.clear()
64 sysconfig._CONFIG_VARS.update(self._config_vars[1])
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):
Éric Araujoec177c12012-06-24 03:27:43 -040086 self.assertEqual(get_path_names(), sysconfig._SCHEME_KEYS)
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)
Éric Araujoc1b7e7f2011-09-18 23:12:30 +020092 wanted = sorted(wanted.items())
93 scheme = sorted(scheme.items())
Ezio Melottib3aedd42010-11-20 19:04:17 +000094 self.assertEqual(scheme, wanted)
Tarek Ziadéedacea32010-01-29 11:41:03 +000095
96 def test_get_path(self):
Éric Araujoc1b7e7f2011-09-18 23:12:30 +020097 # XXX make real tests here
Éric Araujoec177c12012-06-24 03:27:43 -040098 for scheme in _INSTALL_SCHEMES:
99 for name in _INSTALL_SCHEMES[scheme]:
Tarek Ziadéedacea32010-01-29 11:41:03 +0000100 res = get_path(name, scheme)
101
102 def test_get_config_vars(self):
103 cvars = get_config_vars()
Éric Araujoc1b7e7f2011-09-18 23:12:30 +0200104 self.assertIsInstance(cvars, dict)
Tarek Ziadéedacea32010-01-29 11:41:03 +0000105 self.assertTrue(cvars)
106
107 def test_get_platform(self):
108 # windows XP, 32bits
109 os.name = 'nt'
110 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
111 '[MSC v.1310 32 bit (Intel)]')
112 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000113 self.assertEqual(get_platform(), 'win32')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000114
115 # windows XP, amd64
116 os.name = 'nt'
117 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
118 '[MSC v.1310 32 bit (Amd64)]')
119 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000120 self.assertEqual(get_platform(), 'win-amd64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000121
122 # windows XP, itanium
123 os.name = 'nt'
124 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
125 '[MSC v.1310 32 bit (Itanium)]')
126 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000127 self.assertEqual(get_platform(), 'win-ia64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000128
129 # macbook
130 os.name = 'posix'
131 sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) '
132 '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]')
133 sys.platform = 'darwin'
134 self._set_uname(('Darwin', 'macziade', '8.11.1',
135 ('Darwin Kernel Version 8.11.1: '
136 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000137 'root:xnu-792.25.20~1/RELEASE_I386'), 'PowerPC'))
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200138 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000139
140 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
141 '-fwrapv -O3 -Wall -Wstrict-prototypes')
142
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000143 maxint = sys.maxsize
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000144 try:
145 sys.maxsize = 2147483647
Ezio Melottib3aedd42010-11-20 19:04:17 +0000146 self.assertEqual(get_platform(), 'macosx-10.3-ppc')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000147 sys.maxsize = 9223372036854775807
Ezio Melottib3aedd42010-11-20 19:04:17 +0000148 self.assertEqual(get_platform(), 'macosx-10.3-ppc64')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000149 finally:
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000150 sys.maxsize = maxint
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000151
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000152 self._set_uname(('Darwin', 'macziade', '8.11.1',
153 ('Darwin Kernel Version 8.11.1: '
154 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadéedacea32010-01-29 11:41:03 +0000155 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386'))
156 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200157 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Tarek Ziadéedacea32010-01-29 11:41:03 +0000158
159 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
160 '-fwrapv -O3 -Wall -Wstrict-prototypes')
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000161 maxint = sys.maxsize
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000162 try:
163 sys.maxsize = 2147483647
Ezio Melottib3aedd42010-11-20 19:04:17 +0000164 self.assertEqual(get_platform(), 'macosx-10.3-i386')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000165 sys.maxsize = 9223372036854775807
Ezio Melottib3aedd42010-11-20 19:04:17 +0000166 self.assertEqual(get_platform(), 'macosx-10.3-x86_64')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000167 finally:
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000168 sys.maxsize = maxint
Tarek Ziadéedacea32010-01-29 11:41:03 +0000169
170 # macbook with fat binaries (fat, universal or fat64)
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200171 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.4'
Tarek Ziadéedacea32010-01-29 11:41:03 +0000172 get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot '
173 '/Developer/SDKs/MacOSX10.4u.sdk '
174 '-fno-strict-aliasing -fno-common '
175 '-dynamic -DNDEBUG -g -O3')
176
Ezio Melottib3aedd42010-11-20 19:04:17 +0000177 self.assertEqual(get_platform(), 'macosx-10.4-fat')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000178
179 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot '
180 '/Developer/SDKs/MacOSX10.4u.sdk '
181 '-fno-strict-aliasing -fno-common '
182 '-dynamic -DNDEBUG -g -O3')
183
Ezio Melottib3aedd42010-11-20 19:04:17 +0000184 self.assertEqual(get_platform(), 'macosx-10.4-intel')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000185
186 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot '
187 '/Developer/SDKs/MacOSX10.4u.sdk '
188 '-fno-strict-aliasing -fno-common '
189 '-dynamic -DNDEBUG -g -O3')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000190 self.assertEqual(get_platform(), 'macosx-10.4-fat3')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000191
192 get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot '
193 '/Developer/SDKs/MacOSX10.4u.sdk '
194 '-fno-strict-aliasing -fno-common '
195 '-dynamic -DNDEBUG -g -O3')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000196 self.assertEqual(get_platform(), 'macosx-10.4-universal')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000197
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
Ezio Melottib3aedd42010-11-20 19:04:17 +0000203 self.assertEqual(get_platform(), 'macosx-10.4-fat64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000204
205 for arch in ('ppc', 'i386', 'x86_64', 'ppc64'):
206 get_config_vars()['CFLAGS'] = ('-arch %s -isysroot '
207 '/Developer/SDKs/MacOSX10.4u.sdk '
208 '-fno-strict-aliasing -fno-common '
Éric Araujof46676d2011-05-26 16:35:14 +0200209 '-dynamic -DNDEBUG -g -O3' % arch)
Tarek Ziadéedacea32010-01-29 11:41:03 +0000210
Éric Araujof46676d2011-05-26 16:35:14 +0200211 self.assertEqual(get_platform(), 'macosx-10.4-%s' % arch)
Tarek Ziadéedacea32010-01-29 11:41:03 +0000212
213 # linux debian sarge
214 os.name = 'posix'
215 sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) '
216 '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]')
217 sys.platform = 'linux2'
218 self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7',
219 '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686'))
220
Ezio Melottib3aedd42010-11-20 19:04:17 +0000221 self.assertEqual(get_platform(), 'linux-i686')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000222
223 # XXX more platforms to tests here
224
225 def test_get_config_h_filename(self):
226 config_h = sysconfig.get_config_h_filename()
227 self.assertTrue(os.path.isfile(config_h), config_h)
228
Tarek Ziadébd797682010-02-02 23:16:13 +0000229 def test_get_scheme_names(self):
Benjamin Peterson3c451e62010-05-08 15:51:23 +0000230 wanted = ('nt', 'nt_user', 'os2', 'os2_home', 'osx_framework_user',
231 'posix_home', 'posix_prefix', 'posix_user')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000232 self.assertEqual(get_scheme_names(), wanted)
Tarek Ziadébd797682010-02-02 23:16:13 +0000233
Brian Curtin3b4499c2010-12-28 14:31:47 +0000234 @skip_unless_symlink
Florent Xiclunaa4707382010-03-11 00:05:17 +0000235 def test_symlink(self):
Brian Curtind40e6f72010-07-08 21:39:08 +0000236 # On Windows, the EXE needs to know where pythonXY.dll is at so we have
237 # to add the directory to the path.
238 if sys.platform == "win32":
Éric Araujoc1b7e7f2011-09-18 23:12:30 +0200239 os.environ["PATH"] = "{};{}".format(
240 os.path.dirname(sys.executable), os.environ["PATH"])
Brian Curtind40e6f72010-07-08 21:39:08 +0000241
Florent Xiclunaa4707382010-03-11 00:05:17 +0000242 # Issue 7880
Florent Xiclunaa4707382010-03-11 00:05:17 +0000243 def get(python):
244 cmd = [python, '-c',
Florent Xicluna01fe6102010-03-13 15:35:12 +0000245 'import sysconfig; print(sysconfig.get_platform())']
Brian Curtind40e6f72010-07-08 21:39:08 +0000246 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=os.environ)
Florent Xiclunaa4707382010-03-11 00:05:17 +0000247 return p.communicate()
248 real = os.path.realpath(sys.executable)
249 link = os.path.abspath(TESTFN)
Brian Curtind40e6f72010-07-08 21:39:08 +0000250 os.symlink(real, link)
Florent Xiclunaa4707382010-03-11 00:05:17 +0000251 try:
252 self.assertEqual(get(real), get(link))
253 finally:
254 unlink(link)
255
Tarek Ziadé06710a82010-05-19 22:25:00 +0000256 def test_user_similar(self):
Éric Araujo48e484f2011-08-31 16:48:17 +0200257 # Issue #8759: make sure the posix scheme for the users
Tarek Ziadé06710a82010-05-19 22:25:00 +0000258 # is similar to the global posix_prefix one
259 base = get_config_var('base')
260 user = get_config_var('userbase')
Éric Araujode504552011-10-08 01:55:07 +0200261 # the global scheme mirrors the distinction between prefix and
262 # exec-prefix but not the user scheme, so we have to adapt the paths
263 # before comparing (issue #9100)
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100264 adapt = sys.base_prefix != sys.base_exec_prefix
Tarek Ziadé06710a82010-05-19 22:25:00 +0000265 for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'):
266 global_path = get_path(name, 'posix_prefix')
Éric Araujode504552011-10-08 01:55:07 +0200267 if adapt:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100268 global_path = global_path.replace(sys.exec_prefix, sys.base_prefix)
269 base = base.replace(sys.exec_prefix, sys.base_prefix)
270 elif sys.base_prefix != sys.prefix:
271 # virtual environment? Likewise, we have to adapt the paths
272 # before comparing
273 global_path = global_path.replace(sys.base_prefix, sys.prefix)
274 base = base.replace(sys.base_prefix, sys.prefix)
Tarek Ziadé06710a82010-05-19 22:25:00 +0000275 user_path = get_path(name, 'posix_user')
Éric Araujo48e484f2011-08-31 16:48:17 +0200276 self.assertEqual(user_path, global_path.replace(base, user, 1))
Tarek Ziadéedacea32010-01-29 11:41:03 +0000277
Tarek Ziadéa7514992010-05-25 09:44:36 +0000278 def test_main(self):
279 # just making sure _main() runs and returns things in the stdout
280 with captured_stdout() as output:
281 _main()
282 self.assertTrue(len(output.getvalue().split('\n')) > 0)
283
Brian Curtindb902ac2010-07-22 15:38:28 +0000284 @unittest.skipIf(sys.platform == "win32", "Does not apply to Windows")
Ronald Oussorenf4ebe2e2010-07-19 13:00:36 +0000285 def test_ldshared_value(self):
286 ldflags = sysconfig.get_config_var('LDFLAGS')
287 ldshared = sysconfig.get_config_var('LDSHARED')
288
289 self.assertIn(ldflags, ldshared)
290
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200291 @unittest.skipUnless(sys.platform == "darwin", "test only relevant on MacOSX")
292 def test_platform_in_subprocess(self):
293 my_platform = sysconfig.get_platform()
294
295 # Test without MACOSX_DEPLOYMENT_TARGET in the environment
296
297 env = os.environ.copy()
298 if 'MACOSX_DEPLOYMENT_TARGET' in env:
299 del env['MACOSX_DEPLOYMENT_TARGET']
300
301 with open('/dev/null', 'w') as devnull_fp:
302 p = subprocess.Popen([
303 sys.executable, '-c',
304 'import sysconfig; print(sysconfig.get_platform())',
305 ],
306 stdout=subprocess.PIPE,
307 stderr=devnull_fp,
308 env=env)
309 test_platform = p.communicate()[0].strip()
310 test_platform = test_platform.decode('utf-8')
311 status = p.wait()
312
313 self.assertEqual(status, 0)
314 self.assertEqual(my_platform, test_platform)
315
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200316 # Test with MACOSX_DEPLOYMENT_TARGET in the environment, and
317 # using a value that is unlikely to be the default one.
318 env = os.environ.copy()
319 env['MACOSX_DEPLOYMENT_TARGET'] = '10.1'
320
Brett Cannona4265542011-08-04 21:34:52 -0700321 with open('/dev/null') as dev_null:
322 p = subprocess.Popen([
323 sys.executable, '-c',
324 'import sysconfig; print(sysconfig.get_platform())',
325 ],
326 stdout=subprocess.PIPE,
327 stderr=dev_null,
328 env=env)
329 test_platform = p.communicate()[0].strip()
330 test_platform = test_platform.decode('utf-8')
331 status = p.wait()
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200332
Brett Cannona4265542011-08-04 21:34:52 -0700333 self.assertEqual(status, 0)
334 self.assertEqual(my_platform, test_platform)
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200335
336
Victor Stinner1273b7c2011-05-24 23:37:07 +0200337class MakefileTests(unittest.TestCase):
Éric Araujof46676d2011-05-26 16:35:14 +0200338
Victor Stinner1273b7c2011-05-24 23:37:07 +0200339 @unittest.skipIf(sys.platform.startswith('win'),
340 'Test is not Windows compatible')
341 def test_get_makefile_filename(self):
342 makefile = sysconfig.get_makefile_filename()
343 self.assertTrue(os.path.isfile(makefile), makefile)
344
345 def test_parse_makefile(self):
346 self.addCleanup(unlink, TESTFN)
347 with open(TESTFN, "w") as makefile:
348 print("var1=a$(VAR2)", file=makefile)
349 print("VAR2=b$(var3)", file=makefile)
350 print("var3=42", file=makefile)
351 print("var4=$/invalid", file=makefile)
352 print("var5=dollar$$5", file=makefile)
353 vars = sysconfig._parse_makefile(TESTFN)
354 self.assertEqual(vars, {
355 'var1': 'ab42',
356 'VAR2': 'b42',
357 'var3': 42,
358 'var4': '$/invalid',
359 'var5': 'dollar$5',
360 })
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200361
362
Tarek Ziadéedacea32010-01-29 11:41:03 +0000363def test_main():
Victor Stinner1273b7c2011-05-24 23:37:07 +0200364 run_unittest(TestSysConfig, MakefileTests)
Tarek Ziadéedacea32010-01-29 11:41:03 +0000365
366if __name__ == "__main__":
367 test_main()