blob: 93f86d90f1ea015977d747d6cae708be669c172d [file] [log] [blame]
Benjamin Peterson856ff5f2008-05-29 21:22:40 +00001import os
Walter Dörwaldc69d1c42005-11-21 17:48:12 +00002import platform
Hirokazu Yamamotoe6748402008-10-06 04:51:11 +00003import subprocess
Victor Stinner3a38a6d2011-06-10 13:59:59 +02004import sys
5import unittest
6import warnings
Walter Dörwaldc69d1c42005-11-21 17:48:12 +00007
Benjamin Petersonee8712c2008-05-20 21:35:26 +00008from test import support
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +00009
Walter Dörwaldc69d1c42005-11-21 17:48:12 +000010class PlatformTest(unittest.TestCase):
11 def test_architecture(self):
12 res = platform.architecture()
13
Brian Curtin3b4499c2010-12-28 14:31:47 +000014 @support.skip_unless_symlink
Brian Curtind40e6f72010-07-08 21:39:08 +000015 def test_architecture_via_symlink(self): # issue3762
16 # On Windows, the EXE needs to know where pythonXY.dll is at so we have
17 # to add the directory to the path.
18 if sys.platform == "win32":
Brian Curtin74e45612010-07-09 15:58:59 +000019 os.environ["Path"] = "{};{}".format(
20 os.path.dirname(sys.executable), os.environ["Path"])
Brian Curtind40e6f72010-07-08 21:39:08 +000021
22 def get(python):
23 cmd = [python, '-c',
24 'import platform; print(platform.architecture())']
25 p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
26 return p.communicate()
27 real = os.path.realpath(sys.executable)
28 link = os.path.abspath(support.TESTFN)
29 os.symlink(real, link)
30 try:
31 self.assertEqual(get(real), get(link))
32 finally:
33 os.remove(link)
Hirokazu Yamamotoe6748402008-10-06 04:51:11 +000034
Walter Dörwaldc69d1c42005-11-21 17:48:12 +000035 def test_platform(self):
36 for aliased in (False, True):
37 for terse in (False, True):
38 res = platform.platform(aliased, terse)
39
Benjamin Peterson5f28b7b2009-03-26 21:49:58 +000040 def test_system(self):
41 res = platform.system()
42
43 def test_node(self):
44 res = platform.node()
45
46 def test_release(self):
47 res = platform.release()
48
49 def test_version(self):
50 res = platform.version()
51
52 def test_machine(self):
53 res = platform.machine()
54
Walter Dörwaldc69d1c42005-11-21 17:48:12 +000055 def test_processor(self):
56 res = platform.processor()
57
Benjamin Petersone549ead2009-03-28 21:42:05 +000058 def setUp(self):
59 self.save_version = sys.version
Georg Brandl82562422011-03-05 21:09:22 +010060 self.save_mercurial = sys._mercurial
Benjamin Petersone549ead2009-03-28 21:42:05 +000061 self.save_platform = sys.platform
Benjamin Peterson5f28b7b2009-03-26 21:49:58 +000062
Benjamin Petersone549ead2009-03-28 21:42:05 +000063 def tearDown(self):
64 sys.version = self.save_version
Georg Brandl82562422011-03-05 21:09:22 +010065 sys._mercurial = self.save_mercurial
Benjamin Petersone549ead2009-03-28 21:42:05 +000066 sys.platform = self.save_platform
Benjamin Peterson5f28b7b2009-03-26 21:49:58 +000067
Benjamin Petersone549ead2009-03-28 21:42:05 +000068 def test_sys_version(self):
69 # Old test.
70 for input, output in (
71 ('2.4.3 (#1, Jun 21 2006, 13:54:21) \n[GCC 3.3.4 (pre 3.3.5 20040809)]',
72 ('CPython', '2.4.3', '', '', '1', 'Jun 21 2006 13:54:21', 'GCC 3.3.4 (pre 3.3.5 20040809)')),
73 ('IronPython 1.0.60816 on .NET 2.0.50727.42',
74 ('IronPython', '1.0.60816', '', '', '', '', '.NET 2.0.50727.42')),
75 ('IronPython 1.0 (1.0.61005.1977) on .NET 2.0.50727.42',
76 ('IronPython', '1.0.0', '', '', '', '', '.NET 2.0.50727.42')),
77 ):
78 # branch and revision are not "parsed", but fetched
Georg Brandl776e5862011-03-06 10:35:42 +010079 # from sys._mercurial. Ignore them
Benjamin Petersone549ead2009-03-28 21:42:05 +000080 (name, version, branch, revision, buildno, builddate, compiler) \
81 = platform._sys_version(input)
82 self.assertEqual(
83 (name, version, '', '', buildno, builddate, compiler), output)
Benjamin Peterson5f28b7b2009-03-26 21:49:58 +000084
Benjamin Petersone549ead2009-03-28 21:42:05 +000085 # Tests for python_implementation(), python_version(), python_branch(),
86 # python_revision(), python_build(), and python_compiler().
87 sys_versions = {
88 ("2.6.1 (r261:67515, Dec 6 2008, 15:26:00) \n[GCC 4.0.1 (Apple Computer, Inc. build 5370)]",
89 ('CPython', 'tags/r261', '67515'), self.save_platform)
90 :
91 ("CPython", "2.6.1", "tags/r261", "67515",
92 ('r261:67515', 'Dec 6 2008 15:26:00'),
93 'GCC 4.0.1 (Apple Computer, Inc. build 5370)'),
94 ("IronPython 2.0 (2.0.0.0) on .NET 2.0.50727.3053", None, "cli")
95 :
96 ("IronPython", "2.0.0", "", "", ("", ""),
97 ".NET 2.0.50727.3053"),
98 ("2.5 (trunk:6107, Mar 26 2009, 13:02:18) \n[Java HotSpot(TM) Client VM (\"Apple Computer, Inc.\")]",
99 ('Jython', 'trunk', '6107'), "java1.5.0_16")
100 :
101 ("Jython", "2.5.0", "trunk", "6107",
102 ('trunk:6107', 'Mar 26 2009'), "java1.5.0_16"),
103 ("2.5.2 (63378, Mar 26 2009, 18:03:29)\n[PyPy 1.0.0]",
104 ('PyPy', 'trunk', '63378'), self.save_platform)
105 :
106 ("PyPy", "2.5.2", "trunk", "63378", ('63378', 'Mar 26 2009'),
107 "")
108 }
109 for (version_tag, subversion, sys_platform), info in \
110 sys_versions.items():
111 sys.version = version_tag
112 if subversion is None:
Georg Brandl82562422011-03-05 21:09:22 +0100113 if hasattr(sys, "_mercurial"):
114 del sys._mercurial
Benjamin Petersone549ead2009-03-28 21:42:05 +0000115 else:
Georg Brandl82562422011-03-05 21:09:22 +0100116 sys._mercurial = subversion
Benjamin Petersone549ead2009-03-28 21:42:05 +0000117 if sys_platform is not None:
118 sys.platform = sys_platform
119 self.assertEqual(platform.python_implementation(), info[0])
120 self.assertEqual(platform.python_version(), info[1])
121 self.assertEqual(platform.python_branch(), info[2])
122 self.assertEqual(platform.python_revision(), info[3])
123 self.assertEqual(platform.python_build(), info[4])
124 self.assertEqual(platform.python_compiler(), info[5])
Walter Dörwaldc69d1c42005-11-21 17:48:12 +0000125
Walter Dörwaldc69d1c42005-11-21 17:48:12 +0000126 def test_system_alias(self):
127 res = platform.system_alias(
128 platform.system(),
129 platform.release(),
130 platform.version(),
131 )
132
133 def test_uname(self):
134 res = platform.uname()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000135 self.assertTrue(any(res))
Walter Dörwaldc69d1c42005-11-21 17:48:12 +0000136
R. David Murrayca2edce2010-03-22 17:48:48 +0000137 @unittest.skipUnless(sys.platform.startswith('win'), "windows only test")
138 def test_uname_win32_ARCHITEW6432(self):
139 # Issue 7860: make sure we get architecture from the correct variable
140 # on 64 bit Windows: if PROCESSOR_ARCHITEW6432 exists we should be
141 # using it, per
142 # http://blogs.msdn.com/david.wang/archive/2006/03/26/HOWTO-Detect-Process-Bitness.aspx
143 try:
R. David Murraya1135542010-03-24 00:29:21 +0000144 with support.EnvironmentVarGuard() as environ:
R. David Murrayca2edce2010-03-22 17:48:48 +0000145 if 'PROCESSOR_ARCHITEW6432' in environ:
146 del environ['PROCESSOR_ARCHITEW6432']
147 environ['PROCESSOR_ARCHITECTURE'] = 'foo'
148 platform._uname_cache = None
149 system, node, release, version, machine, processor = platform.uname()
150 self.assertEqual(machine, 'foo')
151 environ['PROCESSOR_ARCHITEW6432'] = 'bar'
152 platform._uname_cache = None
153 system, node, release, version, machine, processor = platform.uname()
154 self.assertEqual(machine, 'bar')
155 finally:
156 platform._uname_cache = None
157
Walter Dörwaldc69d1c42005-11-21 17:48:12 +0000158 def test_java_ver(self):
159 res = platform.java_ver()
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000160 if sys.platform == 'java':
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000161 self.assertTrue(all(res))
Walter Dörwaldc69d1c42005-11-21 17:48:12 +0000162
163 def test_win32_ver(self):
164 res = platform.win32_ver()
165
166 def test_mac_ver(self):
167 res = platform.mac_ver()
Benjamin Peterson856ff5f2008-05-29 21:22:40 +0000168
Amaury Forgeot d'Arc3b409272008-06-10 21:44:58 +0000169 if platform.uname()[0] == 'Darwin':
Benjamin Peterson856ff5f2008-05-29 21:22:40 +0000170 # We're on a MacOSX system, check that
171 # the right version information is returned
172 fd = os.popen('sw_vers', 'r')
173 real_ver = None
174 for ln in fd:
175 if ln.startswith('ProductVersion:'):
176 real_ver = ln.strip().split()[-1]
177 break
178 fd.close()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000179 self.assertFalse(real_ver is None)
Brett Cannon353411d2009-09-03 21:29:20 +0000180 result_list = res[0].split('.')
181 expect_list = real_ver.split('.')
182 len_diff = len(result_list) - len(expect_list)
183 # On Snow Leopard, sw_vers reports 10.6.0 as 10.6
184 if len_diff > 0:
185 expect_list.extend(['0'] * len_diff)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000186 self.assertEqual(result_list, expect_list)
Benjamin Peterson856ff5f2008-05-29 21:22:40 +0000187
188 # res[1] claims to contain
189 # (version, dev_stage, non_release_version)
190 # That information is no longer available
Ezio Melottib3aedd42010-11-20 19:04:17 +0000191 self.assertEqual(res[1], ('', '', ''))
Benjamin Peterson856ff5f2008-05-29 21:22:40 +0000192
193 if sys.byteorder == 'little':
Ezio Melottib3aedd42010-11-20 19:04:17 +0000194 self.assertEqual(res[2], 'i386')
Benjamin Peterson856ff5f2008-05-29 21:22:40 +0000195 else:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000196 self.assertEqual(res[2], 'PowerPC')
Walter Dörwaldc69d1c42005-11-21 17:48:12 +0000197
Ronald Oussorene186e382010-07-23 11:54:59 +0000198
199 @unittest.skipUnless(sys.platform == 'darwin', "OSX only test")
200 def test_mac_ver_with_fork(self):
201 # Issue7895: platform.mac_ver() crashes when using fork without exec
202 #
203 # This test checks that the fix for that issue works.
204 #
205 pid = os.fork()
206 if pid == 0:
207 # child
208 info = platform.mac_ver()
209 os._exit(0)
210
211 else:
212 # parent
213 cpid, sts = os.waitpid(pid, 0)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000214 self.assertEqual(cpid, pid)
215 self.assertEqual(sts, 0)
Ronald Oussorene186e382010-07-23 11:54:59 +0000216
Walter Dörwaldc69d1c42005-11-21 17:48:12 +0000217 def test_dist(self):
218 res = platform.dist()
219
220 def test_libc_ver(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000221 import os
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000222 if os.path.isdir(sys.executable) and \
223 os.path.exists(sys.executable+'.exe'):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000224 # Cygwin horror
Georg Brandl89fad142010-03-14 10:23:39 +0000225 executable = sys.executable + '.exe'
226 else:
227 executable = sys.executable
228 res = platform.libc_ver(executable)
Walter Dörwaldc69d1c42005-11-21 17:48:12 +0000229
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000230 def test_parse_release_file(self):
231
232 for input, output in (
233 # Examples of release file contents:
234 ('SuSE Linux 9.3 (x86-64)', ('SuSE Linux ', '9.3', 'x86-64')),
235 ('SUSE LINUX 10.1 (X86-64)', ('SUSE LINUX ', '10.1', 'X86-64')),
236 ('SUSE LINUX 10.1 (i586)', ('SUSE LINUX ', '10.1', 'i586')),
237 ('Fedora Core release 5 (Bordeaux)', ('Fedora Core', '5', 'Bordeaux')),
238 ('Red Hat Linux release 8.0 (Psyche)', ('Red Hat Linux', '8.0', 'Psyche')),
239 ('Red Hat Linux release 9 (Shrike)', ('Red Hat Linux', '9', 'Shrike')),
240 ('Red Hat Enterprise Linux release 4 (Nahant)', ('Red Hat Enterprise Linux', '4', 'Nahant')),
241 ('CentOS release 4', ('CentOS', '4', None)),
242 ('Rocks release 4.2.1 (Cydonia)', ('Rocks', '4.2.1', 'Cydonia')),
Benjamin Peterson25001472010-01-25 03:37:42 +0000243 ('', ('', '', '')), # If there's nothing there.
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000244 ):
245 self.assertEqual(platform._parse_release_file(input), output)
246
Victor Stinner1dfd3802011-03-03 12:54:07 +0000247 def test_popen(self):
Victor Stinnerff45fed2011-03-03 14:07:21 +0000248 mswindows = (sys.platform == "win32")
249
250 if mswindows:
251 command = '"{}" -c "print(\'Hello\')"'.format(sys.executable)
252 else:
253 command = "'{}' -c 'print(\"Hello\")'".format(sys.executable)
Victor Stinner3a38a6d2011-06-10 13:59:59 +0200254 with warnings.catch_warnings():
255 warnings.simplefilter("ignore", DeprecationWarning)
256 with platform.popen(command) as stdout:
257 hello = stdout.read().strip()
258 stdout.close()
259 self.assertEqual(hello, "Hello")
Victor Stinner1dfd3802011-03-03 12:54:07 +0000260
Victor Stinner1dfd3802011-03-03 12:54:07 +0000261 data = 'plop'
Victor Stinnerff45fed2011-03-03 14:07:21 +0000262 if mswindows:
263 command = '"{}" -c "import sys; data=sys.stdin.read(); exit(len(data))"'
264 else:
265 command = "'{}' -c 'import sys; data=sys.stdin.read(); exit(len(data))'"
266 command = command.format(sys.executable)
Victor Stinner3a38a6d2011-06-10 13:59:59 +0200267 with warnings.catch_warnings():
268 warnings.simplefilter("ignore", DeprecationWarning)
269 with platform.popen(command, 'w') as stdin:
270 stdout = stdin.write(data)
271 ret = stdin.close()
272 self.assertIsNotNone(ret)
273 if os.name == 'nt':
274 returncode = ret
275 else:
276 returncode = ret >> 8
277 self.assertEqual(returncode, len(data))
Victor Stinner1dfd3802011-03-03 12:54:07 +0000278
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000279
Walter Dörwaldc69d1c42005-11-21 17:48:12 +0000280def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000281 support.run_unittest(
Walter Dörwaldc69d1c42005-11-21 17:48:12 +0000282 PlatformTest
283 )
284
285if __name__ == '__main__':
286 test_main()