test_exec_command.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import os
  2. import sys
  3. from tempfile import TemporaryFile
  4. from numpy.distutils import exec_command
  5. from numpy.distutils.exec_command import get_pythonexe
  6. from numpy.testing import tempdir, assert_, assert_warns
  7. # In python 3 stdout, stderr are text (unicode compliant) devices, so to
  8. # emulate them import StringIO from the io module.
  9. from io import StringIO
  10. class redirect_stdout:
  11. """Context manager to redirect stdout for exec_command test."""
  12. def __init__(self, stdout=None):
  13. self._stdout = stdout or sys.stdout
  14. def __enter__(self):
  15. self.old_stdout = sys.stdout
  16. sys.stdout = self._stdout
  17. def __exit__(self, exc_type, exc_value, traceback):
  18. self._stdout.flush()
  19. sys.stdout = self.old_stdout
  20. # note: closing sys.stdout won't close it.
  21. self._stdout.close()
  22. class redirect_stderr:
  23. """Context manager to redirect stderr for exec_command test."""
  24. def __init__(self, stderr=None):
  25. self._stderr = stderr or sys.stderr
  26. def __enter__(self):
  27. self.old_stderr = sys.stderr
  28. sys.stderr = self._stderr
  29. def __exit__(self, exc_type, exc_value, traceback):
  30. self._stderr.flush()
  31. sys.stderr = self.old_stderr
  32. # note: closing sys.stderr won't close it.
  33. self._stderr.close()
  34. class emulate_nonposix:
  35. """Context manager to emulate os.name != 'posix' """
  36. def __init__(self, osname='non-posix'):
  37. self._new_name = osname
  38. def __enter__(self):
  39. self._old_name = os.name
  40. os.name = self._new_name
  41. def __exit__(self, exc_type, exc_value, traceback):
  42. os.name = self._old_name
  43. def test_exec_command_stdout():
  44. # Regression test for gh-2999 and gh-2915.
  45. # There are several packages (nose, scipy.weave.inline, Sage inline
  46. # Fortran) that replace stdout, in which case it doesn't have a fileno
  47. # method. This is tested here, with a do-nothing command that fails if the
  48. # presence of fileno() is assumed in exec_command.
  49. # The code has a special case for posix systems, so if we are on posix test
  50. # both that the special case works and that the generic code works.
  51. # Test posix version:
  52. with redirect_stdout(StringIO()):
  53. with redirect_stderr(TemporaryFile()):
  54. with assert_warns(DeprecationWarning):
  55. exec_command.exec_command("cd '.'")
  56. if os.name == 'posix':
  57. # Test general (non-posix) version:
  58. with emulate_nonposix():
  59. with redirect_stdout(StringIO()):
  60. with redirect_stderr(TemporaryFile()):
  61. with assert_warns(DeprecationWarning):
  62. exec_command.exec_command("cd '.'")
  63. def test_exec_command_stderr():
  64. # Test posix version:
  65. with redirect_stdout(TemporaryFile(mode='w+')):
  66. with redirect_stderr(StringIO()):
  67. with assert_warns(DeprecationWarning):
  68. exec_command.exec_command("cd '.'")
  69. if os.name == 'posix':
  70. # Test general (non-posix) version:
  71. with emulate_nonposix():
  72. with redirect_stdout(TemporaryFile()):
  73. with redirect_stderr(StringIO()):
  74. with assert_warns(DeprecationWarning):
  75. exec_command.exec_command("cd '.'")
  76. class TestExecCommand:
  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. with open(tmpfile, 'w') as f:
  150. f.write('Hello')
  151. s, o = exec_command.exec_command(
  152. '"%s" -c "f = open(\'%s\', \'r\'); f.close()"' %
  153. (self.pyexe, fn), **kws)
  154. assert_(s != 0)
  155. assert_(o != '')
  156. s, o = exec_command.exec_command(
  157. '"%s" -c "f = open(\'%s\', \'r\'); print(f.read()); '
  158. 'f.close()"' % (self.pyexe, fn), execute_in=tmpdir, **kws)
  159. assert_(s == 0)
  160. assert_(o == 'Hello')
  161. def test_basic(self):
  162. with redirect_stdout(StringIO()):
  163. with redirect_stderr(StringIO()):
  164. with assert_warns(DeprecationWarning):
  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)