move sections
[python/dscho.git] / Lib / test / test_platform.py
blobafe304238c6d708e17aabdead4e247bb50eff6c3
1 import sys
2 import os
3 import unittest
4 import platform
5 import subprocess
7 from test import test_support
9 class PlatformTest(unittest.TestCase):
10 def test_architecture(self):
11 res = platform.architecture()
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)
28 def test_platform(self):
29 for aliased in (False, True):
30 for terse in (False, True):
31 res = platform.platform(aliased, terse)
33 def test_system(self):
34 res = platform.system()
36 def test_node(self):
37 res = platform.node()
39 def test_release(self):
40 res = platform.release()
42 def test_version(self):
43 res = platform.version()
45 def test_machine(self):
46 res = platform.machine()
48 def test_processor(self):
49 res = platform.processor()
51 def setUp(self):
52 self.save_version = sys.version
53 self.save_subversion = sys.subversion
54 self.save_platform = sys.platform
56 def tearDown(self):
57 sys.version = self.save_version
58 sys.subversion = self.save_subversion
59 sys.platform = self.save_platform
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')),
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)
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)
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 ("IronPython 2.0 (2.0.0.0) on .NET 2.0.50727.3053", None, "cli")
89 ("IronPython", "2.0.0", "", "", ("", ""),
90 ".NET 2.0.50727.3053"),
91 ("2.5 (trunk:6107, Mar 26 2009, 13:02:18) \n[Java HotSpot(TM) Client VM (\"Apple Computer, Inc.\")]",
92 ('Jython', 'trunk', '6107'), "java1.5.0_16")
94 ("Jython", "2.5.0", "trunk", "6107",
95 ('trunk:6107', 'Mar 26 2009'), "java1.5.0_16"),
96 ("2.5.2 (63378, Mar 26 2009, 18:03:29)\n[PyPy 1.0.0]",
97 ('PyPy', 'trunk', '63378'), self.save_platform)
99 ("PyPy", "2.5.2", "trunk", "63378", ('63378', 'Mar 26 2009'),
102 for (version_tag, subversion, sys_platform), info in \
103 sys_versions.iteritems():
104 sys.version = version_tag
105 if subversion is None:
106 if hasattr(sys, "subversion"):
107 del sys.subversion
108 else:
109 sys.subversion = subversion
110 if sys_platform is not None:
111 sys.platform = sys_platform
112 self.assertEqual(platform.python_implementation(), info[0])
113 self.assertEqual(platform.python_version(), info[1])
114 self.assertEqual(platform.python_branch(), info[2])
115 self.assertEqual(platform.python_revision(), info[3])
116 self.assertEqual(platform.python_build(), info[4])
117 self.assertEqual(platform.python_compiler(), info[5])
119 def test_system_alias(self):
120 res = platform.system_alias(
121 platform.system(),
122 platform.release(),
123 platform.version(),
126 def test_uname(self):
127 res = platform.uname()
128 self.assertTrue(any(res))
130 @unittest.skipUnless(sys.platform.startswith('win'), "windows only test")
131 def test_uname_win32_ARCHITEW6432(self):
132 # Issue 7860: make sure we get architecture from the correct variable
133 # on 64 bit Windows: if PROCESSOR_ARCHITEW6432 exists we should be
134 # using it, per
135 # http://blogs.msdn.com/david.wang/archive/2006/03/26/HOWTO-Detect-Process-Bitness.aspx
136 try:
137 with test_support.EnvironmentVarGuard() as environ:
138 if 'PROCESSOR_ARCHITEW6432' in environ:
139 del environ['PROCESSOR_ARCHITEW6432']
140 environ['PROCESSOR_ARCHITECTURE'] = 'foo'
141 platform._uname_cache = None
142 system, node, release, version, machine, processor = platform.uname()
143 self.assertEqual(machine, 'foo')
144 environ['PROCESSOR_ARCHITEW6432'] = 'bar'
145 platform._uname_cache = None
146 system, node, release, version, machine, processor = platform.uname()
147 self.assertEqual(machine, 'bar')
148 finally:
149 platform._uname_cache = None
151 def test_java_ver(self):
152 res = platform.java_ver()
153 if sys.platform == 'java':
154 self.assertTrue(all(res))
156 def test_win32_ver(self):
157 res = platform.win32_ver()
159 def test_mac_ver(self):
160 res = platform.mac_ver()
162 try:
163 import gestalt
164 except ImportError:
165 have_toolbox_glue = False
166 else:
167 have_toolbox_glue = True
169 if have_toolbox_glue and platform.uname()[0] == 'Darwin':
170 # 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()
179 self.assertFalse(real_ver is None)
180 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)
186 self.assertEquals(result_list, expect_list)
188 # res[1] claims to contain
189 # (version, dev_stage, non_release_version)
190 # That information is no longer available
191 self.assertEquals(res[1], ('', '', ''))
193 if sys.byteorder == 'little':
194 self.assertEquals(res[2], 'i386')
195 else:
196 self.assertEquals(res[2], 'PowerPC')
198 def test_dist(self):
199 res = platform.dist()
201 def test_libc_ver(self):
202 import os
203 if os.path.isdir(sys.executable) and \
204 os.path.exists(sys.executable+'.exe'):
205 # Cygwin horror
206 executable = sys.executable + '.exe'
207 else:
208 executable = sys.executable
209 res = platform.libc_ver(executable)
211 def test_parse_release_file(self):
213 for input, output in (
214 # Examples of release file contents:
215 ('SuSE Linux 9.3 (x86-64)', ('SuSE Linux ', '9.3', 'x86-64')),
216 ('SUSE LINUX 10.1 (X86-64)', ('SUSE LINUX ', '10.1', 'X86-64')),
217 ('SUSE LINUX 10.1 (i586)', ('SUSE LINUX ', '10.1', 'i586')),
218 ('Fedora Core release 5 (Bordeaux)', ('Fedora Core', '5', 'Bordeaux')),
219 ('Red Hat Linux release 8.0 (Psyche)', ('Red Hat Linux', '8.0', 'Psyche')),
220 ('Red Hat Linux release 9 (Shrike)', ('Red Hat Linux', '9', 'Shrike')),
221 ('Red Hat Enterprise Linux release 4 (Nahant)', ('Red Hat Enterprise Linux', '4', 'Nahant')),
222 ('CentOS release 4', ('CentOS', '4', None)),
223 ('Rocks release 4.2.1 (Cydonia)', ('Rocks', '4.2.1', 'Cydonia')),
224 ('', ('', '', '')), # If there's nothing there.
226 self.assertEqual(platform._parse_release_file(input), output)
229 def test_main():
230 test_support.run_unittest(
231 PlatformTest
234 if __name__ == '__main__':
235 test_main()