(root)/
Python-3.11.7/
Lib/
distutils/
tests/
test_cygwinccompiler.py
       1  """Tests for distutils.cygwinccompiler."""
       2  import unittest
       3  import sys
       4  import os
       5  from io import BytesIO
       6  
       7  from distutils import cygwinccompiler
       8  from distutils.cygwinccompiler import (check_config_h,
       9                                         CONFIG_H_OK, CONFIG_H_NOTOK,
      10                                         CONFIG_H_UNCERTAIN, get_versions,
      11                                         get_msvcr)
      12  from distutils.tests import support
      13  
      14  class ESC[4;38;5;81mFakePopen(ESC[4;38;5;149mobject):
      15      test_class = None
      16  
      17      def __init__(self, cmd, shell, stdout):
      18          self.cmd = cmd.split()[0]
      19          exes = self.test_class._exes
      20          if self.cmd in exes:
      21              # issue #6438 in Python 3.x, Popen returns bytes
      22              self.stdout = BytesIO(exes[self.cmd])
      23          else:
      24              self.stdout = os.popen(cmd, 'r')
      25  
      26  
      27  class ESC[4;38;5;81mCygwinCCompilerTestCase(ESC[4;38;5;149msupportESC[4;38;5;149m.ESC[4;38;5;149mTempdirManager,
      28                                ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
      29  
      30      def setUp(self):
      31          super(CygwinCCompilerTestCase, self).setUp()
      32          self.version = sys.version
      33          self.python_h = os.path.join(self.mkdtemp(), 'python.h')
      34          from distutils import sysconfig
      35          self.old_get_config_h_filename = sysconfig.get_config_h_filename
      36          sysconfig.get_config_h_filename = self._get_config_h_filename
      37          self.old_find_executable = cygwinccompiler.find_executable
      38          cygwinccompiler.find_executable = self._find_executable
      39          self._exes = {}
      40          self.old_popen = cygwinccompiler.Popen
      41          FakePopen.test_class = self
      42          cygwinccompiler.Popen = FakePopen
      43  
      44      def tearDown(self):
      45          sys.version = self.version
      46          from distutils import sysconfig
      47          sysconfig.get_config_h_filename = self.old_get_config_h_filename
      48          cygwinccompiler.find_executable = self.old_find_executable
      49          cygwinccompiler.Popen = self.old_popen
      50          super(CygwinCCompilerTestCase, self).tearDown()
      51  
      52      def _get_config_h_filename(self):
      53          return self.python_h
      54  
      55      def _find_executable(self, name):
      56          if name in self._exes:
      57              return name
      58          return None
      59  
      60      def test_check_config_h(self):
      61  
      62          # check_config_h looks for "GCC" in sys.version first
      63          # returns CONFIG_H_OK if found
      64          sys.version = ('2.6.1 (r261:67515, Dec  6 2008, 16:42:21) \n[GCC '
      65                         '4.0.1 (Apple Computer, Inc. build 5370)]')
      66  
      67          self.assertEqual(check_config_h()[0], CONFIG_H_OK)
      68  
      69          # then it tries to see if it can find "__GNUC__" in pyconfig.h
      70          sys.version = 'something without the *CC word'
      71  
      72          # if the file doesn't exist it returns  CONFIG_H_UNCERTAIN
      73          self.assertEqual(check_config_h()[0], CONFIG_H_UNCERTAIN)
      74  
      75          # if it exists but does not contain __GNUC__, it returns CONFIG_H_NOTOK
      76          self.write_file(self.python_h, 'xxx')
      77          self.assertEqual(check_config_h()[0], CONFIG_H_NOTOK)
      78  
      79          # and CONFIG_H_OK if __GNUC__ is found
      80          self.write_file(self.python_h, 'xxx __GNUC__ xxx')
      81          self.assertEqual(check_config_h()[0], CONFIG_H_OK)
      82  
      83      def test_get_versions(self):
      84  
      85          # get_versions calls distutils.spawn.find_executable on
      86          # 'gcc', 'ld' and 'dllwrap'
      87          self.assertEqual(get_versions(), (None, None, None))
      88  
      89          # Let's fake we have 'gcc' and it returns '3.4.5'
      90          self._exes['gcc'] = b'gcc (GCC) 3.4.5 (mingw special)\nFSF'
      91          res = get_versions()
      92          self.assertEqual(str(res[0]), '3.4.5')
      93  
      94          # and let's see what happens when the version
      95          # doesn't match the regular expression
      96          # (\d+\.\d+(\.\d+)*)
      97          self._exes['gcc'] = b'very strange output'
      98          res = get_versions()
      99          self.assertEqual(res[0], None)
     100  
     101          # same thing for ld
     102          self._exes['ld'] = b'GNU ld version 2.17.50 20060824'
     103          res = get_versions()
     104          self.assertEqual(str(res[1]), '2.17.50')
     105          self._exes['ld'] = b'@(#)PROGRAM:ld  PROJECT:ld64-77'
     106          res = get_versions()
     107          self.assertEqual(res[1], None)
     108  
     109          # and dllwrap
     110          self._exes['dllwrap'] = b'GNU dllwrap 2.17.50 20060824\nFSF'
     111          res = get_versions()
     112          self.assertEqual(str(res[2]), '2.17.50')
     113          self._exes['dllwrap'] = b'Cheese Wrap'
     114          res = get_versions()
     115          self.assertEqual(res[2], None)
     116  
     117      def test_get_msvcr(self):
     118  
     119          # none
     120          sys.version  = ('2.6.1 (r261:67515, Dec  6 2008, 16:42:21) '
     121                          '\n[GCC 4.0.1 (Apple Computer, Inc. build 5370)]')
     122          self.assertEqual(get_msvcr(), None)
     123  
     124          # MSVC 7.0
     125          sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) '
     126                         '[MSC v.1300 32 bits (Intel)]')
     127          self.assertEqual(get_msvcr(), ['msvcr70'])
     128  
     129          # MSVC 7.1
     130          sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) '
     131                         '[MSC v.1310 32 bits (Intel)]')
     132          self.assertEqual(get_msvcr(), ['msvcr71'])
     133  
     134          # VS2005 / MSVC 8.0
     135          sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) '
     136                         '[MSC v.1400 32 bits (Intel)]')
     137          self.assertEqual(get_msvcr(), ['msvcr80'])
     138  
     139          # VS2008 / MSVC 9.0
     140          sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) '
     141                         '[MSC v.1500 32 bits (Intel)]')
     142          self.assertEqual(get_msvcr(), ['msvcr90'])
     143  
     144          # unknown
     145          sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) '
     146                         '[MSC v.1999 32 bits (Intel)]')
     147          self.assertRaises(ValueError, get_msvcr)
     148  
     149  if __name__ == '__main__':
     150      unittest.main()