PageRenderTime 84ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/python/Lib/test/test_platform.py

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