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 ("IronPython 2.0 (2.0.0.0) on .NET 2.0.50727.3053", None, "cli") 88 : 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") 93 : 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) 98 : 99 ("PyPy", "2.5.2", "trunk", "63378", ('63378', 'Mar 26 2009'), 100 "") 101 } 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]) 118 119 def test_system_alias(self): 120 res = platform.system_alias( 121 platform.system(), 122 platform.release(), 123 platform.version(), 124 ) 125 126 def test_uname(self): 127 res = platform.uname() 128 self.assertTrue(any(res)) 129 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 150 151 def test_java_ver(self): 152 res = platform.java_ver() 153 if sys.platform == 'java': 154 self.assertTrue(all(res)) 155 156 def test_win32_ver(self): 157 res = platform.win32_ver() 158 159 def test_mac_ver(self): 160 res = platform.mac_ver() 161 162 try: 163 import gestalt 164 except ImportError: 165 have_toolbox_glue = False 166 else: 167 have_toolbox_glue = True 168 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.assertEqual(result_list, expect_list) 187 188 # res[1] claims to contain 189 # (version, dev_stage, non_release_version) 190 # That information is no longer available 191 self.assertEqual(res[1], ('', '', '')) 192 193 if sys.byteorder == 'little': 194 self.assertIn(res[2], ('i386', 'x86_64')) 195 else: 196 self.assertEqual(res[2], 'PowerPC') 197 198 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) 214 self.assertEqual(cpid, pid) 215 self.assertEqual(sts, 0) 216 217 def test_dist(self): 218 res = platform.dist() 219 220 def test_libc_ver(self): 221 import os 222 if os.path.isdir(sys.executable) and \ 223 os.path.exists(sys.executable+'.exe'): 224 # Cygwin horror 225 executable = sys.executable + '.exe' 226 else: 227 executable = sys.executable 228 res = platform.libc_ver(executable) 229 230 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')), 243 ('', ('', '', '')), # If there's nothing there. 244 ): 245 self.assertEqual(platform._parse_release_file(input), output) 246 247 248 def test_main(): 249 test_support.run_unittest( 250 PlatformTest 251 ) 252 253 if __name__ == '__main__': 254 test_main() 255