1 | import sys
|
---|
2 | import os
|
---|
3 | import unittest
|
---|
4 | import platform
|
---|
5 | import subprocess
|
---|
6 |
|
---|
7 | from test import test_support
|
---|
8 |
|
---|
9 | class PlatformTest(unittest.TestCase):
|
---|
10 | def test_architecture(self):
|
---|
11 | res = platform.architecture()
|
---|
12 |
|
---|
13 | if hasattr(os, "symlink"):
|
---|
14 | def test_architecture_via_symlink(self): # issue3762
|
---|
15 | def get(python):
|
---|
16 | cmd = [python, '-c',
|
---|
17 | 'import platform; print platform.architecture()']
|
---|
18 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
|
---|
19 | return p.communicate()
|
---|
20 | real = os.path.realpath(sys.executable)
|
---|
21 | link = os.path.abspath(test_support.TESTFN)
|
---|
22 | os.symlink(real, link)
|
---|
23 | try:
|
---|
24 | self.assertEqual(get(real), get(link))
|
---|
25 | finally:
|
---|
26 | os.remove(link)
|
---|
27 |
|
---|
28 | def test_platform(self):
|
---|
29 | for aliased in (False, True):
|
---|
30 | for terse in (False, True):
|
---|
31 | res = platform.platform(aliased, terse)
|
---|
32 |
|
---|
33 | def test_system(self):
|
---|
34 | res = platform.system()
|
---|
35 |
|
---|
36 | def test_node(self):
|
---|
37 | res = platform.node()
|
---|
38 |
|
---|
39 | def test_release(self):
|
---|
40 | res = platform.release()
|
---|
41 |
|
---|
42 | def test_version(self):
|
---|
43 | res = platform.version()
|
---|
44 |
|
---|
45 | def test_machine(self):
|
---|
46 | res = platform.machine()
|
---|
47 |
|
---|
48 | def test_processor(self):
|
---|
49 | res = platform.processor()
|
---|
50 |
|
---|
51 | def setUp(self):
|
---|
52 | self.save_version = sys.version
|
---|
53 | self.save_subversion = sys.subversion
|
---|
54 | self.save_platform = sys.platform
|
---|
55 |
|
---|
56 | def tearDown(self):
|
---|
57 | sys.version = self.save_version
|
---|
58 | sys.subversion = self.save_subversion
|
---|
59 | sys.platform = self.save_platform
|
---|
60 |
|
---|
61 | def test_sys_version(self):
|
---|
62 | # Old test.
|
---|
63 | for input, output in (
|
---|
64 | ('2.4.3 (#1, Jun 21 2006, 13:54:21) \n[GCC 3.3.4 (pre 3.3.5 20040809)]',
|
---|
65 | ('CPython', '2.4.3', '', '', '1', 'Jun 21 2006 13:54:21', 'GCC 3.3.4 (pre 3.3.5 20040809)')),
|
---|
66 | ('IronPython 1.0.60816 on .NET 2.0.50727.42',
|
---|
67 | ('IronPython', '1.0.60816', '', '', '', '', '.NET 2.0.50727.42')),
|
---|
68 | ('IronPython 1.0 (1.0.61005.1977) on .NET 2.0.50727.42',
|
---|
69 | ('IronPython', '1.0.0', '', '', '', '', '.NET 2.0.50727.42')),
|
---|
70 | ):
|
---|
71 | # branch and revision are not "parsed", but fetched
|
---|
72 | # from sys.subversion. Ignore them
|
---|
73 | (name, version, branch, revision, buildno, builddate, compiler) \
|
---|
74 | = platform._sys_version(input)
|
---|
75 | self.assertEqual(
|
---|
76 | (name, version, '', '', buildno, builddate, compiler), output)
|
---|
77 |
|
---|
78 | # Tests for python_implementation(), python_version(), python_branch(),
|
---|
79 | # python_revision(), python_build(), and python_compiler().
|
---|
80 | sys_versions = {
|
---|
81 | ("2.6.1 (r261:67515, Dec 6 2008, 15:26:00) \n[GCC 4.0.1 (Apple Computer, Inc. build 5370)]",
|
---|
82 | ('CPython', 'tags/r261', '67515'), self.save_platform)
|
---|
83 | :
|
---|
84 | ("CPython", "2.6.1", "tags/r261", "67515",
|
---|
85 | ('r261:67515', 'Dec 6 2008 15:26:00'),
|
---|
86 | 'GCC 4.0.1 (Apple Computer, Inc. build 5370)'),
|
---|
87 |
|
---|
88 | ("IronPython 2.0 (2.0.0.0) on .NET 2.0.50727.3053", None, "cli")
|
---|
89 | :
|
---|
90 | ("IronPython", "2.0.0", "", "", ("", ""),
|
---|
91 | ".NET 2.0.50727.3053"),
|
---|
92 |
|
---|
93 | ("2.6.1 (IronPython 2.6.1 (2.6.10920.0) on .NET 2.0.50727.1433)", None, "cli")
|
---|
94 | :
|
---|
95 | ("IronPython", "2.6.1", "", "", ("", ""),
|
---|
96 | ".NET 2.0.50727.1433"),
|
---|
97 |
|
---|
98 | ("2.7.4 (IronPython 2.7.4 (2.7.0.40) on Mono 4.0.30319.1 (32-bit))", None, "cli")
|
---|
99 | :
|
---|
100 | ("IronPython", "2.7.4", "", "", ("", ""),
|
---|
101 | "Mono 4.0.30319.1 (32-bit)"),
|
---|
102 |
|
---|
103 | ("2.5 (trunk:6107, Mar 26 2009, 13:02:18) \n[Java HotSpot(TM) Client VM (\"Apple Computer, Inc.\")]",
|
---|
104 | ('Jython', 'trunk', '6107'), "java1.5.0_16")
|
---|
105 | :
|
---|
106 | ("Jython", "2.5.0", "trunk", "6107",
|
---|
107 | ('trunk:6107', 'Mar 26 2009'), "java1.5.0_16"),
|
---|
108 |
|
---|
109 | ("2.5.2 (63378, Mar 26 2009, 18:03:29)\n[PyPy 1.0.0]",
|
---|
110 | ('PyPy', 'trunk', '63378'), self.save_platform)
|
---|
111 | :
|
---|
112 | ("PyPy", "2.5.2", "trunk", "63378", ('63378', 'Mar 26 2009'),
|
---|
113 | "")
|
---|
114 | }
|
---|
115 | for (version_tag, subversion, sys_platform), info in \
|
---|
116 | sys_versions.iteritems():
|
---|
117 | sys.version = version_tag
|
---|
118 | if subversion is None:
|
---|
119 | if hasattr(sys, "subversion"):
|
---|
120 | del sys.subversion
|
---|
121 | else:
|
---|
122 | sys.subversion = subversion
|
---|
123 | if sys_platform is not None:
|
---|
124 | sys.platform = sys_platform
|
---|
125 | self.assertEqual(platform.python_implementation(), info[0])
|
---|
126 | self.assertEqual(platform.python_version(), info[1])
|
---|
127 | self.assertEqual(platform.python_branch(), info[2])
|
---|
128 | self.assertEqual(platform.python_revision(), info[3])
|
---|
129 | self.assertEqual(platform.python_build(), info[4])
|
---|
130 | self.assertEqual(platform.python_compiler(), info[5])
|
---|
131 |
|
---|
132 | def test_system_alias(self):
|
---|
133 | res = platform.system_alias(
|
---|
134 | platform.system(),
|
---|
135 | platform.release(),
|
---|
136 | platform.version(),
|
---|
137 | )
|
---|
138 |
|
---|
139 | def test_uname(self):
|
---|
140 | res = platform.uname()
|
---|
141 | self.assertTrue(any(res))
|
---|
142 |
|
---|
143 | @unittest.skipUnless(sys.platform.startswith('win'), "windows only test")
|
---|
144 | def test_uname_win32_ARCHITEW6432(self):
|
---|
145 | # Issue 7860: make sure we get architecture from the correct variable
|
---|
146 | # on 64 bit Windows: if PROCESSOR_ARCHITEW6432 exists we should be
|
---|
147 | # using it, per
|
---|
148 | # http://blogs.msdn.com/david.wang/archive/2006/03/26/HOWTO-Detect-Process-Bitness.aspx
|
---|
149 | try:
|
---|
150 | with test_support.EnvironmentVarGuard() as environ:
|
---|
151 | if 'PROCESSOR_ARCHITEW6432' in environ:
|
---|
152 | del environ['PROCESSOR_ARCHITEW6432']
|
---|
153 | environ['PROCESSOR_ARCHITECTURE'] = 'foo'
|
---|
154 | platform._uname_cache = None
|
---|
155 | system, node, release, version, machine, processor = platform.uname()
|
---|
156 | self.assertEqual(machine, 'foo')
|
---|
157 | environ['PROCESSOR_ARCHITEW6432'] = 'bar'
|
---|
158 | platform._uname_cache = None
|
---|
159 | system, node, release, version, machine, processor = platform.uname()
|
---|
160 | self.assertEqual(machine, 'bar')
|
---|
161 | finally:
|
---|
162 | platform._uname_cache = None
|
---|
163 |
|
---|
164 | def test_java_ver(self):
|
---|
165 | res = platform.java_ver()
|
---|
166 | if sys.platform == 'java':
|
---|
167 | self.assertTrue(all(res))
|
---|
168 |
|
---|
169 | def test_win32_ver(self):
|
---|
170 | res = platform.win32_ver()
|
---|
171 |
|
---|
172 | def test_mac_ver(self):
|
---|
173 | res = platform.mac_ver()
|
---|
174 |
|
---|
175 | try:
|
---|
176 | import gestalt
|
---|
177 | except ImportError:
|
---|
178 | have_toolbox_glue = False
|
---|
179 | else:
|
---|
180 | have_toolbox_glue = True
|
---|
181 |
|
---|
182 | if have_toolbox_glue and platform.uname()[0] == 'Darwin':
|
---|
183 | # We're on a MacOSX system, check that
|
---|
184 | # the right version information is returned
|
---|
185 | fd = os.popen('sw_vers', 'r')
|
---|
186 | real_ver = None
|
---|
187 | for ln in fd:
|
---|
188 | if ln.startswith('ProductVersion:'):
|
---|
189 | real_ver = ln.strip().split()[-1]
|
---|
190 | break
|
---|
191 | fd.close()
|
---|
192 | self.assertFalse(real_ver is None)
|
---|
193 | result_list = res[0].split('.')
|
---|
194 | expect_list = real_ver.split('.')
|
---|
195 | len_diff = len(result_list) - len(expect_list)
|
---|
196 | # On Snow Leopard, sw_vers reports 10.6.0 as 10.6
|
---|
197 | if len_diff > 0:
|
---|
198 | expect_list.extend(['0'] * len_diff)
|
---|
199 | self.assertEqual(result_list, expect_list)
|
---|
200 |
|
---|
201 | # res[1] claims to contain
|
---|
202 | # (version, dev_stage, non_release_version)
|
---|
203 | # That information is no longer available
|
---|
204 | self.assertEqual(res[1], ('', '', ''))
|
---|
205 |
|
---|
206 | if sys.byteorder == 'little':
|
---|
207 | self.assertIn(res[2], ('i386', 'x86_64'))
|
---|
208 | else:
|
---|
209 | self.assertEqual(res[2], 'PowerPC')
|
---|
210 |
|
---|
211 |
|
---|
212 | @unittest.skipUnless(sys.platform == 'darwin', "OSX only test")
|
---|
213 | def test_mac_ver_with_fork(self):
|
---|
214 | # Issue7895: platform.mac_ver() crashes when using fork without exec
|
---|
215 | #
|
---|
216 | # This test checks that the fix for that issue works.
|
---|
217 | #
|
---|
218 | pid = os.fork()
|
---|
219 | if pid == 0:
|
---|
220 | # child
|
---|
221 | info = platform.mac_ver()
|
---|
222 | os._exit(0)
|
---|
223 |
|
---|
224 | else:
|
---|
225 | # parent
|
---|
226 | cpid, sts = os.waitpid(pid, 0)
|
---|
227 | self.assertEqual(cpid, pid)
|
---|
228 | self.assertEqual(sts, 0)
|
---|
229 |
|
---|
230 | def test_dist(self):
|
---|
231 | res = platform.dist()
|
---|
232 |
|
---|
233 | def test_libc_ver(self):
|
---|
234 | import os
|
---|
235 | if os.path.isdir(sys.executable) and \
|
---|
236 | os.path.exists(sys.executable+'.exe'):
|
---|
237 | # Cygwin horror
|
---|
238 | executable = sys.executable + '.exe'
|
---|
239 | else:
|
---|
240 | executable = sys.executable
|
---|
241 | res = platform.libc_ver(executable)
|
---|
242 |
|
---|
243 | def test_parse_release_file(self):
|
---|
244 |
|
---|
245 | for input, output in (
|
---|
246 | # Examples of release file contents:
|
---|
247 | ('SuSE Linux 9.3 (x86-64)', ('SuSE Linux ', '9.3', 'x86-64')),
|
---|
248 | ('SUSE LINUX 10.1 (X86-64)', ('SUSE LINUX ', '10.1', 'X86-64')),
|
---|
249 | ('SUSE LINUX 10.1 (i586)', ('SUSE LINUX ', '10.1', 'i586')),
|
---|
250 | ('Fedora Core release 5 (Bordeaux)', ('Fedora Core', '5', 'Bordeaux')),
|
---|
251 | ('Red Hat Linux release 8.0 (Psyche)', ('Red Hat Linux', '8.0', 'Psyche')),
|
---|
252 | ('Red Hat Linux release 9 (Shrike)', ('Red Hat Linux', '9', 'Shrike')),
|
---|
253 | ('Red Hat Enterprise Linux release 4 (Nahant)', ('Red Hat Enterprise Linux', '4', 'Nahant')),
|
---|
254 | ('CentOS release 4', ('CentOS', '4', None)),
|
---|
255 | ('Rocks release 4.2.1 (Cydonia)', ('Rocks', '4.2.1', 'Cydonia')),
|
---|
256 | ('', ('', '', '')), # If there's nothing there.
|
---|
257 | ):
|
---|
258 | self.assertEqual(platform._parse_release_file(input), output)
|
---|
259 |
|
---|
260 |
|
---|
261 | def test_main():
|
---|
262 | test_support.run_unittest(
|
---|
263 | PlatformTest
|
---|
264 | )
|
---|
265 |
|
---|
266 | if __name__ == '__main__':
|
---|
267 | test_main()
|
---|