ยปCore Development>Code coverage>Lib/test/test_sysconfig.py

Python code coverage for Lib/test/test_sysconfig.py

#countcontent
1n/aimport unittest
2n/aimport sys
3n/aimport os
4n/aimport subprocess
5n/aimport shutil
6n/afrom copy import copy
7n/a
8n/afrom test.support import (import_module, TESTFN, unlink, check_warnings,
9n/a captured_stdout, skip_unless_symlink, change_cwd)
10n/a
11n/aimport sysconfig
12n/afrom sysconfig import (get_paths, get_platform, get_config_vars,
13n/a get_path, get_path_names, _INSTALL_SCHEMES,
14n/a _get_default_scheme, _expand_vars,
15n/a get_scheme_names, get_config_var, _main)
16n/aimport _osx_support
17n/a
18n/aclass TestSysConfig(unittest.TestCase):
19n/a
20n/a def setUp(self):
21n/a super(TestSysConfig, self).setUp()
22n/a self.sys_path = sys.path[:]
23n/a # patching os.uname
24n/a if hasattr(os, 'uname'):
25n/a self.uname = os.uname
26n/a self._uname = os.uname()
27n/a else:
28n/a self.uname = None
29n/a self._set_uname(('',)*5)
30n/a os.uname = self._get_uname
31n/a # saving the environment
32n/a self.name = os.name
33n/a self.platform = sys.platform
34n/a self.version = sys.version
35n/a self.sep = os.sep
36n/a self.join = os.path.join
37n/a self.isabs = os.path.isabs
38n/a self.splitdrive = os.path.splitdrive
39n/a self._config_vars = sysconfig._CONFIG_VARS, copy(sysconfig._CONFIG_VARS)
40n/a self._added_envvars = []
41n/a self._changed_envvars = []
42n/a for var in ('MACOSX_DEPLOYMENT_TARGET', 'PATH'):
43n/a if var in os.environ:
44n/a self._changed_envvars.append((var, os.environ[var]))
45n/a else:
46n/a self._added_envvars.append(var)
47n/a
48n/a def tearDown(self):
49n/a sys.path[:] = self.sys_path
50n/a self._cleanup_testfn()
51n/a if self.uname is not None:
52n/a os.uname = self.uname
53n/a else:
54n/a del os.uname
55n/a os.name = self.name
56n/a sys.platform = self.platform
57n/a sys.version = self.version
58n/a os.sep = self.sep
59n/a os.path.join = self.join
60n/a os.path.isabs = self.isabs
61n/a os.path.splitdrive = self.splitdrive
62n/a sysconfig._CONFIG_VARS = self._config_vars[0]
63n/a sysconfig._CONFIG_VARS.clear()
64n/a sysconfig._CONFIG_VARS.update(self._config_vars[1])
65n/a for var, value in self._changed_envvars:
66n/a os.environ[var] = value
67n/a for var in self._added_envvars:
68n/a os.environ.pop(var, None)
69n/a
70n/a super(TestSysConfig, self).tearDown()
71n/a
72n/a def _set_uname(self, uname):
73n/a self._uname = os.uname_result(uname)
74n/a
75n/a def _get_uname(self):
76n/a return self._uname
77n/a
78n/a def _cleanup_testfn(self):
79n/a path = TESTFN
80n/a if os.path.isfile(path):
81n/a os.remove(path)
82n/a elif os.path.isdir(path):
83n/a shutil.rmtree(path)
84n/a
85n/a def test_get_path_names(self):
86n/a self.assertEqual(get_path_names(), sysconfig._SCHEME_KEYS)
87n/a
88n/a def test_get_paths(self):
89n/a scheme = get_paths()
90n/a default_scheme = _get_default_scheme()
91n/a wanted = _expand_vars(default_scheme, None)
92n/a wanted = sorted(wanted.items())
93n/a scheme = sorted(scheme.items())
94n/a self.assertEqual(scheme, wanted)
95n/a
96n/a def test_get_path(self):
97n/a # XXX make real tests here
98n/a for scheme in _INSTALL_SCHEMES:
99n/a for name in _INSTALL_SCHEMES[scheme]:
100n/a res = get_path(name, scheme)
101n/a
102n/a def test_get_config_vars(self):
103n/a cvars = get_config_vars()
104n/a self.assertIsInstance(cvars, dict)
105n/a self.assertTrue(cvars)
106n/a
107n/a def test_get_platform(self):
108n/a # windows XP, 32bits
109n/a os.name = 'nt'
110n/a sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
111n/a '[MSC v.1310 32 bit (Intel)]')
112n/a sys.platform = 'win32'
113n/a self.assertEqual(get_platform(), 'win32')
114n/a
115n/a # windows XP, amd64
116n/a os.name = 'nt'
117n/a sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
118n/a '[MSC v.1310 32 bit (Amd64)]')
119n/a sys.platform = 'win32'
120n/a self.assertEqual(get_platform(), 'win-amd64')
121n/a
122n/a # windows XP, itanium
123n/a os.name = 'nt'
124n/a sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
125n/a '[MSC v.1310 32 bit (Itanium)]')
126n/a sys.platform = 'win32'
127n/a self.assertEqual(get_platform(), 'win-ia64')
128n/a
129n/a # macbook
130n/a os.name = 'posix'
131n/a sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) '
132n/a '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]')
133n/a sys.platform = 'darwin'
134n/a self._set_uname(('Darwin', 'macziade', '8.11.1',
135n/a ('Darwin Kernel Version 8.11.1: '
136n/a 'Wed Oct 10 18:23:28 PDT 2007; '
137n/a 'root:xnu-792.25.20~1/RELEASE_I386'), 'PowerPC'))
138n/a _osx_support._remove_original_values(get_config_vars())
139n/a get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
140n/a
141n/a get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
142n/a '-fwrapv -O3 -Wall -Wstrict-prototypes')
143n/a
144n/a maxint = sys.maxsize
145n/a try:
146n/a sys.maxsize = 2147483647
147n/a self.assertEqual(get_platform(), 'macosx-10.3-ppc')
148n/a sys.maxsize = 9223372036854775807
149n/a self.assertEqual(get_platform(), 'macosx-10.3-ppc64')
150n/a finally:
151n/a sys.maxsize = maxint
152n/a
153n/a self._set_uname(('Darwin', 'macziade', '8.11.1',
154n/a ('Darwin Kernel Version 8.11.1: '
155n/a 'Wed Oct 10 18:23:28 PDT 2007; '
156n/a 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386'))
157n/a _osx_support._remove_original_values(get_config_vars())
158n/a get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
159n/a
160n/a get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
161n/a '-fwrapv -O3 -Wall -Wstrict-prototypes')
162n/a maxint = sys.maxsize
163n/a try:
164n/a sys.maxsize = 2147483647
165n/a self.assertEqual(get_platform(), 'macosx-10.3-i386')
166n/a sys.maxsize = 9223372036854775807
167n/a self.assertEqual(get_platform(), 'macosx-10.3-x86_64')
168n/a finally:
169n/a sys.maxsize = maxint
170n/a
171n/a # macbook with fat binaries (fat, universal or fat64)
172n/a _osx_support._remove_original_values(get_config_vars())
173n/a get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.4'
174n/a get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot '
175n/a '/Developer/SDKs/MacOSX10.4u.sdk '
176n/a '-fno-strict-aliasing -fno-common '
177n/a '-dynamic -DNDEBUG -g -O3')
178n/a
179n/a self.assertEqual(get_platform(), 'macosx-10.4-fat')
180n/a
181n/a _osx_support._remove_original_values(get_config_vars())
182n/a get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot '
183n/a '/Developer/SDKs/MacOSX10.4u.sdk '
184n/a '-fno-strict-aliasing -fno-common '
185n/a '-dynamic -DNDEBUG -g -O3')
186n/a
187n/a self.assertEqual(get_platform(), 'macosx-10.4-intel')
188n/a
189n/a _osx_support._remove_original_values(get_config_vars())
190n/a get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot '
191n/a '/Developer/SDKs/MacOSX10.4u.sdk '
192n/a '-fno-strict-aliasing -fno-common '
193n/a '-dynamic -DNDEBUG -g -O3')
194n/a self.assertEqual(get_platform(), 'macosx-10.4-fat3')
195n/a
196n/a _osx_support._remove_original_values(get_config_vars())
197n/a get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot '
198n/a '/Developer/SDKs/MacOSX10.4u.sdk '
199n/a '-fno-strict-aliasing -fno-common '
200n/a '-dynamic -DNDEBUG -g -O3')
201n/a self.assertEqual(get_platform(), 'macosx-10.4-universal')
202n/a
203n/a _osx_support._remove_original_values(get_config_vars())
204n/a get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -isysroot '
205n/a '/Developer/SDKs/MacOSX10.4u.sdk '
206n/a '-fno-strict-aliasing -fno-common '
207n/a '-dynamic -DNDEBUG -g -O3')
208n/a
209n/a self.assertEqual(get_platform(), 'macosx-10.4-fat64')
210n/a
211n/a for arch in ('ppc', 'i386', 'x86_64', 'ppc64'):
212n/a _osx_support._remove_original_values(get_config_vars())
213n/a get_config_vars()['CFLAGS'] = ('-arch %s -isysroot '
214n/a '/Developer/SDKs/MacOSX10.4u.sdk '
215n/a '-fno-strict-aliasing -fno-common '
216n/a '-dynamic -DNDEBUG -g -O3' % arch)
217n/a
218n/a self.assertEqual(get_platform(), 'macosx-10.4-%s' % arch)
219n/a
220n/a # linux debian sarge
221n/a os.name = 'posix'
222n/a sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) '
223n/a '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]')
224n/a sys.platform = 'linux2'
225n/a self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7',
226n/a '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686'))
227n/a
228n/a self.assertEqual(get_platform(), 'linux-i686')
229n/a
230n/a # XXX more platforms to tests here
231n/a
232n/a def test_get_config_h_filename(self):
233n/a config_h = sysconfig.get_config_h_filename()
234n/a self.assertTrue(os.path.isfile(config_h), config_h)
235n/a
236n/a def test_get_scheme_names(self):
237n/a wanted = ('nt', 'nt_user', 'osx_framework_user',
238n/a 'posix_home', 'posix_prefix', 'posix_user')
239n/a self.assertEqual(get_scheme_names(), wanted)
240n/a
241n/a @skip_unless_symlink
242n/a def test_symlink(self):
243n/a # On Windows, the EXE needs to know where pythonXY.dll is at so we have
244n/a # to add the directory to the path.
245n/a if sys.platform == "win32":
246n/a os.environ["PATH"] = "{};{}".format(
247n/a os.path.dirname(sys.executable), os.environ["PATH"])
248n/a
249n/a # Issue 7880
250n/a def get(python):
251n/a cmd = [python, '-c',
252n/a 'import sysconfig; print(sysconfig.get_platform())']
253n/a p = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=os.environ)
254n/a return p.communicate()
255n/a real = os.path.realpath(sys.executable)
256n/a link = os.path.abspath(TESTFN)
257n/a os.symlink(real, link)
258n/a try:
259n/a self.assertEqual(get(real), get(link))
260n/a finally:
261n/a unlink(link)
262n/a
263n/a def test_user_similar(self):
264n/a # Issue #8759: make sure the posix scheme for the users
265n/a # is similar to the global posix_prefix one
266n/a base = get_config_var('base')
267n/a user = get_config_var('userbase')
268n/a # the global scheme mirrors the distinction between prefix and
269n/a # exec-prefix but not the user scheme, so we have to adapt the paths
270n/a # before comparing (issue #9100)
271n/a adapt = sys.base_prefix != sys.base_exec_prefix
272n/a for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'):
273n/a global_path = get_path(name, 'posix_prefix')
274n/a if adapt:
275n/a global_path = global_path.replace(sys.exec_prefix, sys.base_prefix)
276n/a base = base.replace(sys.exec_prefix, sys.base_prefix)
277n/a elif sys.base_prefix != sys.prefix:
278n/a # virtual environment? Likewise, we have to adapt the paths
279n/a # before comparing
280n/a global_path = global_path.replace(sys.base_prefix, sys.prefix)
281n/a base = base.replace(sys.base_prefix, sys.prefix)
282n/a user_path = get_path(name, 'posix_user')
283n/a self.assertEqual(user_path, global_path.replace(base, user, 1))
284n/a
285n/a def test_main(self):
286n/a # just making sure _main() runs and returns things in the stdout
287n/a with captured_stdout() as output:
288n/a _main()
289n/a self.assertTrue(len(output.getvalue().split('\n')) > 0)
290n/a
291n/a @unittest.skipIf(sys.platform == "win32", "Does not apply to Windows")
292n/a def test_ldshared_value(self):
293n/a ldflags = sysconfig.get_config_var('LDFLAGS')
294n/a ldshared = sysconfig.get_config_var('LDSHARED')
295n/a
296n/a self.assertIn(ldflags, ldshared)
297n/a
298n/a @unittest.skipUnless(sys.platform == "darwin", "test only relevant on MacOSX")
299n/a def test_platform_in_subprocess(self):
300n/a my_platform = sysconfig.get_platform()
301n/a
302n/a # Test without MACOSX_DEPLOYMENT_TARGET in the environment
303n/a
304n/a env = os.environ.copy()
305n/a if 'MACOSX_DEPLOYMENT_TARGET' in env:
306n/a del env['MACOSX_DEPLOYMENT_TARGET']
307n/a
308n/a p = subprocess.Popen([
309n/a sys.executable, '-c',
310n/a 'import sysconfig; print(sysconfig.get_platform())',
311n/a ],
312n/a stdout=subprocess.PIPE,
313n/a stderr=subprocess.DEVNULL,
314n/a env=env)
315n/a test_platform = p.communicate()[0].strip()
316n/a test_platform = test_platform.decode('utf-8')
317n/a status = p.wait()
318n/a
319n/a self.assertEqual(status, 0)
320n/a self.assertEqual(my_platform, test_platform)
321n/a
322n/a # Test with MACOSX_DEPLOYMENT_TARGET in the environment, and
323n/a # using a value that is unlikely to be the default one.
324n/a env = os.environ.copy()
325n/a env['MACOSX_DEPLOYMENT_TARGET'] = '10.1'
326n/a
327n/a p = subprocess.Popen([
328n/a sys.executable, '-c',
329n/a 'import sysconfig; print(sysconfig.get_platform())',
330n/a ],
331n/a stdout=subprocess.PIPE,
332n/a stderr=subprocess.DEVNULL,
333n/a env=env)
334n/a test_platform = p.communicate()[0].strip()
335n/a test_platform = test_platform.decode('utf-8')
336n/a status = p.wait()
337n/a
338n/a self.assertEqual(status, 0)
339n/a self.assertEqual(my_platform, test_platform)
340n/a
341n/a def test_srcdir(self):
342n/a # See Issues #15322, #15364.
343n/a srcdir = sysconfig.get_config_var('srcdir')
344n/a
345n/a self.assertTrue(os.path.isabs(srcdir), srcdir)
346n/a self.assertTrue(os.path.isdir(srcdir), srcdir)
347n/a
348n/a if sysconfig._PYTHON_BUILD:
349n/a # The python executable has not been installed so srcdir
350n/a # should be a full source checkout.
351n/a Python_h = os.path.join(srcdir, 'Include', 'Python.h')
352n/a self.assertTrue(os.path.exists(Python_h), Python_h)
353n/a self.assertTrue(sysconfig._is_python_source_dir(srcdir))
354n/a elif os.name == 'posix':
355n/a makefile_dir = os.path.dirname(sysconfig.get_makefile_filename())
356n/a # Issue #19340: srcdir has been realpath'ed already
357n/a makefile_dir = os.path.realpath(makefile_dir)
358n/a self.assertEqual(makefile_dir, srcdir)
359n/a
360n/a def test_srcdir_independent_of_cwd(self):
361n/a # srcdir should be independent of the current working directory
362n/a # See Issues #15322, #15364.
363n/a srcdir = sysconfig.get_config_var('srcdir')
364n/a with change_cwd(os.pardir):
365n/a srcdir2 = sysconfig.get_config_var('srcdir')
366n/a self.assertEqual(srcdir, srcdir2)
367n/a
368n/a @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None,
369n/a 'EXT_SUFFIX required for this test')
370n/a def test_SO_deprecation(self):
371n/a self.assertWarns(DeprecationWarning,
372n/a sysconfig.get_config_var, 'SO')
373n/a
374n/a @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None,
375n/a 'EXT_SUFFIX required for this test')
376n/a def test_SO_value(self):
377n/a with check_warnings(('', DeprecationWarning)):
378n/a self.assertEqual(sysconfig.get_config_var('SO'),
379n/a sysconfig.get_config_var('EXT_SUFFIX'))
380n/a
381n/a @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None,
382n/a 'EXT_SUFFIX required for this test')
383n/a def test_SO_in_vars(self):
384n/a vars = sysconfig.get_config_vars()
385n/a self.assertIsNotNone(vars['SO'])
386n/a self.assertEqual(vars['SO'], vars['EXT_SUFFIX'])
387n/a
388n/a @unittest.skipUnless(sys.platform == 'linux' and
389n/a hasattr(sys.implementation, '_multiarch'),
390n/a 'multiarch-specific test')
391n/a def test_triplet_in_ext_suffix(self):
392n/a ctypes = import_module('ctypes')
393n/a import platform, re
394n/a machine = platform.machine()
395n/a suffix = sysconfig.get_config_var('EXT_SUFFIX')
396n/a if re.match('(aarch64|arm|mips|ppc|powerpc|s390|sparc)', machine):
397n/a self.assertTrue('linux' in suffix, suffix)
398n/a if re.match('(i[3-6]86|x86_64)$', machine):
399n/a if ctypes.sizeof(ctypes.c_char_p()) == 4:
400n/a self.assertTrue(suffix.endswith('i386-linux-gnu.so') or
401n/a suffix.endswith('x86_64-linux-gnux32.so'),
402n/a suffix)
403n/a else: # 8 byte pointer size
404n/a self.assertTrue(suffix.endswith('x86_64-linux-gnu.so'), suffix)
405n/a
406n/a @unittest.skipUnless(sys.platform == 'darwin', 'OS X-specific test')
407n/a def test_osx_ext_suffix(self):
408n/a suffix = sysconfig.get_config_var('EXT_SUFFIX')
409n/a self.assertTrue(suffix.endswith('-darwin.so'), suffix)
410n/a
411n/aclass MakefileTests(unittest.TestCase):
412n/a
413n/a @unittest.skipIf(sys.platform.startswith('win'),
414n/a 'Test is not Windows compatible')
415n/a def test_get_makefile_filename(self):
416n/a makefile = sysconfig.get_makefile_filename()
417n/a self.assertTrue(os.path.isfile(makefile), makefile)
418n/a
419n/a def test_parse_makefile(self):
420n/a self.addCleanup(unlink, TESTFN)
421n/a with open(TESTFN, "w") as makefile:
422n/a print("var1=a$(VAR2)", file=makefile)
423n/a print("VAR2=b$(var3)", file=makefile)
424n/a print("var3=42", file=makefile)
425n/a print("var4=$/invalid", file=makefile)
426n/a print("var5=dollar$$5", file=makefile)
427n/a print("var6=${var3}/lib/python3.5/config-$(VAR2)$(var5)"
428n/a "-x86_64-linux-gnu", file=makefile)
429n/a vars = sysconfig._parse_makefile(TESTFN)
430n/a self.assertEqual(vars, {
431n/a 'var1': 'ab42',
432n/a 'VAR2': 'b42',
433n/a 'var3': 42,
434n/a 'var4': '$/invalid',
435n/a 'var5': 'dollar$5',
436n/a 'var6': '42/lib/python3.5/config-b42dollar$5-x86_64-linux-gnu',
437n/a })
438n/a
439n/a
440n/aif __name__ == "__main__":
441n/a unittest.main()