Benjamin Peterson | 856ff5f | 2008-05-29 21:22:40 +0000 | [diff] [blame] | 1 | import os |
Walter Dörwald | c69d1c4 | 2005-11-21 17:48:12 +0000 | [diff] [blame] | 2 | import platform |
Hirokazu Yamamoto | e674840 | 2008-10-06 04:51:11 +0000 | [diff] [blame] | 3 | import subprocess |
Victor Stinner | 3a38a6d | 2011-06-10 13:59:59 +0200 | [diff] [blame] | 4 | import sys |
| 5 | import unittest |
| 6 | import warnings |
Walter Dörwald | c69d1c4 | 2005-11-21 17:48:12 +0000 | [diff] [blame] | 7 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 8 | from test import support |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 9 | |
Walter Dörwald | c69d1c4 | 2005-11-21 17:48:12 +0000 | [diff] [blame] | 10 | class PlatformTest(unittest.TestCase): |
| 11 | def test_architecture(self): |
| 12 | res = platform.architecture() |
| 13 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 14 | @support.skip_unless_symlink |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 15 | def test_architecture_via_symlink(self): # issue3762 |
| 16 | # On Windows, the EXE needs to know where pythonXY.dll is at so we have |
| 17 | # to add the directory to the path. |
| 18 | if sys.platform == "win32": |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 19 | os.environ["Path"] = "{};{}".format( |
| 20 | os.path.dirname(sys.executable), os.environ["Path"]) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 21 | |
| 22 | def get(python): |
| 23 | cmd = [python, '-c', |
| 24 | 'import platform; print(platform.architecture())'] |
| 25 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
| 26 | return p.communicate() |
| 27 | real = os.path.realpath(sys.executable) |
| 28 | link = os.path.abspath(support.TESTFN) |
| 29 | os.symlink(real, link) |
| 30 | try: |
| 31 | self.assertEqual(get(real), get(link)) |
| 32 | finally: |
| 33 | os.remove(link) |
Hirokazu Yamamoto | e674840 | 2008-10-06 04:51:11 +0000 | [diff] [blame] | 34 | |
Walter Dörwald | c69d1c4 | 2005-11-21 17:48:12 +0000 | [diff] [blame] | 35 | def test_platform(self): |
| 36 | for aliased in (False, True): |
| 37 | for terse in (False, True): |
| 38 | res = platform.platform(aliased, terse) |
| 39 | |
Benjamin Peterson | 5f28b7b | 2009-03-26 21:49:58 +0000 | [diff] [blame] | 40 | def test_system(self): |
| 41 | res = platform.system() |
| 42 | |
| 43 | def test_node(self): |
| 44 | res = platform.node() |
| 45 | |
| 46 | def test_release(self): |
| 47 | res = platform.release() |
| 48 | |
| 49 | def test_version(self): |
| 50 | res = platform.version() |
| 51 | |
| 52 | def test_machine(self): |
| 53 | res = platform.machine() |
| 54 | |
Walter Dörwald | c69d1c4 | 2005-11-21 17:48:12 +0000 | [diff] [blame] | 55 | def test_processor(self): |
| 56 | res = platform.processor() |
| 57 | |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 58 | def setUp(self): |
| 59 | self.save_version = sys.version |
Georg Brandl | 8256242 | 2011-03-05 21:09:22 +0100 | [diff] [blame] | 60 | self.save_mercurial = sys._mercurial |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 61 | self.save_platform = sys.platform |
Benjamin Peterson | 5f28b7b | 2009-03-26 21:49:58 +0000 | [diff] [blame] | 62 | |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 63 | def tearDown(self): |
| 64 | sys.version = self.save_version |
Georg Brandl | 8256242 | 2011-03-05 21:09:22 +0100 | [diff] [blame] | 65 | sys._mercurial = self.save_mercurial |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 66 | sys.platform = self.save_platform |
Benjamin Peterson | 5f28b7b | 2009-03-26 21:49:58 +0000 | [diff] [blame] | 67 | |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 68 | def test_sys_version(self): |
| 69 | # Old test. |
| 70 | for input, output in ( |
| 71 | ('2.4.3 (#1, Jun 21 2006, 13:54:21) \n[GCC 3.3.4 (pre 3.3.5 20040809)]', |
| 72 | ('CPython', '2.4.3', '', '', '1', 'Jun 21 2006 13:54:21', 'GCC 3.3.4 (pre 3.3.5 20040809)')), |
| 73 | ('IronPython 1.0.60816 on .NET 2.0.50727.42', |
| 74 | ('IronPython', '1.0.60816', '', '', '', '', '.NET 2.0.50727.42')), |
| 75 | ('IronPython 1.0 (1.0.61005.1977) on .NET 2.0.50727.42', |
| 76 | ('IronPython', '1.0.0', '', '', '', '', '.NET 2.0.50727.42')), |
| 77 | ): |
| 78 | # branch and revision are not "parsed", but fetched |
Georg Brandl | 776e586 | 2011-03-06 10:35:42 +0100 | [diff] [blame] | 79 | # from sys._mercurial. Ignore them |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 80 | (name, version, branch, revision, buildno, builddate, compiler) \ |
| 81 | = platform._sys_version(input) |
| 82 | self.assertEqual( |
| 83 | (name, version, '', '', buildno, builddate, compiler), output) |
Benjamin Peterson | 5f28b7b | 2009-03-26 21:49:58 +0000 | [diff] [blame] | 84 | |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 85 | # Tests for python_implementation(), python_version(), python_branch(), |
| 86 | # python_revision(), python_build(), and python_compiler(). |
| 87 | sys_versions = { |
| 88 | ("2.6.1 (r261:67515, Dec 6 2008, 15:26:00) \n[GCC 4.0.1 (Apple Computer, Inc. build 5370)]", |
| 89 | ('CPython', 'tags/r261', '67515'), self.save_platform) |
| 90 | : |
| 91 | ("CPython", "2.6.1", "tags/r261", "67515", |
| 92 | ('r261:67515', 'Dec 6 2008 15:26:00'), |
| 93 | 'GCC 4.0.1 (Apple Computer, Inc. build 5370)'), |
| 94 | ("IronPython 2.0 (2.0.0.0) on .NET 2.0.50727.3053", None, "cli") |
| 95 | : |
| 96 | ("IronPython", "2.0.0", "", "", ("", ""), |
| 97 | ".NET 2.0.50727.3053"), |
| 98 | ("2.5 (trunk:6107, Mar 26 2009, 13:02:18) \n[Java HotSpot(TM) Client VM (\"Apple Computer, Inc.\")]", |
| 99 | ('Jython', 'trunk', '6107'), "java1.5.0_16") |
| 100 | : |
| 101 | ("Jython", "2.5.0", "trunk", "6107", |
| 102 | ('trunk:6107', 'Mar 26 2009'), "java1.5.0_16"), |
| 103 | ("2.5.2 (63378, Mar 26 2009, 18:03:29)\n[PyPy 1.0.0]", |
| 104 | ('PyPy', 'trunk', '63378'), self.save_platform) |
| 105 | : |
| 106 | ("PyPy", "2.5.2", "trunk", "63378", ('63378', 'Mar 26 2009'), |
| 107 | "") |
| 108 | } |
| 109 | for (version_tag, subversion, sys_platform), info in \ |
| 110 | sys_versions.items(): |
| 111 | sys.version = version_tag |
| 112 | if subversion is None: |
Georg Brandl | 8256242 | 2011-03-05 21:09:22 +0100 | [diff] [blame] | 113 | if hasattr(sys, "_mercurial"): |
| 114 | del sys._mercurial |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 115 | else: |
Georg Brandl | 8256242 | 2011-03-05 21:09:22 +0100 | [diff] [blame] | 116 | sys._mercurial = subversion |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 117 | if sys_platform is not None: |
| 118 | sys.platform = sys_platform |
| 119 | self.assertEqual(platform.python_implementation(), info[0]) |
| 120 | self.assertEqual(platform.python_version(), info[1]) |
| 121 | self.assertEqual(platform.python_branch(), info[2]) |
| 122 | self.assertEqual(platform.python_revision(), info[3]) |
| 123 | self.assertEqual(platform.python_build(), info[4]) |
| 124 | self.assertEqual(platform.python_compiler(), info[5]) |
Walter Dörwald | c69d1c4 | 2005-11-21 17:48:12 +0000 | [diff] [blame] | 125 | |
Walter Dörwald | c69d1c4 | 2005-11-21 17:48:12 +0000 | [diff] [blame] | 126 | def test_system_alias(self): |
| 127 | res = platform.system_alias( |
| 128 | platform.system(), |
| 129 | platform.release(), |
| 130 | platform.version(), |
| 131 | ) |
| 132 | |
| 133 | def test_uname(self): |
| 134 | res = platform.uname() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 135 | self.assertTrue(any(res)) |
Walter Dörwald | c69d1c4 | 2005-11-21 17:48:12 +0000 | [diff] [blame] | 136 | |
R. David Murray | ca2edce | 2010-03-22 17:48:48 +0000 | [diff] [blame] | 137 | @unittest.skipUnless(sys.platform.startswith('win'), "windows only test") |
| 138 | def test_uname_win32_ARCHITEW6432(self): |
| 139 | # Issue 7860: make sure we get architecture from the correct variable |
| 140 | # on 64 bit Windows: if PROCESSOR_ARCHITEW6432 exists we should be |
| 141 | # using it, per |
| 142 | # http://blogs.msdn.com/david.wang/archive/2006/03/26/HOWTO-Detect-Process-Bitness.aspx |
| 143 | try: |
R. David Murray | a113554 | 2010-03-24 00:29:21 +0000 | [diff] [blame] | 144 | with support.EnvironmentVarGuard() as environ: |
R. David Murray | ca2edce | 2010-03-22 17:48:48 +0000 | [diff] [blame] | 145 | if 'PROCESSOR_ARCHITEW6432' in environ: |
| 146 | del environ['PROCESSOR_ARCHITEW6432'] |
| 147 | environ['PROCESSOR_ARCHITECTURE'] = 'foo' |
| 148 | platform._uname_cache = None |
| 149 | system, node, release, version, machine, processor = platform.uname() |
| 150 | self.assertEqual(machine, 'foo') |
| 151 | environ['PROCESSOR_ARCHITEW6432'] = 'bar' |
| 152 | platform._uname_cache = None |
| 153 | system, node, release, version, machine, processor = platform.uname() |
| 154 | self.assertEqual(machine, 'bar') |
| 155 | finally: |
| 156 | platform._uname_cache = None |
| 157 | |
Walter Dörwald | c69d1c4 | 2005-11-21 17:48:12 +0000 | [diff] [blame] | 158 | def test_java_ver(self): |
| 159 | res = platform.java_ver() |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 160 | if sys.platform == 'java': |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 161 | self.assertTrue(all(res)) |
Walter Dörwald | c69d1c4 | 2005-11-21 17:48:12 +0000 | [diff] [blame] | 162 | |
| 163 | def test_win32_ver(self): |
| 164 | res = platform.win32_ver() |
| 165 | |
| 166 | def test_mac_ver(self): |
| 167 | res = platform.mac_ver() |
Benjamin Peterson | 856ff5f | 2008-05-29 21:22:40 +0000 | [diff] [blame] | 168 | |
Amaury Forgeot d'Arc | 3b40927 | 2008-06-10 21:44:58 +0000 | [diff] [blame] | 169 | if platform.uname()[0] == 'Darwin': |
Benjamin Peterson | 856ff5f | 2008-05-29 21:22:40 +0000 | [diff] [blame] | 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() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 179 | self.assertFalse(real_ver is None) |
Brett Cannon | 353411d | 2009-09-03 21:29:20 +0000 | [diff] [blame] | 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) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 186 | self.assertEqual(result_list, expect_list) |
Benjamin Peterson | 856ff5f | 2008-05-29 21:22:40 +0000 | [diff] [blame] | 187 | |
| 188 | # res[1] claims to contain |
| 189 | # (version, dev_stage, non_release_version) |
| 190 | # That information is no longer available |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 191 | self.assertEqual(res[1], ('', '', '')) |
Benjamin Peterson | 856ff5f | 2008-05-29 21:22:40 +0000 | [diff] [blame] | 192 | |
| 193 | if sys.byteorder == 'little': |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 194 | self.assertEqual(res[2], 'i386') |
Benjamin Peterson | 856ff5f | 2008-05-29 21:22:40 +0000 | [diff] [blame] | 195 | else: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 196 | self.assertEqual(res[2], 'PowerPC') |
Walter Dörwald | c69d1c4 | 2005-11-21 17:48:12 +0000 | [diff] [blame] | 197 | |
Ronald Oussoren | e186e38 | 2010-07-23 11:54:59 +0000 | [diff] [blame] | 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) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 214 | self.assertEqual(cpid, pid) |
| 215 | self.assertEqual(sts, 0) |
Ronald Oussoren | e186e38 | 2010-07-23 11:54:59 +0000 | [diff] [blame] | 216 | |
Walter Dörwald | c69d1c4 | 2005-11-21 17:48:12 +0000 | [diff] [blame] | 217 | def test_dist(self): |
| 218 | res = platform.dist() |
| 219 | |
| 220 | def test_libc_ver(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 221 | import os |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 222 | if os.path.isdir(sys.executable) and \ |
| 223 | os.path.exists(sys.executable+'.exe'): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 224 | # Cygwin horror |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 225 | executable = sys.executable + '.exe' |
| 226 | else: |
| 227 | executable = sys.executable |
| 228 | res = platform.libc_ver(executable) |
Walter Dörwald | c69d1c4 | 2005-11-21 17:48:12 +0000 | [diff] [blame] | 229 | |
Benjamin Peterson | 1a6e0d0 | 2008-10-25 15:49:17 +0000 | [diff] [blame] | 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')), |
Benjamin Peterson | 2500147 | 2010-01-25 03:37:42 +0000 | [diff] [blame] | 243 | ('', ('', '', '')), # If there's nothing there. |
Benjamin Peterson | 1a6e0d0 | 2008-10-25 15:49:17 +0000 | [diff] [blame] | 244 | ): |
| 245 | self.assertEqual(platform._parse_release_file(input), output) |
| 246 | |
Victor Stinner | 1dfd380 | 2011-03-03 12:54:07 +0000 | [diff] [blame] | 247 | def test_popen(self): |
Victor Stinner | ff45fed | 2011-03-03 14:07:21 +0000 | [diff] [blame] | 248 | mswindows = (sys.platform == "win32") |
| 249 | |
| 250 | if mswindows: |
| 251 | command = '"{}" -c "print(\'Hello\')"'.format(sys.executable) |
| 252 | else: |
| 253 | command = "'{}' -c 'print(\"Hello\")'".format(sys.executable) |
Victor Stinner | 3a38a6d | 2011-06-10 13:59:59 +0200 | [diff] [blame] | 254 | with warnings.catch_warnings(): |
| 255 | warnings.simplefilter("ignore", DeprecationWarning) |
| 256 | with platform.popen(command) as stdout: |
| 257 | hello = stdout.read().strip() |
| 258 | stdout.close() |
| 259 | self.assertEqual(hello, "Hello") |
Victor Stinner | 1dfd380 | 2011-03-03 12:54:07 +0000 | [diff] [blame] | 260 | |
Victor Stinner | 1dfd380 | 2011-03-03 12:54:07 +0000 | [diff] [blame] | 261 | data = 'plop' |
Victor Stinner | ff45fed | 2011-03-03 14:07:21 +0000 | [diff] [blame] | 262 | if mswindows: |
| 263 | command = '"{}" -c "import sys; data=sys.stdin.read(); exit(len(data))"' |
| 264 | else: |
| 265 | command = "'{}' -c 'import sys; data=sys.stdin.read(); exit(len(data))'" |
| 266 | command = command.format(sys.executable) |
Victor Stinner | 3a38a6d | 2011-06-10 13:59:59 +0200 | [diff] [blame] | 267 | with warnings.catch_warnings(): |
| 268 | warnings.simplefilter("ignore", DeprecationWarning) |
| 269 | with platform.popen(command, 'w') as stdin: |
| 270 | stdout = stdin.write(data) |
| 271 | ret = stdin.close() |
| 272 | self.assertIsNotNone(ret) |
| 273 | if os.name == 'nt': |
| 274 | returncode = ret |
| 275 | else: |
| 276 | returncode = ret >> 8 |
| 277 | self.assertEqual(returncode, len(data)) |
Victor Stinner | 1dfd380 | 2011-03-03 12:54:07 +0000 | [diff] [blame] | 278 | |
Benjamin Peterson | 1a6e0d0 | 2008-10-25 15:49:17 +0000 | [diff] [blame] | 279 | |
Walter Dörwald | c69d1c4 | 2005-11-21 17:48:12 +0000 | [diff] [blame] | 280 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 281 | support.run_unittest( |
Walter Dörwald | c69d1c4 | 2005-11-21 17:48:12 +0000 | [diff] [blame] | 282 | PlatformTest |
| 283 | ) |
| 284 | |
| 285 | if __name__ == '__main__': |
| 286 | test_main() |