blob: 7dd7eef2fedb1d4bb96cf99a0f34c394a996d3ce [file] [log] [blame]
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +00001import sys
Benjamin Peterson856ff5f2008-05-29 21:22:40 +00002import os
Walter Dörwaldc69d1c42005-11-21 17:48:12 +00003import unittest
Walter Dörwaldc69d1c42005-11-21 17:48:12 +00004import platform
Hirokazu Yamamotoe6748402008-10-06 04:51:11 +00005import subprocess
Walter Dörwaldc69d1c42005-11-21 17:48:12 +00006
Benjamin Petersonee8712c2008-05-20 21:35:26 +00007from test import support
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +00008
Walter Dörwaldc69d1c42005-11-21 17:48:12 +00009class PlatformTest(unittest.TestCase):
10 def test_architecture(self):
11 res = platform.architecture()
12
Brian Curtind40e6f72010-07-08 21:39:08 +000013 @support.skip_unless_symlink
14 def test_architecture_via_symlink(self): # issue3762
15 # On Windows, the EXE needs to know where pythonXY.dll is at so we have
16 # to add the directory to the path.
17 if sys.platform == "win32":
Brian Curtin74e45612010-07-09 15:58:59 +000018 os.environ["Path"] = "{};{}".format(
19 os.path.dirname(sys.executable), os.environ["Path"])
Brian Curtind40e6f72010-07-08 21:39:08 +000020
21 def get(python):
22 cmd = [python, '-c',
23 'import platform; print(platform.architecture())']
24 p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
25 return p.communicate()
26 real = os.path.realpath(sys.executable)
27 link = os.path.abspath(support.TESTFN)
28 os.symlink(real, link)
29 try:
30 self.assertEqual(get(real), get(link))
31 finally:
32 os.remove(link)
Hirokazu Yamamotoe6748402008-10-06 04:51:11 +000033
Walter Dörwaldc69d1c42005-11-21 17:48:12 +000034 def test_platform(self):
35 for aliased in (False, True):
36 for terse in (False, True):
37 res = platform.platform(aliased, terse)
38
Benjamin Peterson5f28b7b2009-03-26 21:49:58 +000039 def test_system(self):
40 res = platform.system()
41
42 def test_node(self):
43 res = platform.node()
44
45 def test_release(self):
46 res = platform.release()
47
48 def test_version(self):
49 res = platform.version()
50
51 def test_machine(self):
52 res = platform.machine()
53
Walter Dörwaldc69d1c42005-11-21 17:48:12 +000054 def test_processor(self):
55 res = platform.processor()
56
Benjamin Petersone549ead2009-03-28 21:42:05 +000057 def setUp(self):
58 self.save_version = sys.version
59 self.save_subversion = sys.subversion
60 self.save_platform = sys.platform
Benjamin Peterson5f28b7b2009-03-26 21:49:58 +000061
Benjamin Petersone549ead2009-03-28 21:42:05 +000062 def tearDown(self):
63 sys.version = self.save_version
64 sys.subversion = self.save_subversion
65 sys.platform = self.save_platform
Benjamin Peterson5f28b7b2009-03-26 21:49:58 +000066
Benjamin Petersone549ead2009-03-28 21:42:05 +000067 def test_sys_version(self):
68 # Old test.
69 for input, output in (
70 ('2.4.3 (#1, Jun 21 2006, 13:54:21) \n[GCC 3.3.4 (pre 3.3.5 20040809)]',
71 ('CPython', '2.4.3', '', '', '1', 'Jun 21 2006 13:54:21', 'GCC 3.3.4 (pre 3.3.5 20040809)')),
72 ('IronPython 1.0.60816 on .NET 2.0.50727.42',
73 ('IronPython', '1.0.60816', '', '', '', '', '.NET 2.0.50727.42')),
74 ('IronPython 1.0 (1.0.61005.1977) on .NET 2.0.50727.42',
75 ('IronPython', '1.0.0', '', '', '', '', '.NET 2.0.50727.42')),
76 ):
77 # branch and revision are not "parsed", but fetched
78 # from sys.subversion. Ignore them
79 (name, version, branch, revision, buildno, builddate, compiler) \
80 = platform._sys_version(input)
81 self.assertEqual(
82 (name, version, '', '', buildno, builddate, compiler), output)
Benjamin Peterson5f28b7b2009-03-26 21:49:58 +000083
Benjamin Petersone549ead2009-03-28 21:42:05 +000084 # Tests for python_implementation(), python_version(), python_branch(),
85 # python_revision(), python_build(), and python_compiler().
86 sys_versions = {
87 ("2.6.1 (r261:67515, Dec 6 2008, 15:26:00) \n[GCC 4.0.1 (Apple Computer, Inc. build 5370)]",
88 ('CPython', 'tags/r261', '67515'), self.save_platform)
89 :
90 ("CPython", "2.6.1", "tags/r261", "67515",
91 ('r261:67515', 'Dec 6 2008 15:26:00'),
92 'GCC 4.0.1 (Apple Computer, Inc. build 5370)'),
93 ("IronPython 2.0 (2.0.0.0) on .NET 2.0.50727.3053", None, "cli")
94 :
95 ("IronPython", "2.0.0", "", "", ("", ""),
96 ".NET 2.0.50727.3053"),
97 ("2.5 (trunk:6107, Mar 26 2009, 13:02:18) \n[Java HotSpot(TM) Client VM (\"Apple Computer, Inc.\")]",
98 ('Jython', 'trunk', '6107'), "java1.5.0_16")
99 :
100 ("Jython", "2.5.0", "trunk", "6107",
101 ('trunk:6107', 'Mar 26 2009'), "java1.5.0_16"),
102 ("2.5.2 (63378, Mar 26 2009, 18:03:29)\n[PyPy 1.0.0]",
103 ('PyPy', 'trunk', '63378'), self.save_platform)
104 :
105 ("PyPy", "2.5.2", "trunk", "63378", ('63378', 'Mar 26 2009'),
106 "")
107 }
108 for (version_tag, subversion, sys_platform), info in \
109 sys_versions.items():
110 sys.version = version_tag
111 if subversion is None:
112 if hasattr(sys, "subversion"):
113 del sys.subversion
114 else:
115 sys.subversion = subversion
116 if sys_platform is not None:
117 sys.platform = sys_platform
118 self.assertEqual(platform.python_implementation(), info[0])
119 self.assertEqual(platform.python_version(), info[1])
120 self.assertEqual(platform.python_branch(), info[2])
121 self.assertEqual(platform.python_revision(), info[3])
122 self.assertEqual(platform.python_build(), info[4])
123 self.assertEqual(platform.python_compiler(), info[5])
Walter Dörwaldc69d1c42005-11-21 17:48:12 +0000124
Walter Dörwaldc69d1c42005-11-21 17:48:12 +0000125 def test_system_alias(self):
126 res = platform.system_alias(
127 platform.system(),
128 platform.release(),
129 platform.version(),
130 )
131
132 def test_uname(self):
133 res = platform.uname()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000134 self.assertTrue(any(res))
Walter Dörwaldc69d1c42005-11-21 17:48:12 +0000135
R. David Murrayca2edce2010-03-22 17:48:48 +0000136 @unittest.skipUnless(sys.platform.startswith('win'), "windows only test")
137 def test_uname_win32_ARCHITEW6432(self):
138 # Issue 7860: make sure we get architecture from the correct variable
139 # on 64 bit Windows: if PROCESSOR_ARCHITEW6432 exists we should be
140 # using it, per
141 # http://blogs.msdn.com/david.wang/archive/2006/03/26/HOWTO-Detect-Process-Bitness.aspx
142 try:
R. David Murraya1135542010-03-24 00:29:21 +0000143 with support.EnvironmentVarGuard() as environ:
R. David Murrayca2edce2010-03-22 17:48:48 +0000144 if 'PROCESSOR_ARCHITEW6432' in environ:
145 del environ['PROCESSOR_ARCHITEW6432']
146 environ['PROCESSOR_ARCHITECTURE'] = 'foo'
147 platform._uname_cache = None
148 system, node, release, version, machine, processor = platform.uname()
149 self.assertEqual(machine, 'foo')
150 environ['PROCESSOR_ARCHITEW6432'] = 'bar'
151 platform._uname_cache = None
152 system, node, release, version, machine, processor = platform.uname()
153 self.assertEqual(machine, 'bar')
154 finally:
155 platform._uname_cache = None
156
Walter Dörwaldc69d1c42005-11-21 17:48:12 +0000157 def test_java_ver(self):
158 res = platform.java_ver()
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000159 if sys.platform == 'java':
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000160 self.assertTrue(all(res))
Walter Dörwaldc69d1c42005-11-21 17:48:12 +0000161
162 def test_win32_ver(self):
163 res = platform.win32_ver()
164
165 def test_mac_ver(self):
166 res = platform.mac_ver()
Benjamin Peterson856ff5f2008-05-29 21:22:40 +0000167
Amaury Forgeot d'Arc3b409272008-06-10 21:44:58 +0000168 if platform.uname()[0] == 'Darwin':
Benjamin Peterson856ff5f2008-05-29 21:22:40 +0000169 # We're on a MacOSX system, check that
170 # the right version information is returned
171 fd = os.popen('sw_vers', 'r')
172 real_ver = None
173 for ln in fd:
174 if ln.startswith('ProductVersion:'):
175 real_ver = ln.strip().split()[-1]
176 break
177 fd.close()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000178 self.assertFalse(real_ver is None)
Brett Cannon353411d2009-09-03 21:29:20 +0000179 result_list = res[0].split('.')
180 expect_list = real_ver.split('.')
181 len_diff = len(result_list) - len(expect_list)
182 # On Snow Leopard, sw_vers reports 10.6.0 as 10.6
183 if len_diff > 0:
184 expect_list.extend(['0'] * len_diff)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000185 self.assertEqual(result_list, expect_list)
Benjamin Peterson856ff5f2008-05-29 21:22:40 +0000186
187 # res[1] claims to contain
188 # (version, dev_stage, non_release_version)
189 # That information is no longer available
Ezio Melottib3aedd42010-11-20 19:04:17 +0000190 self.assertEqual(res[1], ('', '', ''))
Benjamin Peterson856ff5f2008-05-29 21:22:40 +0000191
192 if sys.byteorder == 'little':
Ezio Melottib3aedd42010-11-20 19:04:17 +0000193 self.assertEqual(res[2], 'i386')
Benjamin Peterson856ff5f2008-05-29 21:22:40 +0000194 else:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000195 self.assertEqual(res[2], 'PowerPC')
Walter Dörwaldc69d1c42005-11-21 17:48:12 +0000196
Ronald Oussorene186e382010-07-23 11:54:59 +0000197
198 @unittest.skipUnless(sys.platform == 'darwin', "OSX only test")
199 def test_mac_ver_with_fork(self):
200 # Issue7895: platform.mac_ver() crashes when using fork without exec
201 #
202 # This test checks that the fix for that issue works.
203 #
204 pid = os.fork()
205 if pid == 0:
206 # child
207 info = platform.mac_ver()
208 os._exit(0)
209
210 else:
211 # parent
212 cpid, sts = os.waitpid(pid, 0)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000213 self.assertEqual(cpid, pid)
214 self.assertEqual(sts, 0)
Ronald Oussorene186e382010-07-23 11:54:59 +0000215
Walter Dörwaldc69d1c42005-11-21 17:48:12 +0000216 def test_dist(self):
217 res = platform.dist()
218
219 def test_libc_ver(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000220 import os
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000221 if os.path.isdir(sys.executable) and \
222 os.path.exists(sys.executable+'.exe'):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000223 # Cygwin horror
Georg Brandl89fad142010-03-14 10:23:39 +0000224 executable = sys.executable + '.exe'
225 else:
226 executable = sys.executable
227 res = platform.libc_ver(executable)
Walter Dörwaldc69d1c42005-11-21 17:48:12 +0000228
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000229 def test_parse_release_file(self):
230
231 for input, output in (
232 # Examples of release file contents:
233 ('SuSE Linux 9.3 (x86-64)', ('SuSE Linux ', '9.3', 'x86-64')),
234 ('SUSE LINUX 10.1 (X86-64)', ('SUSE LINUX ', '10.1', 'X86-64')),
235 ('SUSE LINUX 10.1 (i586)', ('SUSE LINUX ', '10.1', 'i586')),
236 ('Fedora Core release 5 (Bordeaux)', ('Fedora Core', '5', 'Bordeaux')),
237 ('Red Hat Linux release 8.0 (Psyche)', ('Red Hat Linux', '8.0', 'Psyche')),
238 ('Red Hat Linux release 9 (Shrike)', ('Red Hat Linux', '9', 'Shrike')),
239 ('Red Hat Enterprise Linux release 4 (Nahant)', ('Red Hat Enterprise Linux', '4', 'Nahant')),
240 ('CentOS release 4', ('CentOS', '4', None)),
241 ('Rocks release 4.2.1 (Cydonia)', ('Rocks', '4.2.1', 'Cydonia')),
Benjamin Peterson25001472010-01-25 03:37:42 +0000242 ('', ('', '', '')), # If there's nothing there.
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000243 ):
244 self.assertEqual(platform._parse_release_file(input), output)
245
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000246
Walter Dörwaldc69d1c42005-11-21 17:48:12 +0000247def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000248 support.run_unittest(
Walter Dörwaldc69d1c42005-11-21 17:48:12 +0000249 PlatformTest
250 )
251
252if __name__ == '__main__':
253 test_main()