| 1 |
import sys |
| 2 |
import os |
| 3 |
import unittest |
| 4 |
import platform |
| 5 |
import subprocess |
| 6 |
|
| 7 |
from test import 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): |
| 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(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 |
|
| 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 |
('2.4.3 (truncation, date, t) \n[GCC]', |
| 71 |
('CPython', '2.4.3', '', '', 'truncation', 'date t', 'GCC')), |
| 72 |
('2.4.3 (truncation, date, ) \n[GCC]', |
| 73 |
('CPython', '2.4.3', '', '', 'truncation', 'date', 'GCC')), |
| 74 |
('2.4.3 (truncation, date,) \n[GCC]', |
| 75 |
('CPython', '2.4.3', '', '', 'truncation', 'date', 'GCC')), |
| 76 |
('2.4.3 (truncation, date) \n[GCC]', |
| 77 |
('CPython', '2.4.3', '', '', 'truncation', 'date', 'GCC')), |
| 78 |
('2.4.3 (truncation, d) \n[GCC]', |
| 79 |
('CPython', '2.4.3', '', '', 'truncation', 'd', 'GCC')), |
| 80 |
('2.4.3 (truncation, ) \n[GCC]', |
| 81 |
('CPython', '2.4.3', '', '', 'truncation', '', 'GCC')), |
| 82 |
('2.4.3 (truncation,) \n[GCC]', |
| 83 |
('CPython', '2.4.3', '', '', 'truncation', '', 'GCC')), |
| 84 |
('2.4.3 (truncation) \n[GCC]', |
| 85 |
('CPython', '2.4.3', '', '', 'truncation', '', 'GCC')), |
| 86 |
): |
| 87 |
|
| 88 |
|
| 89 |
(name, version, branch, revision, buildno, builddate, compiler) \ |
| 90 |
= platform._sys_version(input) |
| 91 |
self.assertEqual( |
| 92 |
(name, version, '', '', buildno, builddate, compiler), output) |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
sys_versions = { |
| 97 |
("2.6.1 (r261:67515, Dec 6 2008, 15:26:00) \n[GCC 4.0.1 (Apple Computer, Inc. build 5370)]", |
| 98 |
('CPython', 'tags/r261', '67515'), self.save_platform) |
| 99 |
: |
| 100 |
("CPython", "2.6.1", "tags/r261", "67515", |
| 101 |
('r261:67515', 'Dec 6 2008 15:26:00'), |
| 102 |
'GCC 4.0.1 (Apple Computer, Inc. build 5370)'), |
| 103 |
|
| 104 |
("IronPython 2.0 (2.0.0.0) on .NET 2.0.50727.3053", None, "cli") |
| 105 |
: |
| 106 |
("IronPython", "2.0.0", "", "", ("", ""), |
| 107 |
".NET 2.0.50727.3053"), |
| 108 |
|
| 109 |
("2.6.1 (IronPython 2.6.1 (2.6.10920.0) on .NET 2.0.50727.1433)", None, "cli") |
| 110 |
: |
| 111 |
("IronPython", "2.6.1", "", "", ("", ""), |
| 112 |
".NET 2.0.50727.1433"), |
| 113 |
|
| 114 |
("2.7.4 (IronPython 2.7.4 (2.7.0.40) on Mono 4.0.30319.1 (32-bit))", None, "cli") |
| 115 |
: |
| 116 |
("IronPython", "2.7.4", "", "", ("", ""), |
| 117 |
"Mono 4.0.30319.1 (32-bit)"), |
| 118 |
|
| 119 |
("2.5 (trunk:6107, Mar 26 2009, 13:02:18) \n[Java HotSpot(TM) Client VM (\"Apple Computer, Inc.\")]", |
| 120 |
('Jython', 'trunk', '6107'), "java1.5.0_16") |
| 121 |
: |
| 122 |
("Jython", "2.5.0", "trunk", "6107", |
| 123 |
('trunk:6107', 'Mar 26 2009'), "java1.5.0_16"), |
| 124 |
|
| 125 |
("2.5.2 (63378, Mar 26 2009, 18:03:29)\n[PyPy 1.0.0]", |
| 126 |
('PyPy', 'trunk', '63378'), self.save_platform) |
| 127 |
: |
| 128 |
("PyPy", "2.5.2", "trunk", "63378", ('63378', 'Mar 26 2009'), |
| 129 |
"") |
| 130 |
} |
| 131 |
for (version_tag, subversion, sys_platform), info in \ |
| 132 |
sys_versions.iteritems(): |
| 133 |
sys.version = version_tag |
| 134 |
if subversion is None: |
| 135 |
if hasattr(sys, "subversion"): |
| 136 |
del sys.subversion |
| 137 |
else: |
| 138 |
sys.subversion = subversion |
| 139 |
if sys_platform is not None: |
| 140 |
sys.platform = sys_platform |
| 141 |
self.assertEqual(platform.python_implementation(), info[0]) |
| 142 |
self.assertEqual(platform.python_version(), info[1]) |
| 143 |
self.assertEqual(platform.python_branch(), info[2]) |
| 144 |
self.assertEqual(platform.python_revision(), info[3]) |
| 145 |
self.assertEqual(platform.python_build(), info[4]) |
| 146 |
self.assertEqual(platform.python_compiler(), info[5]) |
| 147 |
|
| 148 |
def test_system_alias(self): |
| 149 |
res = platform.system_alias( |
| 150 |
platform.system(), |
| 151 |
platform.release(), |
| 152 |
platform.version(), |
| 153 |
) |
| 154 |
|
| 155 |
def test_uname(self): |
| 156 |
res = platform.uname() |
| 157 |
self.assertTrue(any(res)) |
| 158 |
|
| 159 |
@unittest.skipUnless(sys.platform.startswith('win'), "windows only test") |
| 160 |
def test_uname_win32_ARCHITEW6432(self): |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
try: |
| 166 |
with support.EnvironmentVarGuard() as environ: |
| 167 |
if 'PROCESSOR_ARCHITEW6432' in environ: |
| 168 |
del environ['PROCESSOR_ARCHITEW6432'] |
| 169 |
environ['PROCESSOR_ARCHITECTURE'] = 'foo' |
| 170 |
platform._uname_cache = None |
| 171 |
system, node, release, version, machine, processor = platform.uname() |
| 172 |
self.assertEqual(machine, 'foo') |
| 173 |
environ['PROCESSOR_ARCHITEW6432'] = 'bar' |
| 174 |
platform._uname_cache = None |
| 175 |
system, node, release, version, machine, processor = platform.uname() |
| 176 |
self.assertEqual(machine, 'bar') |
| 177 |
finally: |
| 178 |
platform._uname_cache = None |
| 179 |
|
| 180 |
def test_java_ver(self): |
| 181 |
res = platform.java_ver() |
| 182 |
if sys.platform == 'java': |
| 183 |
self.assertTrue(all(res)) |
| 184 |
|
| 185 |
def test_win32_ver(self): |
| 186 |
res = platform.win32_ver() |
| 187 |
|
| 188 |
def test_mac_ver(self): |
| 189 |
res = platform.mac_ver() |
| 190 |
|
| 191 |
try: |
| 192 |
import gestalt |
| 193 |
except ImportError: |
| 194 |
have_toolbox_glue = False |
| 195 |
else: |
| 196 |
have_toolbox_glue = True |
| 197 |
|
| 198 |
if have_toolbox_glue and platform.uname()[0] == 'Darwin': |
| 199 |
|
| 200 |
|
| 201 |
fd = os.popen('sw_vers', 'r') |
| 202 |
real_ver = None |
| 203 |
for ln in fd: |
| 204 |
if ln.startswith('ProductVersion:'): |
| 205 |
real_ver = ln.strip().split()[-1] |
| 206 |
break |
| 207 |
fd.close() |
| 208 |
self.assertFalse(real_ver is None) |
| 209 |
result_list = res[0].split('.') |
| 210 |
expect_list = real_ver.split('.') |
| 211 |
len_diff = len(result_list) - len(expect_list) |
| 212 |
|
| 213 |
if len_diff > 0: |
| 214 |
expect_list.extend(['0'] * len_diff) |
| 215 |
self.assertEqual(result_list, expect_list) |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
self.assertEqual(res[1], ('', '', '')) |
| 221 |
|
| 222 |
if sys.byteorder == 'little': |
| 223 |
self.assertIn(res[2], ('i386', 'x86_64')) |
| 224 |
else: |
| 225 |
self.assertEqual(res[2], 'PowerPC') |
| 226 |
|
| 227 |
|
| 228 |
@unittest.skipUnless(sys.platform == 'darwin', "OSX only test") |
| 229 |
def test_mac_ver_with_fork(self): |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
pid = os.fork() |
| 235 |
if pid == 0: |
| 236 |
|
| 237 |
info = platform.mac_ver() |
| 238 |
os._exit(0) |
| 239 |
|
| 240 |
else: |
| 241 |
|
| 242 |
cpid, sts = os.waitpid(pid, 0) |
| 243 |
self.assertEqual(cpid, pid) |
| 244 |
self.assertEqual(sts, 0) |
| 245 |
|
| 246 |
def test_dist(self): |
| 247 |
res = platform.dist() |
| 248 |
|
| 249 |
def test_libc_ver(self): |
| 250 |
if os.path.isdir(sys.executable) and \ |
| 251 |
os.path.exists(sys.executable+'.exe'): |
| 252 |
|
| 253 |
executable = sys.executable + '.exe' |
| 254 |
else: |
| 255 |
executable = sys.executable |
| 256 |
res = platform.libc_ver(executable) |
| 257 |
|
| 258 |
self.addCleanup(support.unlink, support.TESTFN) |
| 259 |
with open(support.TESTFN, 'wb') as f: |
| 260 |
f.write(b'x'*(16384-10)) |
| 261 |
f.write(b'GLIBC_1.23.4\0GLIBC_1.9\0GLIBC_1.21\0') |
| 262 |
self.assertEqual(platform.libc_ver(support.TESTFN), |
| 263 |
('glibc', '1.23.4')) |
| 264 |
|
| 265 |
def test_parse_release_file(self): |
| 266 |
|
| 267 |
for input, output in ( |
| 268 |
|
| 269 |
('SuSE Linux 9.3 (x86-64)', ('SuSE Linux ', '9.3', 'x86-64')), |
| 270 |
('SUSE LINUX 10.1 (X86-64)', ('SUSE LINUX ', '10.1', 'X86-64')), |
| 271 |
('SUSE LINUX 10.1 (i586)', ('SUSE LINUX ', '10.1', 'i586')), |
| 272 |
('Fedora Core release 5 (Bordeaux)', ('Fedora Core', '5', 'Bordeaux')), |
| 273 |
('Red Hat Linux release 8.0 (Psyche)', ('Red Hat Linux', '8.0', 'Psyche')), |
| 274 |
('Red Hat Linux release 9 (Shrike)', ('Red Hat Linux', '9', 'Shrike')), |
| 275 |
('Red Hat Enterprise Linux release 4 (Nahant)', ('Red Hat Enterprise Linux', '4', 'Nahant')), |
| 276 |
('CentOS release 4', ('CentOS', '4', None)), |
| 277 |
('Rocks release 4.2.1 (Cydonia)', ('Rocks', '4.2.1', 'Cydonia')), |
| 278 |
('', ('', '', '')), |
| 279 |
): |
| 280 |
self.assertEqual(platform._parse_release_file(input), output) |
| 281 |
|
| 282 |
|
| 283 |
def test_main(): |
| 284 |
support.run_unittest( |
| 285 |
PlatformTest |
| 286 |
) |
| 287 |
|
| 288 |
if __name__ == '__main__': |
| 289 |
test_main() |