test_exec_command.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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_, assert_warns
  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. with assert_warns(DeprecationWarning):
  59. exec_command.exec_command("cd '.'")
  60. if os.name == 'posix':
  61. # Test general (non-posix) version:
  62. with emulate_nonposix():
  63. with redirect_stdout(StringIO()):
  64. with redirect_stderr(TemporaryFile()):
  65. with assert_warns(DeprecationWarning):
  66. exec_command.exec_command("cd '.'")
  67. def test_exec_command_stderr():
  68. # Test posix version:
  69. with redirect_stdout(TemporaryFile(mode='w+')):
  70. with redirect_stderr(StringIO()):
  71. with assert_warns(DeprecationWarning):
  72. exec_command.exec_command("cd '.'")
  73. if os.name == 'posix':
  74. # Test general (non-posix) version:
  75. with emulate_nonposix():
  76. with redirect_stdout(TemporaryFile()):
  77. with redirect_stderr(StringIO()):
  78. with assert_warns(DeprecationWarning):
  79. exec_command.exec_command("cd '.'")
  80. class TestExecCommand(object):
  81. def setup(self):
  82. self.pyexe = get_pythonexe()
  83. def check_nt(self, **kws):
  84. s, o = exec_command.exec_command('cmd /C echo path=%path%')
  85. assert_(s == 0)
  86. assert_(o != '')
  87. s, o = exec_command.exec_command(
  88. '"%s" -c "import sys;sys.stderr.write(sys.platform)"' % self.pyexe)
  89. assert_(s == 0)
  90. assert_(o == 'win32')
  91. def check_posix(self, **kws):
  92. s, o = exec_command.exec_command("echo Hello", **kws)
  93. assert_(s == 0)
  94. assert_(o == 'Hello')
  95. s, o = exec_command.exec_command('echo $AAA', **kws)
  96. assert_(s == 0)
  97. assert_(o == '')
  98. s, o = exec_command.exec_command('echo "$AAA"', AAA='Tere', **kws)
  99. assert_(s == 0)
  100. assert_(o == 'Tere')
  101. s, o = exec_command.exec_command('echo "$AAA"', **kws)
  102. assert_(s == 0)
  103. assert_(o == '')
  104. if 'BBB' not in os.environ:
  105. os.environ['BBB'] = 'Hi'
  106. s, o = exec_command.exec_command('echo "$BBB"', **kws)
  107. assert_(s == 0)
  108. assert_(o == 'Hi')
  109. s, o = exec_command.exec_command('echo "$BBB"', BBB='Hey', **kws)
  110. assert_(s == 0)
  111. assert_(o == 'Hey')
  112. s, o = exec_command.exec_command('echo "$BBB"', **kws)
  113. assert_(s == 0)
  114. assert_(o == 'Hi')
  115. del os.environ['BBB']
  116. s, o = exec_command.exec_command('echo "$BBB"', **kws)
  117. assert_(s == 0)
  118. assert_(o == '')
  119. s, o = exec_command.exec_command('this_is_not_a_command', **kws)
  120. assert_(s != 0)
  121. assert_(o != '')
  122. s, o = exec_command.exec_command('echo path=$PATH', **kws)
  123. assert_(s == 0)
  124. assert_(o != '')
  125. s, o = exec_command.exec_command(
  126. '"%s" -c "import sys,os;sys.stderr.write(os.name)"' %
  127. self.pyexe, **kws)
  128. assert_(s == 0)
  129. assert_(o == 'posix')
  130. def check_basic(self, *kws):
  131. s, o = exec_command.exec_command(
  132. '"%s" -c "raise \'Ignore me.\'"' % self.pyexe, **kws)
  133. assert_(s != 0)
  134. assert_(o != '')
  135. s, o = exec_command.exec_command(
  136. '"%s" -c "import sys;sys.stderr.write(\'0\');'
  137. 'sys.stderr.write(\'1\');sys.stderr.write(\'2\')"' %
  138. self.pyexe, **kws)
  139. assert_(s == 0)
  140. assert_(o == '012')
  141. s, o = exec_command.exec_command(
  142. '"%s" -c "import sys;sys.exit(15)"' % self.pyexe, **kws)
  143. assert_(s == 15)
  144. assert_(o == '')
  145. s, o = exec_command.exec_command(
  146. '"%s" -c "print(\'Heipa\'")' % self.pyexe, **kws)
  147. assert_(s == 0)
  148. assert_(o == 'Heipa')
  149. def check_execute_in(self, **kws):
  150. with tempdir() as tmpdir:
  151. fn = "file"
  152. tmpfile = os.path.join(tmpdir, fn)
  153. f = open(tmpfile, 'w')
  154. f.write('Hello')
  155. f.close()
  156. s, o = exec_command.exec_command(
  157. '"%s" -c "f = open(\'%s\', \'r\'); f.close()"' %
  158. (self.pyexe, fn), **kws)
  159. assert_(s != 0)
  160. assert_(o != '')
  161. s, o = exec_command.exec_command(
  162. '"%s" -c "f = open(\'%s\', \'r\'); print(f.read()); '
  163. 'f.close()"' % (self.pyexe, fn), execute_in=tmpdir, **kws)
  164. assert_(s == 0)
  165. assert_(o == 'Hello')
  166. def test_basic(self):
  167. with redirect_stdout(StringIO()):
  168. with redirect_stderr(StringIO()):
  169. with assert_warns(DeprecationWarning):
  170. if os.name == "posix":
  171. self.check_posix(use_tee=0)
  172. self.check_posix(use_tee=1)
  173. elif os.name == "nt":
  174. self.check_nt(use_tee=0)
  175. self.check_nt(use_tee=1)
  176. self.check_execute_in(use_tee=0)
  177. self.check_execute_in(use_tee=1)