Home | History | Annotate | Download | only in test
      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             ('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             # branch and revision are not "parsed", but fetched
     88             # from sys.subversion.  Ignore them
     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         # Tests for python_implementation(), python_version(), python_branch(),
     95         # python_revision(), python_build(), and python_compiler().
     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         # Issue 7860: make sure we get architecture from the correct variable
    162         # on 64 bit Windows: if PROCESSOR_ARCHITEW6432 exists we should be
    163         # using it, per
    164         # http://blogs.msdn.com/david.wang/archive/2006/03/26/HOWTO-Detect-Process-Bitness.aspx
    165         try:
    166             with test_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             # We're on a MacOSX system, check that
    200             # the right version information is returned
    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             # On Snow Leopard, sw_vers reports 10.6.0 as 10.6
    213             if len_diff > 0:
    214                 expect_list.extend(['0'] * len_diff)
    215             self.assertEqual(result_list, expect_list)
    216 
    217             # res[1] claims to contain
    218             # (version, dev_stage, non_release_version)
    219             # That information is no longer available
    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         # Issue7895: platform.mac_ver() crashes when using fork without exec
    231         #
    232         # This test checks that the fix for that issue works.
    233         #
    234         pid = os.fork()
    235         if pid == 0:
    236             # child
    237             info = platform.mac_ver()
    238             os._exit(0)
    239 
    240         else:
    241             # parent
    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         import os
    251         if os.path.isdir(sys.executable) and \
    252            os.path.exists(sys.executable+'.exe'):
    253             # Cygwin horror
    254             executable = sys.executable + '.exe'
    255         else:
    256             executable = sys.executable
    257         res = platform.libc_ver(executable)
    258 
    259     def test_parse_release_file(self):
    260 
    261         for input, output in (
    262             # Examples of release file contents:
    263             ('SuSE Linux 9.3 (x86-64)', ('SuSE Linux ', '9.3', 'x86-64')),
    264             ('SUSE LINUX 10.1 (X86-64)', ('SUSE LINUX ', '10.1', 'X86-64')),
    265             ('SUSE LINUX 10.1 (i586)', ('SUSE LINUX ', '10.1', 'i586')),
    266             ('Fedora Core release 5 (Bordeaux)', ('Fedora Core', '5', 'Bordeaux')),
    267             ('Red Hat Linux release 8.0 (Psyche)', ('Red Hat Linux', '8.0', 'Psyche')),
    268             ('Red Hat Linux release 9 (Shrike)', ('Red Hat Linux', '9', 'Shrike')),
    269             ('Red Hat Enterprise Linux release 4 (Nahant)', ('Red Hat Enterprise Linux', '4', 'Nahant')),
    270             ('CentOS release 4', ('CentOS', '4', None)),
    271             ('Rocks release 4.2.1 (Cydonia)', ('Rocks', '4.2.1', 'Cydonia')),
    272             ('', ('', '', '')), # If there's nothing there.
    273             ):
    274             self.assertEqual(platform._parse_release_file(input), output)
    275 
    276 
    277 def test_main():
    278     test_support.run_unittest(
    279         PlatformTest
    280     )
    281 
    282 if __name__ == '__main__':
    283     test_main()
    284