test_exec_command.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. from __future__ import division, absolute_import, print_function
  2. import os
  3. import sys
  4. from tempfile import TemporaryFile
  5. from numpy.distutils import exec_command
  6. from numpy.distutils.exec_command import get_pythonexe
  7. from numpy.testing import tempdir, assert_
  8. # In python 3 stdout, stderr are text (unicode compliant) devices, so to
  9. # emulate them import StringIO from the io module.
  10. if sys.version_info[0] >= 3:
  11. from io import StringIO
  12. else:
  13. from StringIO import StringIO
  14. class redirect_stdout(object):
  15. """Context manager to redirect stdout for exec_command test."""
  16. def __init__(self, stdout=None):
  17. self._stdout = stdout or sys.stdout
  18. def __enter__(self):
  19. self.old_stdout = sys.stdout
  20. sys.stdout = self._stdout
  21. def __exit__(self, exc_type, exc_value, traceback):
  22. self._stdout.flush()
  23. sys.stdout = self.old_stdout
  24. # note: closing sys.stdout won't close it.
  25. self._stdout.close()
  26. class redirect_stderr(object):
  27. """Context manager to redirect stderr for exec_command test."""
  28. def __init__(self, stderr=None):
  29. self._stderr = stderr or sys.stderr
  30. def __enter__(self):
  31. self.old_stderr = sys.stderr
  32. sys.stderr = self._stderr
  33. def __exit__(self, exc_type, exc_value, traceback):
  34. self._stderr.flush()
  35. sys.stderr = self.old_stderr
  36. # note: closing sys.stderr won't close it.
  37. self._stderr.close()
  38. class emulate_nonposix(object):
  39. """Context manager to emulate os.name != 'posix' """
  40. def __init__(self, osname='non-posix'):
  41. self._new_name = osname
  42. def __enter__(self):
  43. self._old_name = os.name
  44. os.name = self._new_name
  45. def __exit__(self, exc_type, exc_value, traceback):
  46. os.name = self._old_name
  47. def test_exec_command_stdout():
  48. # Regression test for gh-2999 and gh-2915.
  49. # There are several packages (nose, scipy.weave.inline, Sage inline
  50. # Fortran) that replace stdout, in which case it doesn't have a fileno
  51. # method. This is tested here, with a do-nothing command that fails if the
  52. # presence of fileno() is assumed in exec_command.
  53. # The code has a special case for posix systems, so if we are on posix test
  54. # both that the special case works and that the generic code works.
  55. # Test posix version:
  56. with redirect_stdout(StringIO()):
  57. with redirect_stderr(TemporaryFile()):
  58. exec_command.exec_command("cd '.'")
  59. if os.name == 'posix':
  60. # Test general (non-posix) version:
  61. with emulate_nonposix():
  62. with redirect_stdout(StringIO()):
  63. with redirect_stderr(TemporaryFile()):
  64. exec_command.exec_command("cd '.'")
  65. def test_exec_command_stderr():
  66. # Test posix version:
  67. with redirect_stdout(TemporaryFile(mode='w+')):
  68. with redirect_stderr(StringIO()):
  69. exec_command.exec_command("cd '.'")
  70. if os.name == 'posix':
  71. # Test general (non-posix) version:
  72. with emulate_nonposix():
  73. with redirect_stdout(TemporaryFile()):
  74. with redirect_stderr(StringIO()):
  75. exec_command.exec_command("cd '.'")
  76. class TestExecCommand(object):
  77. def setup(self):
  78. self.pyexe = get_pythonexe()
  79. def check_nt(self, **kws):
  80. s, o = exec_command.exec_command('cmd /C echo path=%path%')
  81. assert_(s == 0)
  82. assert_(o != '')
  83. s, o = exec_command.exec_command(
  84. '"%s" -c "import sys;sys.stderr.write(sys.platform)"' % self.pyexe)
  85. assert_(s == 0)
  86. assert_(o == 'win32')
  87. def check_posix(self, **kws):
  88. s, o = exec_command.exec_command("echo Hello", **kws)
  89. assert_(s == 0)
  90. assert_(o == 'Hello')
  91. s, o = exec_command.exec_command('echo $AAA', **kws)
  92. assert_(s == 0)
  93. assert_(o == '')
  94. s, o = exec_command.exec_command('echo "$AAA"', AAA='Tere', **kws)
  95. assert_(s == 0)
  96. assert_(o == 'Tere')
  97. s, o = exec_command.exec_command('echo "$AAA"', **kws)
  98. assert_(s == 0)
  99. assert_(o == '')
  100. if 'BBB' not in os.environ:
  101. os.environ['BBB'] = 'Hi'
  102. s, o = exec_command.exec_command('echo "$BBB"', **kws)
  103. assert_(s == 0)
  104. assert_(o == 'Hi')
  105. s, o = exec_command.exec_command('echo "$BBB"', BBB='Hey', **kws)
  106. assert_(s == 0)
  107. assert_(o == 'Hey')
  108. s, o = exec_command.exec_command('echo "$BBB"', **kws)
  109. assert_(s == 0)
  110. assert_(o == 'Hi')
  111. del os.environ['BBB']
  112. s, o = exec_command.exec_command('echo "$BBB"', **kws)
  113. assert_(s == 0)
  114. assert_(o == '')
  115. s, o = exec_command.exec_command('this_is_not_a_command', **kws)
  116. assert_(s != 0)
  117. assert_(o != '')
  118. s, o = exec_command.exec_command('echo path=$PATH', **kws)
  119. assert_(s == 0)
  120. assert_(o != '')
  121. s, o = exec_command.exec_command(
  122. '"%s" -c "import sys,os;sys.stderr.write(os.name)"' %
  123. self.pyexe, **kws)
  124. assert_(s == 0)
  125. assert_(o == 'posix')
  126. def check_basic(self, *kws):
  127. s, o = exec_command.exec_command(
  128. '"%s" -c "raise \'Ignore me.\'"' % self.pyexe, **kws)
  129. assert_(s != 0)
  130. assert_(o != '')
  131. s, o = exec_command.exec_command(
  132. '"%s" -c "import sys;sys.stderr.write(\'0\');'
  133. 'sys.stderr.write(\'1\');sys.stderr.write(\'2\')"' %
  134. self.pyexe, **kws)
  135. assert_(s == 0)
  136. assert_(o == '012')
  137. s, o = exec_command.exec_command(
  138. '"%s" -c "import sys;sys.exit(15)"' % self.pyexe, **kws)
  139. assert_(s == 15)
  140. assert_(o == '')
  141. s, o = exec_command.exec_command(
  142. '"%s" -c "print(\'Heipa\'")' % self.pyexe, **kws)
  143. assert_(s == 0)
  144. assert_(o == 'Heipa')
  145. def check_execute_in(self, **kws):
  146. with tempdir() as tmpdir:
  147. fn = "file"
  148. tmpfile = os.path.join(tmpdir, fn)
  149. f = open(tmpfile, 'w')
  150. f.write('Hello')
  151. f.close()
  152. s, o = exec_command.exec_command(
  153. '"%s" -c "f = open(\'%s\', \'r\'); f.close()"' %
  154. (self.pyexe, fn), **kws)
  155. assert_(s != 0)
  156. assert_(o != '')
  157. s, o = exec_command.exec_command(
  158. '"%s" -c "f = open(\'%s\', \'r\'); print(f.read()); '
  159. 'f.close()"' % (self.pyexe, fn), execute_in=tmpdir, **kws)
  160. assert_(s == 0)
  161. assert_(o == 'Hello')
  162. def test_basic(self):
  163. with redirect_stdout(StringIO()):
  164. with redirect_stderr(StringIO()):
  165. if os.name == "posix":
  166. self.check_posix(use_tee=0)
  167. self.check_posix(use_tee=1)
  168. elif os.name == "nt":
  169. self.check_nt(use_tee=0)
  170. self.check_nt(use_tee=1)
  171. self.check_execute_in(use_tee=0)
  172. self.check_execute_in(use_tee=1)