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

Python code coverage for Lib/test/test_platform.py

#countcontent
1n/afrom unittest import mock
2n/aimport os
3n/aimport platform
4n/aimport subprocess
5n/aimport sys
6n/aimport tempfile
7n/aimport unittest
8n/aimport warnings
9n/a
10n/afrom test import support
11n/a
12n/aclass PlatformTest(unittest.TestCase):
13n/a def test_architecture(self):
14n/a res = platform.architecture()
15n/a
16n/a @support.skip_unless_symlink
17n/a def test_architecture_via_symlink(self): # issue3762
18n/a # On Windows, the EXE needs to know where pythonXY.dll and *.pyd is at
19n/a # so we add the directory to the path and PYTHONPATH.
20n/a if sys.platform == "win32":
21n/a def restore_environ(old_env):
22n/a os.environ.clear()
23n/a os.environ.update(old_env)
24n/a
25n/a self.addCleanup(restore_environ, dict(os.environ))
26n/a
27n/a os.environ["Path"] = "{};{}".format(
28n/a os.path.dirname(sys.executable), os.environ["Path"])
29n/a os.environ["PYTHONPATH"] = os.path.dirname(sys.executable)
30n/a
31n/a def get(python):
32n/a cmd = [python, '-c',
33n/a 'import platform; print(platform.architecture())']
34n/a p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
35n/a return p.communicate()
36n/a
37n/a real = os.path.realpath(sys.executable)
38n/a link = os.path.abspath(support.TESTFN)
39n/a os.symlink(real, link)
40n/a try:
41n/a self.assertEqual(get(real), get(link))
42n/a finally:
43n/a os.remove(link)
44n/a
45n/a def test_platform(self):
46n/a for aliased in (False, True):
47n/a for terse in (False, True):
48n/a res = platform.platform(aliased, terse)
49n/a
50n/a def test_system(self):
51n/a res = platform.system()
52n/a
53n/a def test_node(self):
54n/a res = platform.node()
55n/a
56n/a def test_release(self):
57n/a res = platform.release()
58n/a
59n/a def test_version(self):
60n/a res = platform.version()
61n/a
62n/a def test_machine(self):
63n/a res = platform.machine()
64n/a
65n/a def test_processor(self):
66n/a res = platform.processor()
67n/a
68n/a def setUp(self):
69n/a self.save_version = sys.version
70n/a self.save_mercurial = sys._mercurial
71n/a self.save_platform = sys.platform
72n/a
73n/a def tearDown(self):
74n/a sys.version = self.save_version
75n/a sys._mercurial = self.save_mercurial
76n/a sys.platform = self.save_platform
77n/a
78n/a def test_sys_version(self):
79n/a # Old test.
80n/a for input, output in (
81n/a ('2.4.3 (#1, Jun 21 2006, 13:54:21) \n[GCC 3.3.4 (pre 3.3.5 20040809)]',
82n/a ('CPython', '2.4.3', '', '', '1', 'Jun 21 2006 13:54:21', 'GCC 3.3.4 (pre 3.3.5 20040809)')),
83n/a ('IronPython 1.0.60816 on .NET 2.0.50727.42',
84n/a ('IronPython', '1.0.60816', '', '', '', '', '.NET 2.0.50727.42')),
85n/a ('IronPython 1.0 (1.0.61005.1977) on .NET 2.0.50727.42',
86n/a ('IronPython', '1.0.0', '', '', '', '', '.NET 2.0.50727.42')),
87n/a ('2.4.3 (truncation, date, t) \n[GCC]',
88n/a ('CPython', '2.4.3', '', '', 'truncation', 'date t', 'GCC')),
89n/a ('2.4.3 (truncation, date, ) \n[GCC]',
90n/a ('CPython', '2.4.3', '', '', 'truncation', 'date', 'GCC')),
91n/a ('2.4.3 (truncation, date,) \n[GCC]',
92n/a ('CPython', '2.4.3', '', '', 'truncation', 'date', 'GCC')),
93n/a ('2.4.3 (truncation, date) \n[GCC]',
94n/a ('CPython', '2.4.3', '', '', 'truncation', 'date', 'GCC')),
95n/a ('2.4.3 (truncation, d) \n[GCC]',
96n/a ('CPython', '2.4.3', '', '', 'truncation', 'd', 'GCC')),
97n/a ('2.4.3 (truncation, ) \n[GCC]',
98n/a ('CPython', '2.4.3', '', '', 'truncation', '', 'GCC')),
99n/a ('2.4.3 (truncation,) \n[GCC]',
100n/a ('CPython', '2.4.3', '', '', 'truncation', '', 'GCC')),
101n/a ('2.4.3 (truncation) \n[GCC]',
102n/a ('CPython', '2.4.3', '', '', 'truncation', '', 'GCC')),
103n/a ):
104n/a # branch and revision are not "parsed", but fetched
105n/a # from sys._mercurial. Ignore them
106n/a (name, version, branch, revision, buildno, builddate, compiler) \
107n/a = platform._sys_version(input)
108n/a self.assertEqual(
109n/a (name, version, '', '', buildno, builddate, compiler), output)
110n/a
111n/a # Tests for python_implementation(), python_version(), python_branch(),
112n/a # python_revision(), python_build(), and python_compiler().
113n/a sys_versions = {
114n/a ("2.6.1 (r261:67515, Dec 6 2008, 15:26:00) \n[GCC 4.0.1 (Apple Computer, Inc. build 5370)]",
115n/a ('CPython', 'tags/r261', '67515'), self.save_platform)
116n/a :
117n/a ("CPython", "2.6.1", "tags/r261", "67515",
118n/a ('r261:67515', 'Dec 6 2008 15:26:00'),
119n/a 'GCC 4.0.1 (Apple Computer, Inc. build 5370)'),
120n/a
121n/a ("IronPython 2.0 (2.0.0.0) on .NET 2.0.50727.3053", None, "cli")
122n/a :
123n/a ("IronPython", "2.0.0", "", "", ("", ""),
124n/a ".NET 2.0.50727.3053"),
125n/a
126n/a ("2.6.1 (IronPython 2.6.1 (2.6.10920.0) on .NET 2.0.50727.1433)", None, "cli")
127n/a :
128n/a ("IronPython", "2.6.1", "", "", ("", ""),
129n/a ".NET 2.0.50727.1433"),
130n/a
131n/a ("2.7.4 (IronPython 2.7.4 (2.7.0.40) on Mono 4.0.30319.1 (32-bit))", None, "cli")
132n/a :
133n/a ("IronPython", "2.7.4", "", "", ("", ""),
134n/a "Mono 4.0.30319.1 (32-bit)"),
135n/a
136n/a ("2.5 (trunk:6107, Mar 26 2009, 13:02:18) \n[Java HotSpot(TM) Client VM (\"Apple Computer, Inc.\")]",
137n/a ('Jython', 'trunk', '6107'), "java1.5.0_16")
138n/a :
139n/a ("Jython", "2.5.0", "trunk", "6107",
140n/a ('trunk:6107', 'Mar 26 2009'), "java1.5.0_16"),
141n/a
142n/a ("2.5.2 (63378, Mar 26 2009, 18:03:29)\n[PyPy 1.0.0]",
143n/a ('PyPy', 'trunk', '63378'), self.save_platform)
144n/a :
145n/a ("PyPy", "2.5.2", "trunk", "63378", ('63378', 'Mar 26 2009'),
146n/a "")
147n/a }
148n/a for (version_tag, subversion, sys_platform), info in \
149n/a sys_versions.items():
150n/a sys.version = version_tag
151n/a if subversion is None:
152n/a if hasattr(sys, "_mercurial"):
153n/a del sys._mercurial
154n/a else:
155n/a sys._mercurial = subversion
156n/a if sys_platform is not None:
157n/a sys.platform = sys_platform
158n/a self.assertEqual(platform.python_implementation(), info[0])
159n/a self.assertEqual(platform.python_version(), info[1])
160n/a self.assertEqual(platform.python_branch(), info[2])
161n/a self.assertEqual(platform.python_revision(), info[3])
162n/a self.assertEqual(platform.python_build(), info[4])
163n/a self.assertEqual(platform.python_compiler(), info[5])
164n/a
165n/a def test_system_alias(self):
166n/a res = platform.system_alias(
167n/a platform.system(),
168n/a platform.release(),
169n/a platform.version(),
170n/a )
171n/a
172n/a def test_uname(self):
173n/a res = platform.uname()
174n/a self.assertTrue(any(res))
175n/a self.assertEqual(res[0], res.system)
176n/a self.assertEqual(res[1], res.node)
177n/a self.assertEqual(res[2], res.release)
178n/a self.assertEqual(res[3], res.version)
179n/a self.assertEqual(res[4], res.machine)
180n/a self.assertEqual(res[5], res.processor)
181n/a
182n/a @unittest.skipUnless(sys.platform.startswith('win'), "windows only test")
183n/a def test_uname_win32_ARCHITEW6432(self):
184n/a # Issue 7860: make sure we get architecture from the correct variable
185n/a # on 64 bit Windows: if PROCESSOR_ARCHITEW6432 exists we should be
186n/a # using it, per
187n/a # http://blogs.msdn.com/david.wang/archive/2006/03/26/HOWTO-Detect-Process-Bitness.aspx
188n/a try:
189n/a with support.EnvironmentVarGuard() as environ:
190n/a if 'PROCESSOR_ARCHITEW6432' in environ:
191n/a del environ['PROCESSOR_ARCHITEW6432']
192n/a environ['PROCESSOR_ARCHITECTURE'] = 'foo'
193n/a platform._uname_cache = None
194n/a system, node, release, version, machine, processor = platform.uname()
195n/a self.assertEqual(machine, 'foo')
196n/a environ['PROCESSOR_ARCHITEW6432'] = 'bar'
197n/a platform._uname_cache = None
198n/a system, node, release, version, machine, processor = platform.uname()
199n/a self.assertEqual(machine, 'bar')
200n/a finally:
201n/a platform._uname_cache = None
202n/a
203n/a def test_java_ver(self):
204n/a res = platform.java_ver()
205n/a if sys.platform == 'java':
206n/a self.assertTrue(all(res))
207n/a
208n/a def test_win32_ver(self):
209n/a res = platform.win32_ver()
210n/a
211n/a def test_mac_ver(self):
212n/a res = platform.mac_ver()
213n/a
214n/a if platform.uname().system == 'Darwin':
215n/a # We're on a MacOSX system, check that
216n/a # the right version information is returned
217n/a fd = os.popen('sw_vers', 'r')
218n/a real_ver = None
219n/a for ln in fd:
220n/a if ln.startswith('ProductVersion:'):
221n/a real_ver = ln.strip().split()[-1]
222n/a break
223n/a fd.close()
224n/a self.assertFalse(real_ver is None)
225n/a result_list = res[0].split('.')
226n/a expect_list = real_ver.split('.')
227n/a len_diff = len(result_list) - len(expect_list)
228n/a # On Snow Leopard, sw_vers reports 10.6.0 as 10.6
229n/a if len_diff > 0:
230n/a expect_list.extend(['0'] * len_diff)
231n/a self.assertEqual(result_list, expect_list)
232n/a
233n/a # res[1] claims to contain
234n/a # (version, dev_stage, non_release_version)
235n/a # That information is no longer available
236n/a self.assertEqual(res[1], ('', '', ''))
237n/a
238n/a if sys.byteorder == 'little':
239n/a self.assertIn(res[2], ('i386', 'x86_64'))
240n/a else:
241n/a self.assertEqual(res[2], 'PowerPC')
242n/a
243n/a
244n/a @unittest.skipUnless(sys.platform == 'darwin', "OSX only test")
245n/a def test_mac_ver_with_fork(self):
246n/a # Issue7895: platform.mac_ver() crashes when using fork without exec
247n/a #
248n/a # This test checks that the fix for that issue works.
249n/a #
250n/a pid = os.fork()
251n/a if pid == 0:
252n/a # child
253n/a info = platform.mac_ver()
254n/a os._exit(0)
255n/a
256n/a else:
257n/a # parent
258n/a cpid, sts = os.waitpid(pid, 0)
259n/a self.assertEqual(cpid, pid)
260n/a self.assertEqual(sts, 0)
261n/a
262n/a def test_dist(self):
263n/a with warnings.catch_warnings():
264n/a warnings.filterwarnings(
265n/a 'ignore',
266n/a r'dist\(\) and linux_distribution\(\) '
267n/a 'functions are deprecated .*',
268n/a PendingDeprecationWarning,
269n/a )
270n/a res = platform.dist()
271n/a
272n/a def test_libc_ver(self):
273n/a import os
274n/a if os.path.isdir(sys.executable) and \
275n/a os.path.exists(sys.executable+'.exe'):
276n/a # Cygwin horror
277n/a executable = sys.executable + '.exe'
278n/a else:
279n/a executable = sys.executable
280n/a res = platform.libc_ver(executable)
281n/a
282n/a def test_parse_release_file(self):
283n/a
284n/a for input, output in (
285n/a # Examples of release file contents:
286n/a ('SuSE Linux 9.3 (x86-64)', ('SuSE Linux ', '9.3', 'x86-64')),
287n/a ('SUSE LINUX 10.1 (X86-64)', ('SUSE LINUX ', '10.1', 'X86-64')),
288n/a ('SUSE LINUX 10.1 (i586)', ('SUSE LINUX ', '10.1', 'i586')),
289n/a ('Fedora Core release 5 (Bordeaux)', ('Fedora Core', '5', 'Bordeaux')),
290n/a ('Red Hat Linux release 8.0 (Psyche)', ('Red Hat Linux', '8.0', 'Psyche')),
291n/a ('Red Hat Linux release 9 (Shrike)', ('Red Hat Linux', '9', 'Shrike')),
292n/a ('Red Hat Enterprise Linux release 4 (Nahant)', ('Red Hat Enterprise Linux', '4', 'Nahant')),
293n/a ('CentOS release 4', ('CentOS', '4', None)),
294n/a ('Rocks release 4.2.1 (Cydonia)', ('Rocks', '4.2.1', 'Cydonia')),
295n/a ('', ('', '', '')), # If there's nothing there.
296n/a ):
297n/a self.assertEqual(platform._parse_release_file(input), output)
298n/a
299n/a def test_popen(self):
300n/a mswindows = (sys.platform == "win32")
301n/a
302n/a if mswindows:
303n/a command = '"{}" -c "print(\'Hello\')"'.format(sys.executable)
304n/a else:
305n/a command = "'{}' -c 'print(\"Hello\")'".format(sys.executable)
306n/a with warnings.catch_warnings():
307n/a warnings.simplefilter("ignore", DeprecationWarning)
308n/a with platform.popen(command) as stdout:
309n/a hello = stdout.read().strip()
310n/a stdout.close()
311n/a self.assertEqual(hello, "Hello")
312n/a
313n/a data = 'plop'
314n/a if mswindows:
315n/a command = '"{}" -c "import sys; data=sys.stdin.read(); exit(len(data))"'
316n/a else:
317n/a command = "'{}' -c 'import sys; data=sys.stdin.read(); exit(len(data))'"
318n/a command = command.format(sys.executable)
319n/a with warnings.catch_warnings():
320n/a warnings.simplefilter("ignore", DeprecationWarning)
321n/a with platform.popen(command, 'w') as stdin:
322n/a stdout = stdin.write(data)
323n/a ret = stdin.close()
324n/a self.assertIsNotNone(ret)
325n/a if os.name == 'nt':
326n/a returncode = ret
327n/a else:
328n/a returncode = ret >> 8
329n/a self.assertEqual(returncode, len(data))
330n/a
331n/a def test_linux_distribution_encoding(self):
332n/a # Issue #17429
333n/a with tempfile.TemporaryDirectory() as tempdir:
334n/a filename = os.path.join(tempdir, 'fedora-release')
335n/a with open(filename, 'w', encoding='utf-8') as f:
336n/a f.write('Fedora release 19 (Schr\xf6dinger\u2019s Cat)\n')
337n/a
338n/a with mock.patch('platform._UNIXCONFDIR', tempdir):
339n/a with warnings.catch_warnings():
340n/a warnings.filterwarnings(
341n/a 'ignore',
342n/a r'dist\(\) and linux_distribution\(\) '
343n/a 'functions are deprecated .*',
344n/a PendingDeprecationWarning,
345n/a )
346n/a distname, version, distid = platform.linux_distribution()
347n/a
348n/a self.assertEqual(distname, 'Fedora')
349n/a self.assertEqual(version, '19')
350n/a self.assertEqual(distid, 'Schr\xf6dinger\u2019s Cat')
351n/a
352n/a
353n/aclass DeprecationTest(unittest.TestCase):
354n/a
355n/a def test_dist_deprecation(self):
356n/a with self.assertWarns(PendingDeprecationWarning) as cm:
357n/a platform.dist()
358n/a self.assertEqual(str(cm.warning),
359n/a 'dist() and linux_distribution() functions are '
360n/a 'deprecated in Python 3.5')
361n/a
362n/a def test_linux_distribution_deprecation(self):
363n/a with self.assertWarns(PendingDeprecationWarning) as cm:
364n/a platform.linux_distribution()
365n/a self.assertEqual(str(cm.warning),
366n/a 'dist() and linux_distribution() functions are '
367n/a 'deprecated in Python 3.5')
368n/a
369n/aif __name__ == '__main__':
370n/a unittest.main()