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