test_subprocess32.py 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485
  1. import unittest
  2. from test import test_support
  3. import subprocess32
  4. subprocess = subprocess32
  5. import sys
  6. try:
  7. import ctypes
  8. except ImportError:
  9. ctypes = None
  10. else:
  11. import ctypes.util
  12. import signal
  13. import os
  14. import errno
  15. import tempfile
  16. import textwrap
  17. import time
  18. try:
  19. import threading
  20. except ImportError:
  21. threading = None
  22. import re
  23. #import sysconfig
  24. import select
  25. import shutil
  26. try:
  27. import gc
  28. except ImportError:
  29. gc = None
  30. import pickle
  31. mswindows = (sys.platform == "win32")
  32. yenv = '''
  33. import os
  34. os.environ['Y_PYTHON_ENTRY_POINT'] = ':main'
  35. '''
  36. #
  37. # Depends on the following external programs: Python
  38. #
  39. if mswindows:
  40. SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
  41. 'os.O_BINARY);')
  42. else:
  43. SETBINARY = ''
  44. try:
  45. mkstemp = tempfile.mkstemp
  46. except AttributeError:
  47. # tempfile.mkstemp is not available
  48. def mkstemp():
  49. """Replacement for mkstemp, calling mktemp."""
  50. fname = tempfile.mktemp()
  51. return os.open(fname, os.O_RDWR|os.O_CREAT), fname
  52. try:
  53. strip_python_stderr = test_support.strip_python_stderr
  54. except AttributeError:
  55. # Copied from the test.test_support module in 2.7.
  56. def strip_python_stderr(stderr):
  57. """Strip the stderr of a Python process from potential debug output
  58. emitted by the interpreter.
  59. This will typically be run on the result of the communicate() method
  60. of a subprocess.Popen object.
  61. """
  62. stderr = re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr).strip()
  63. return stderr
  64. class BaseTestCase(unittest.TestCase):
  65. def setUp(self):
  66. os.environ['Y_PYTHON_ENTRY_POINT'] = ':main'
  67. # Try to minimize the number of children we have so this test
  68. # doesn't crash on some buildbots (Alphas in particular).
  69. reap_children()
  70. if not hasattr(unittest.TestCase, 'addCleanup'):
  71. self._cleanups = []
  72. def tearDown(self):
  73. try:
  74. for inst in subprocess._active:
  75. inst.wait()
  76. subprocess._cleanup()
  77. self.assertFalse(subprocess._active, "subprocess._active not empty")
  78. finally:
  79. if self._use_our_own_cleanup_implementation:
  80. self._doCleanups()
  81. if not hasattr(unittest.TestCase, 'assertIn'):
  82. def assertIn(self, a, b, msg=None):
  83. self.assert_((a in b), msg or ('%r not in %r' % (a, b)))
  84. def assertNotIn(self, a, b, msg=None):
  85. self.assert_((a not in b), msg or ('%r in %r' % (a, b)))
  86. if not hasattr(unittest.TestCase, 'skipTest'):
  87. def skipTest(self, message):
  88. """These will still fail but it'll be clear that it is okay."""
  89. self.fail('SKIPPED - %s\n' % (message,))
  90. def _addCleanup(self, function, *args, **kwargs):
  91. """Add a function, with arguments, to be called when the test is
  92. completed. Functions added are called on a LIFO basis and are
  93. called after tearDown on test failure or success.
  94. Unlike unittest2 or python 2.7, cleanups are not if setUp fails.
  95. That is easier to implement in this subclass and is all we need.
  96. """
  97. self._cleanups.append((function, args, kwargs))
  98. def _doCleanups(self):
  99. """Execute all cleanup functions. Normally called for you after
  100. tearDown."""
  101. while self._cleanups:
  102. function, args, kwargs = self._cleanups.pop()
  103. try:
  104. function(*args, **kwargs)
  105. except KeyboardInterrupt:
  106. raise
  107. except:
  108. pass
  109. _use_our_own_cleanup_implementation = False
  110. if not hasattr(unittest.TestCase, 'addCleanup'):
  111. _use_our_own_cleanup_implementation = True
  112. addCleanup = _addCleanup
  113. def assertStderrEqual(self, stderr, expected, msg=None):
  114. # In a debug build, stuff like "[6580 refs]" is printed to stderr at
  115. # shutdown time. That frustrates tests trying to check stderr produced
  116. # from a spawned Python process.
  117. actual = strip_python_stderr(stderr)
  118. # strip_python_stderr also strips whitespace, so we do too.
  119. expected = expected.strip()
  120. self.assertEqual(actual, expected, msg)
  121. class PopenTestException(Exception):
  122. pass
  123. class PopenExecuteChildRaises(subprocess32.Popen):
  124. """Popen subclass for testing cleanup of subprocess.PIPE filehandles when
  125. _execute_child fails.
  126. """
  127. def _execute_child(self, *args, **kwargs):
  128. raise PopenTestException("Forced Exception for Test")
  129. class ProcessTestCase(BaseTestCase):
  130. def test_call_seq(self):
  131. # call() function with sequence argument
  132. rc = subprocess.call([sys.executable, "-c", yenv +
  133. "import sys; sys.exit(47)"])
  134. self.assertEqual(rc, 47)
  135. def test_call_timeout(self):
  136. # call() function with timeout argument; we want to test that the child
  137. # process gets killed when the timeout expires. If the child isn't
  138. # killed, this call will deadlock since subprocess.call waits for the
  139. # child.
  140. self.assertRaises(subprocess.TimeoutExpired, subprocess.call,
  141. [sys.executable, "-c", yenv + "while True: pass"],
  142. timeout=0.1)
  143. def test_check_call_zero(self):
  144. # check_call() function with zero return code
  145. rc = subprocess.check_call([sys.executable, "-c", yenv +
  146. "import sys; sys.exit(0)"])
  147. self.assertEqual(rc, 0)
  148. def test_check_call_nonzero(self):
  149. # check_call() function with non-zero return code
  150. try:
  151. subprocess.check_call([sys.executable, "-c", yenv +
  152. "import sys; sys.exit(47)"])
  153. except subprocess.CalledProcessError, c:
  154. self.assertEqual(c.returncode, 47)
  155. def test_check_output(self):
  156. # check_output() function with zero return code
  157. output = subprocess.check_output(
  158. [sys.executable, "-c", yenv + "print 'BDFL'"])
  159. self.assertIn('BDFL', output)
  160. def test_check_output_nonzero(self):
  161. # check_call() function with non-zero return code
  162. try:
  163. subprocess.check_output(
  164. [sys.executable, "-c", yenv + "import sys; sys.exit(5)"])
  165. except subprocess.CalledProcessError, c:
  166. self.assertEqual(c.returncode, 5)
  167. def test_check_output_stderr(self):
  168. # check_output() function stderr redirected to stdout
  169. output = subprocess.check_output(
  170. [sys.executable, "-c", yenv + "import sys; sys.stderr.write('BDFL')"],
  171. stderr=subprocess.STDOUT)
  172. self.assertIn('BDFL', output)
  173. def test_check_output_stdout_arg(self):
  174. # check_output() function stderr redirected to stdout
  175. try:
  176. output = subprocess.check_output(
  177. [sys.executable, "-c", yenv + "print 'will not be run'"],
  178. stdout=sys.stdout)
  179. self.fail("Expected ValueError when stdout arg supplied.")
  180. except ValueError, c:
  181. self.assertIn('stdout', c.args[0])
  182. def test_check_output_timeout(self):
  183. # check_output() function with timeout arg
  184. try:
  185. output = subprocess.check_output(
  186. [sys.executable, "-c", yenv +
  187. "import sys; sys.stdout.write('BDFL')\n"
  188. "sys.stdout.flush()\n"
  189. "while True: pass"],
  190. timeout=0.5)
  191. except subprocess.TimeoutExpired, exception:
  192. self.assertEqual(exception.output, 'BDFL')
  193. else:
  194. self.fail("Expected TimeoutExpired.")
  195. def test_call_kwargs(self):
  196. # call() function with keyword args
  197. newenv = os.environ.copy()
  198. newenv["FRUIT"] = "banana"
  199. rc = subprocess.call([sys.executable, "-c", yenv +
  200. 'import sys, os;'
  201. 'sys.exit(os.getenv("FRUIT")=="banana")'],
  202. env=newenv)
  203. self.assertEqual(rc, 1)
  204. def test_stdin_none(self):
  205. # .stdin is None when not redirected
  206. p = subprocess.Popen([sys.executable, "-c", yenv + 'print "banana"'],
  207. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  208. p.wait()
  209. self.assertEqual(p.stdin, None)
  210. def test_stdout_none(self):
  211. # .stdout is None when not redirected, and the child's stdout will
  212. # be inherited from the parent. In order to test this we run a
  213. # subprocess in a subprocess:
  214. # this_test
  215. # \-- subprocess created by this test (parent)
  216. # \-- subprocess created by the parent subprocess (child)
  217. # The parent doesn't specify stdout, so the child will use the
  218. # parent's stdout. This test checks that the message printed by the
  219. # child goes to the parent stdout. The parent also checks that the
  220. # child's stdout is None. See #11963.
  221. code = ('import sys; from subprocess32 import Popen, PIPE;'
  222. 'p = Popen([sys.executable, "-c", "print \'test_stdout_none\'"],'
  223. ' stdin=PIPE, stderr=PIPE);'
  224. 'p.wait(); assert p.stdout is None;')
  225. p = subprocess.Popen([sys.executable, "-c", yenv + code],
  226. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  227. self.addCleanup(p.stdout.close)
  228. self.addCleanup(p.stderr.close)
  229. out, err = p.communicate()
  230. self.assertEqual(p.returncode, 0, err)
  231. self.assertEqual(out.rstrip(), 'test_stdout_none')
  232. def test_stderr_none(self):
  233. # .stderr is None when not redirected
  234. p = subprocess.Popen([sys.executable, "-c", yenv + 'print "banana"'],
  235. stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  236. p.wait()
  237. self.assertEqual(p.stderr, None)
  238. # For use in the test_cwd* tests below.
  239. def _normalize_cwd(self, cwd):
  240. # Normalize an expected cwd (for Tru64 support).
  241. # We can't use os.path.realpath since it doesn't expand Tru64 {memb}
  242. # strings. See bug #1063571.
  243. original_cwd = os.getcwd()
  244. os.chdir(cwd)
  245. cwd = os.getcwd()
  246. os.chdir(original_cwd)
  247. return cwd
  248. # For use in the test_cwd* tests below.
  249. def _split_python_path(self):
  250. # Return normalized (python_dir, python_base).
  251. python_path = os.path.realpath(sys.executable)
  252. return os.path.split(python_path)
  253. # For use in the test_cwd* tests below.
  254. def _assert_cwd(self, expected_cwd, python_arg, **kwargs):
  255. # Invoke Python via Popen, and assert that (1) the call succeeds,
  256. # and that (2) the current working directory of the child process
  257. # matches *expected_cwd*.
  258. p = subprocess.Popen([python_arg, "-c", yenv +
  259. "import os, sys; "
  260. "sys.stdout.write(os.getcwd()); "
  261. "sys.exit(47)"],
  262. stdout=subprocess.PIPE,
  263. **kwargs)
  264. self.addCleanup(p.stdout.close)
  265. p.wait()
  266. self.assertEqual(47, p.returncode)
  267. normcase = os.path.normcase
  268. self.assertEqual(normcase(expected_cwd),
  269. normcase(p.stdout.read().decode("utf-8")))
  270. def test_cwd(self):
  271. # Check that cwd changes the cwd for the child process.
  272. temp_dir = tempfile.gettempdir()
  273. temp_dir = self._normalize_cwd(temp_dir)
  274. self._assert_cwd(temp_dir, sys.executable, cwd=temp_dir)
  275. if not mswindows: # pending resolution of issue #15533
  276. def test_cwd_with_relative_arg(self):
  277. # Check that Popen looks for args[0] relative to cwd if args[0]
  278. # is relative.
  279. python_dir, python_base = self._split_python_path()
  280. rel_python = os.path.join(os.curdir, python_base)
  281. path = 'tempcwd'
  282. saved_dir = os.getcwd()
  283. os.mkdir(path)
  284. try:
  285. os.chdir(path)
  286. wrong_dir = os.getcwd()
  287. # Before calling with the correct cwd, confirm that the call fails
  288. # without cwd and with the wrong cwd.
  289. self.assertRaises(OSError, subprocess.Popen,
  290. [rel_python])
  291. self.assertRaises(OSError, subprocess.Popen,
  292. [rel_python], cwd=wrong_dir)
  293. python_dir = self._normalize_cwd(python_dir)
  294. self._assert_cwd(python_dir, rel_python, cwd=python_dir)
  295. finally:
  296. os.chdir(saved_dir)
  297. shutil.rmtree(path)
  298. def test_cwd_with_relative_executable(self):
  299. # Check that Popen looks for executable relative to cwd if executable
  300. # is relative (and that executable takes precedence over args[0]).
  301. python_dir, python_base = self._split_python_path()
  302. rel_python = os.path.join(os.curdir, python_base)
  303. doesntexist = "somethingyoudonthave"
  304. path = 'tempcwd'
  305. saved_dir = os.getcwd()
  306. os.mkdir(path)
  307. try:
  308. os.chdir(path)
  309. wrong_dir = os.getcwd()
  310. # Before calling with the correct cwd, confirm that the call fails
  311. # without cwd and with the wrong cwd.
  312. self.assertRaises(OSError, subprocess.Popen,
  313. [doesntexist], executable=rel_python)
  314. self.assertRaises(OSError, subprocess.Popen,
  315. [doesntexist], executable=rel_python,
  316. cwd=wrong_dir)
  317. python_dir = self._normalize_cwd(python_dir)
  318. self._assert_cwd(python_dir, doesntexist, executable=rel_python,
  319. cwd=python_dir)
  320. finally:
  321. os.chdir(saved_dir)
  322. shutil.rmtree(path)
  323. def test_cwd_with_absolute_arg(self):
  324. # Check that Popen can find the executable when the cwd is wrong
  325. # if args[0] is an absolute path.
  326. python_dir, python_base = self._split_python_path()
  327. abs_python = os.path.join(python_dir, python_base)
  328. rel_python = os.path.join(os.curdir, python_base)
  329. wrong_dir = tempfile.mkdtemp()
  330. wrong_dir = os.path.realpath(wrong_dir)
  331. try:
  332. # Before calling with an absolute path, confirm that using a
  333. # relative path fails.
  334. self.assertRaises(OSError, subprocess.Popen,
  335. [rel_python], cwd=wrong_dir)
  336. wrong_dir = self._normalize_cwd(wrong_dir)
  337. self._assert_cwd(wrong_dir, abs_python, cwd=wrong_dir)
  338. finally:
  339. shutil.rmtree(wrong_dir)
  340. def test_executable_with_cwd(self):
  341. python_dir, python_base = self._split_python_path()
  342. python_dir = self._normalize_cwd(python_dir)
  343. self._assert_cwd(python_dir, "somethingyoudonthave",
  344. executable=sys.executable, cwd=python_dir)
  345. #@unittest.skipIf(sysconfig.is_python_build(),
  346. # "need an installed Python. See #7774")
  347. #def test_executable_without_cwd(self):
  348. # # For a normal installation, it should work without 'cwd'
  349. # # argument. For test runs in the build directory, see #7774.
  350. # self._assert_cwd('', "somethingyoudonthave", executable=sys.executable)
  351. def test_stdin_pipe(self):
  352. # stdin redirection
  353. p = subprocess.Popen([sys.executable, "-c", yenv +
  354. 'import sys; sys.exit(sys.stdin.read() == "pear")'],
  355. stdin=subprocess.PIPE)
  356. p.stdin.write("pear")
  357. p.stdin.close()
  358. p.wait()
  359. self.assertEqual(p.returncode, 1)
  360. def test_stdin_filedes(self):
  361. # stdin is set to open file descriptor
  362. tf = tempfile.TemporaryFile()
  363. d = tf.fileno()
  364. os.write(d, "pear")
  365. os.lseek(d, 0, 0)
  366. p = subprocess.Popen([sys.executable, "-c", yenv +
  367. 'import sys; sys.exit(sys.stdin.read() == "pear")'],
  368. stdin=d)
  369. p.wait()
  370. self.assertEqual(p.returncode, 1)
  371. def test_stdin_fileobj(self):
  372. # stdin is set to open file object
  373. tf = tempfile.TemporaryFile()
  374. tf.write("pear")
  375. tf.seek(0)
  376. p = subprocess.Popen([sys.executable, "-c", yenv +
  377. 'import sys; sys.exit(sys.stdin.read() == "pear")'],
  378. stdin=tf)
  379. p.wait()
  380. self.assertEqual(p.returncode, 1)
  381. def test_stdout_pipe(self):
  382. # stdout redirection
  383. p = subprocess.Popen([sys.executable, "-c", yenv +
  384. 'import sys; sys.stdout.write("orange")'],
  385. stdout=subprocess.PIPE)
  386. self.assertEqual(p.stdout.read(), "orange")
  387. def test_stdout_filedes(self):
  388. # stdout is set to open file descriptor
  389. tf = tempfile.TemporaryFile()
  390. d = tf.fileno()
  391. p = subprocess.Popen([sys.executable, "-c", yenv +
  392. 'import sys; sys.stdout.write("orange")'],
  393. stdout=d)
  394. p.wait()
  395. os.lseek(d, 0, 0)
  396. self.assertEqual(os.read(d, 1024), "orange")
  397. def test_stdout_fileobj(self):
  398. # stdout is set to open file object
  399. tf = tempfile.TemporaryFile()
  400. p = subprocess.Popen([sys.executable, "-c", yenv +
  401. 'import sys; sys.stdout.write("orange")'],
  402. stdout=tf)
  403. p.wait()
  404. tf.seek(0)
  405. self.assertEqual(tf.read(), "orange")
  406. def test_stderr_pipe(self):
  407. # stderr redirection
  408. p = subprocess.Popen([sys.executable, "-c", yenv +
  409. 'import sys; sys.stderr.write("strawberry")'],
  410. stderr=subprocess.PIPE)
  411. self.assertStderrEqual(p.stderr.read(), "strawberry")
  412. def test_stderr_filedes(self):
  413. # stderr is set to open file descriptor
  414. tf = tempfile.TemporaryFile()
  415. d = tf.fileno()
  416. p = subprocess.Popen([sys.executable, "-c", yenv +
  417. 'import sys; sys.stderr.write("strawberry")'],
  418. stderr=d)
  419. p.wait()
  420. os.lseek(d, 0, 0)
  421. self.assertStderrEqual(os.read(d, 1024), "strawberry")
  422. def test_stderr_fileobj(self):
  423. # stderr is set to open file object
  424. tf = tempfile.TemporaryFile()
  425. p = subprocess.Popen([sys.executable, "-c", yenv +
  426. 'import sys; sys.stderr.write("strawberry")'],
  427. stderr=tf)
  428. p.wait()
  429. tf.seek(0)
  430. self.assertStderrEqual(tf.read(), "strawberry")
  431. def test_stderr_redirect_with_no_stdout_redirect(self):
  432. # test stderr=STDOUT while stdout=None (not set)
  433. # - grandchild prints to stderr
  434. # - child redirects grandchild's stderr to its stdout
  435. # - the parent should get grandchild's stderr in child's stdout
  436. p = subprocess.Popen([sys.executable, "-c", yenv +
  437. 'import sys, subprocess32 as subprocess;'
  438. 'rc = subprocess.call([sys.executable, "-c",'
  439. ' "import sys;"'
  440. ' "sys.stderr.write(\'42\')"],'
  441. ' stderr=subprocess.STDOUT);'
  442. 'sys.exit(rc)'],
  443. stdout=subprocess.PIPE,
  444. stderr=subprocess.PIPE)
  445. stdout, stderr = p.communicate()
  446. #NOTE: stdout should get stderr from grandchild
  447. self.assertStderrEqual(stdout, '42')
  448. self.assertStderrEqual(stderr, '') # should be empty
  449. self.assertEqual(p.returncode, 0)
  450. def test_stdout_stderr_pipe(self):
  451. # capture stdout and stderr to the same pipe
  452. p = subprocess.Popen([sys.executable, "-c", yenv +
  453. 'import sys;'
  454. 'sys.stdout.write("apple");'
  455. 'sys.stdout.flush();'
  456. 'sys.stderr.write("orange")'],
  457. stdout=subprocess.PIPE,
  458. stderr=subprocess.STDOUT)
  459. self.assertStderrEqual(p.stdout.read(), "appleorange")
  460. def test_stdout_stderr_file(self):
  461. # capture stdout and stderr to the same open file
  462. tf = tempfile.TemporaryFile()
  463. p = subprocess.Popen([sys.executable, "-c", yenv +
  464. 'import sys;'
  465. 'sys.stdout.write("apple");'
  466. 'sys.stdout.flush();'
  467. 'sys.stderr.write("orange")'],
  468. stdout=tf,
  469. stderr=tf)
  470. p.wait()
  471. tf.seek(0)
  472. self.assertStderrEqual(tf.read(), "appleorange")
  473. def test_stdout_filedes_of_stdout(self):
  474. # stdout is set to 1 (#1531862).
  475. # To avoid printing the text on stdout, we do something similar to
  476. # test_stdout_none (see above). The parent subprocess calls the child
  477. # subprocess passing stdout=1, and this test uses stdout=PIPE in
  478. # order to capture and check the output of the parent. See #11963.
  479. code = ('import sys, subprocess32; '
  480. 'rc = subprocess32.call([sys.executable, "-c", '
  481. ' "import os, sys; sys.exit(os.write(sys.stdout.fileno(), '
  482. '\'test with stdout=1\'))"], stdout=1); '
  483. 'assert rc == 18')
  484. p = subprocess.Popen([sys.executable, "-c", yenv + code],
  485. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  486. self.addCleanup(p.stdout.close)
  487. self.addCleanup(p.stderr.close)
  488. out, err = p.communicate()
  489. self.assertEqual(p.returncode, 0, err)
  490. self.assertEqual(out.rstrip(), 'test with stdout=1')
  491. def test_stdout_devnull(self):
  492. p = subprocess.Popen([sys.executable, "-c", yenv +
  493. 'for i in range(10240):'
  494. 'print("x" * 1024)'],
  495. stdout=subprocess.DEVNULL)
  496. p.wait()
  497. self.assertEqual(p.stdout, None)
  498. def test_stderr_devnull(self):
  499. p = subprocess.Popen([sys.executable, "-c", yenv +
  500. 'import sys\n'
  501. 'for i in range(10240):'
  502. 'sys.stderr.write("x" * 1024)'],
  503. stderr=subprocess.DEVNULL)
  504. p.wait()
  505. self.assertEqual(p.stderr, None)
  506. def test_stdin_devnull(self):
  507. p = subprocess.Popen([sys.executable, "-c", yenv +
  508. 'import sys;'
  509. 'sys.stdin.read(1)'],
  510. stdin=subprocess.DEVNULL)
  511. p.wait()
  512. self.assertEqual(p.stdin, None)
  513. def test_env(self):
  514. newenv = os.environ.copy()
  515. newenv["FRUIT"] = "orange"
  516. p = subprocess.Popen([sys.executable, "-c", yenv +
  517. 'import sys,os;'
  518. 'sys.stdout.write(os.getenv("FRUIT"))'],
  519. stdout=subprocess.PIPE,
  520. env=newenv)
  521. try:
  522. stdout, stderr = p.communicate()
  523. self.assertEqual(stdout, "orange")
  524. finally:
  525. p.__exit__(None, None, None)
  526. def test_empty_env(self):
  527. """test_empty_env() - verify that env={} is as empty as possible."""
  528. def is_env_var_to_ignore(n):
  529. """Determine if an environment variable is under our control."""
  530. # This excludes some __CF_* and VERSIONER_* keys MacOS insists
  531. # on adding even when the environment in exec is empty.
  532. # Gentoo sandboxes also force LD_PRELOAD and SANDBOX_* to exist.
  533. return ('VERSIONER' in n or '__CF' in n or # MacOS
  534. n == 'LD_PRELOAD' or n.startswith('SANDBOX')) # Gentoo
  535. p = subprocess.Popen(
  536. [sys.executable, '-c',
  537. 'import os; print(list(os.environ.keys()))'],
  538. stdout=subprocess.PIPE, env={'Y_PYTHON_ENTRY_POINT': ':main'})
  539. try:
  540. stdout, stderr = p.communicate()
  541. child_env_names = eval(stdout.strip())
  542. self.assertTrue(isinstance(child_env_names, list),
  543. msg=repr(child_env_names))
  544. child_env_names = [k for k in child_env_names
  545. if not is_env_var_to_ignore(k)]
  546. self.assertEqual(child_env_names, [])
  547. finally:
  548. p.__exit__(None, None, None)
  549. def test_communicate_stdin(self):
  550. p = subprocess.Popen([sys.executable, "-c", yenv +
  551. 'import sys;'
  552. 'sys.exit(sys.stdin.read() == "pear")'],
  553. stdin=subprocess.PIPE)
  554. p.communicate("pear")
  555. self.assertEqual(p.returncode, 1)
  556. def test_communicate_stdout(self):
  557. p = subprocess.Popen([sys.executable, "-c", yenv +
  558. 'import sys; sys.stdout.write("pineapple")'],
  559. stdout=subprocess.PIPE)
  560. (stdout, stderr) = p.communicate()
  561. self.assertEqual(stdout, "pineapple")
  562. self.assertEqual(stderr, None)
  563. def test_communicate_stderr(self):
  564. p = subprocess.Popen([sys.executable, "-c", yenv +
  565. 'import sys; sys.stderr.write("pineapple")'],
  566. stderr=subprocess.PIPE)
  567. (stdout, stderr) = p.communicate()
  568. self.assertEqual(stdout, None)
  569. self.assertStderrEqual(stderr, "pineapple")
  570. def test_communicate(self):
  571. p = subprocess.Popen([sys.executable, "-c", yenv +
  572. 'import sys,os;'
  573. 'sys.stderr.write("pineapple");'
  574. 'sys.stdout.write(sys.stdin.read())'],
  575. stdin=subprocess.PIPE,
  576. stdout=subprocess.PIPE,
  577. stderr=subprocess.PIPE)
  578. (stdout, stderr) = p.communicate("banana")
  579. self.assertEqual(stdout, "banana")
  580. self.assertStderrEqual(stderr, "pineapple")
  581. def test_communicate_timeout(self):
  582. p = subprocess.Popen([sys.executable, "-c", yenv +
  583. 'import sys,os,time;'
  584. 'sys.stderr.write("pineapple\\n");'
  585. 'time.sleep(1);'
  586. 'sys.stderr.write("pear\\n");'
  587. 'sys.stdout.write(sys.stdin.read())'],
  588. universal_newlines=True,
  589. stdin=subprocess.PIPE,
  590. stdout=subprocess.PIPE,
  591. stderr=subprocess.PIPE)
  592. self.assertRaises(subprocess.TimeoutExpired, p.communicate, u"banana",
  593. timeout=0.3)
  594. # Make sure we can keep waiting for it, and that we get the whole output
  595. # after it completes.
  596. (stdout, stderr) = p.communicate()
  597. self.assertEqual(stdout, "banana")
  598. self.assertStderrEqual(stderr.encode(), "pineapple\npear\n")
  599. def test_communicate_timeout_large_ouput(self):
  600. # Test a expring timeout while the child is outputting lots of data.
  601. p = subprocess.Popen([sys.executable, "-c", yenv +
  602. 'import sys,os,time;'
  603. 'sys.stdout.write("a" * (64 * 1024));'
  604. 'time.sleep(0.2);'
  605. 'sys.stdout.write("a" * (64 * 1024));'
  606. 'time.sleep(0.2);'
  607. 'sys.stdout.write("a" * (64 * 1024));'
  608. 'time.sleep(0.2);'
  609. 'sys.stdout.write("a" * (64 * 1024));'],
  610. stdout=subprocess.PIPE)
  611. self.assertRaises(subprocess.TimeoutExpired, p.communicate, timeout=0.4)
  612. (stdout, _) = p.communicate()
  613. self.assertEqual(len(stdout), 4 * 64 * 1024)
  614. # Test for the fd leak reported in http://bugs.python.org/issue2791.
  615. def test_communicate_pipe_fd_leak(self):
  616. for stdin_pipe in (False, True):
  617. for stdout_pipe in (False, True):
  618. for stderr_pipe in (False, True):
  619. options = {}
  620. if stdin_pipe:
  621. options['stdin'] = subprocess.PIPE
  622. if stdout_pipe:
  623. options['stdout'] = subprocess.PIPE
  624. if stderr_pipe:
  625. options['stderr'] = subprocess.PIPE
  626. if not options:
  627. continue
  628. p = subprocess.Popen((sys.executable, "-c", yenv + "pass"), **options)
  629. p.communicate()
  630. if p.stdin is not None:
  631. self.assertTrue(p.stdin.closed)
  632. if p.stdout is not None:
  633. self.assertTrue(p.stdout.closed)
  634. if p.stderr is not None:
  635. self.assertTrue(p.stderr.closed)
  636. def test_communicate_returns(self):
  637. # communicate() should return None if no redirection is active
  638. p = subprocess.Popen([sys.executable, "-c", yenv +
  639. "import sys; sys.exit(47)"])
  640. (stdout, stderr) = p.communicate()
  641. self.assertEqual(stdout, None)
  642. self.assertEqual(stderr, None)
  643. def test_communicate_pipe_buf(self):
  644. # communicate() with writes larger than pipe_buf
  645. # This test will probably deadlock rather than fail, if
  646. # communicate() does not work properly.
  647. x, y = os.pipe()
  648. if mswindows:
  649. pipe_buf = 512
  650. else:
  651. pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
  652. os.close(x)
  653. os.close(y)
  654. p = subprocess.Popen([sys.executable, "-c", yenv +
  655. 'import sys,os;'
  656. 'sys.stdout.write(sys.stdin.read(47));'
  657. 'sys.stderr.write("xyz"*%d);'
  658. 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
  659. stdin=subprocess.PIPE,
  660. stdout=subprocess.PIPE,
  661. stderr=subprocess.PIPE)
  662. string_to_write = "abc"*pipe_buf
  663. (stdout, stderr) = p.communicate(string_to_write)
  664. self.assertEqual(stdout, string_to_write)
  665. def test_writes_before_communicate(self):
  666. # stdin.write before communicate()
  667. p = subprocess.Popen([sys.executable, "-c", yenv +
  668. 'import sys,os;'
  669. 'sys.stdout.write(sys.stdin.read())'],
  670. stdin=subprocess.PIPE,
  671. stdout=subprocess.PIPE,
  672. stderr=subprocess.PIPE)
  673. p.stdin.write("banana")
  674. (stdout, stderr) = p.communicate("split")
  675. self.assertEqual(stdout, "bananasplit")
  676. self.assertStderrEqual(stderr, "")
  677. def test_universal_newlines(self):
  678. p = subprocess.Popen([sys.executable, "-c", yenv +
  679. 'import sys,os;' + SETBINARY +
  680. 'sys.stdout.write("line1\\n");'
  681. 'sys.stdout.flush();'
  682. 'sys.stdout.write("line2\\r");'
  683. 'sys.stdout.flush();'
  684. 'sys.stdout.write("line3\\r\\n");'
  685. 'sys.stdout.flush();'
  686. 'sys.stdout.write("line4\\r");'
  687. 'sys.stdout.flush();'
  688. 'sys.stdout.write("\\nline5");'
  689. 'sys.stdout.flush();'
  690. 'sys.stdout.write("\\nline6");'],
  691. stdout=subprocess.PIPE,
  692. universal_newlines=1)
  693. stdout = p.stdout.read()
  694. if hasattr(file, 'newlines'):
  695. # Interpreter with universal newline support
  696. self.assertEqual(stdout,
  697. "line1\nline2\nline3\nline4\nline5\nline6")
  698. else:
  699. # Interpreter without universal newline support
  700. self.assertEqual(stdout,
  701. "line1\nline2\rline3\r\nline4\r\nline5\nline6")
  702. def test_universal_newlines_communicate(self):
  703. # universal newlines through communicate()
  704. p = subprocess.Popen([sys.executable, "-c", yenv +
  705. 'import sys,os;' + SETBINARY +
  706. 'sys.stdout.write("line1\\n");'
  707. 'sys.stdout.flush();'
  708. 'sys.stdout.write("line2\\r");'
  709. 'sys.stdout.flush();'
  710. 'sys.stdout.write("line3\\r\\n");'
  711. 'sys.stdout.flush();'
  712. 'sys.stdout.write("line4\\r");'
  713. 'sys.stdout.flush();'
  714. 'sys.stdout.write("\\nline5");'
  715. 'sys.stdout.flush();'
  716. 'sys.stdout.write("\\nline6");'],
  717. stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  718. universal_newlines=1)
  719. (stdout, stderr) = p.communicate()
  720. if hasattr(file, 'newlines'):
  721. # Interpreter with universal newline support
  722. self.assertEqual(stdout,
  723. "line1\nline2\nline3\nline4\nline5\nline6")
  724. else:
  725. # Interpreter without universal newline support
  726. self.assertEqual(stdout,
  727. "line1\nline2\rline3\r\nline4\r\nline5\nline6")
  728. def test_universal_newlines_communicate_input_none(self):
  729. # Test communicate(input=None) with universal newlines.
  730. #
  731. # We set stdout to PIPE because, as of this writing, a different
  732. # code path is tested when the number of pipes is zero or one.
  733. p = subprocess.Popen([sys.executable, "-c", yenv + "pass"],
  734. stdin=subprocess.PIPE,
  735. stdout=subprocess.PIPE,
  736. universal_newlines=True)
  737. p.communicate()
  738. self.assertEqual(p.returncode, 0)
  739. def test_no_leaking(self):
  740. # Make sure we leak no resources
  741. if not hasattr(test_support, "is_resource_enabled") \
  742. or test_support.is_resource_enabled("subprocess") and not mswindows:
  743. max_handles = 1026 # too much for most UNIX systems
  744. else:
  745. max_handles = 65
  746. for i in range(max_handles):
  747. p = subprocess.Popen([sys.executable, "-c", yenv +
  748. "import sys;sys.stdout.write(sys.stdin.read())"],
  749. stdin=subprocess.PIPE,
  750. stdout=subprocess.PIPE,
  751. stderr=subprocess.PIPE)
  752. data = p.communicate("lime")[0]
  753. self.assertEqual(data, "lime")
  754. def test_universal_newlines_communicate_stdin_stdout_stderr(self):
  755. # universal newlines through communicate(), with stdin, stdout, stderr
  756. p = subprocess.Popen([sys.executable, "-c", yenv +
  757. 'import sys,os;' + SETBINARY + '''\nif True:
  758. s = sys.stdin.readline()
  759. sys.stdout.write(s)
  760. sys.stdout.write("line2\\r")
  761. sys.stderr.write("eline2\\n")
  762. s = sys.stdin.read()
  763. sys.stdout.write(s)
  764. sys.stdout.write("line4\\n")
  765. sys.stdout.write("line5\\r\\n")
  766. sys.stderr.write("eline6\\r")
  767. sys.stderr.write("eline7\\r\\nz")
  768. '''],
  769. stdin=subprocess.PIPE,
  770. stderr=subprocess.PIPE,
  771. stdout=subprocess.PIPE,
  772. universal_newlines=True)
  773. self.addCleanup(p.stdout.close)
  774. self.addCleanup(p.stderr.close)
  775. (stdout, stderr) = p.communicate(u"line1\nline3\n")
  776. self.assertEqual(p.returncode, 0)
  777. self.assertEqual(u"line1\nline2\nline3\nline4\nline5\n", stdout)
  778. # Python debug build push something like "[42442 refs]\n"
  779. # to stderr at exit of subprocess.
  780. # Don't use assertStderrEqual because it strips CR and LF from output.
  781. self.assertTrue(stderr.startswith(u"eline2\neline6\neline7\n"))
  782. def test_list2cmdline(self):
  783. self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
  784. '"a b c" d e')
  785. self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
  786. 'ab\\"c \\ d')
  787. self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
  788. 'ab\\"c " \\\\" d')
  789. self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
  790. 'a\\\\\\b "de fg" h')
  791. self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
  792. 'a\\\\\\"b c d')
  793. self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
  794. '"a\\\\b c" d e')
  795. self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
  796. '"a\\\\b\\ c" d e')
  797. self.assertEqual(subprocess.list2cmdline(['ab', '']),
  798. 'ab ""')
  799. def test_poll(self):
  800. p = subprocess.Popen([sys.executable,
  801. "-c", yenv + "import time; time.sleep(1)"])
  802. count = 0
  803. while p.poll() is None:
  804. time.sleep(0.1)
  805. count += 1
  806. # We expect that the poll loop probably went around about 10 times,
  807. # but, based on system scheduling we can't control, it's possible
  808. # poll() never returned None. It "should be" very rare that it
  809. # didn't go around at least twice.
  810. self.assert_(count >= 2)
  811. # Subsequent invocations should just return the returncode
  812. self.assertEqual(p.poll(), 0)
  813. def test_wait(self):
  814. p = subprocess.Popen([sys.executable,
  815. "-c", yenv + "import time; time.sleep(2)"])
  816. self.assertEqual(p.wait(), 0)
  817. # Subsequent invocations should just return the returncode
  818. self.assertEqual(p.wait(), 0)
  819. def test_wait_timeout(self):
  820. p = subprocess.Popen([sys.executable,
  821. "-c", yenv + "import time; time.sleep(0.1)"])
  822. try:
  823. p.wait(timeout=0.01)
  824. except subprocess.TimeoutExpired, e:
  825. self.assertIn("0.01", str(e)) # For coverage of __str__.
  826. else:
  827. self.fail("subprocess.TimeoutExpired expected but not raised.")
  828. self.assertEqual(p.wait(timeout=2), 0)
  829. def test_invalid_bufsize(self):
  830. # an invalid type of the bufsize argument should raise
  831. # TypeError.
  832. try:
  833. subprocess.Popen([sys.executable, "-c", yenv + "pass"], "orange")
  834. except TypeError:
  835. pass
  836. def test_leaking_fds_on_error(self):
  837. # see bug #5179: Popen leaks file descriptors to PIPEs if
  838. # the child fails to execute; this will eventually exhaust
  839. # the maximum number of open fds. 1024 seems a very common
  840. # value for that limit, but Windows has 2048, so we loop
  841. # 1024 times (each call leaked two fds).
  842. for i in range(1024):
  843. # Windows raises IOError. Others raise OSError.
  844. try:
  845. subprocess.Popen(['nonexisting_i_hope'],
  846. stdout=subprocess.PIPE,
  847. stderr=subprocess.PIPE)
  848. except EnvironmentError, c:
  849. if c.errno != 2: # ignore "no such file"
  850. raise
  851. #@unittest.skipIf(threading is None, "threading required")
  852. def test_threadsafe_wait(self):
  853. """Issue21291: Popen.wait() needs to be threadsafe for returncode."""
  854. proc = subprocess.Popen([sys.executable, '-c', yenv +
  855. 'import time; time.sleep(12)'])
  856. self.assertEqual(proc.returncode, None)
  857. results = []
  858. def kill_proc_timer_thread():
  859. results.append(('thread-start-poll-result', proc.poll()))
  860. # terminate it from the thread and wait for the result.
  861. proc.kill()
  862. proc.wait()
  863. results.append(('thread-after-kill-and-wait', proc.returncode))
  864. # this wait should be a no-op given the above.
  865. proc.wait()
  866. results.append(('thread-after-second-wait', proc.returncode))
  867. # This is a timing sensitive test, the failure mode is
  868. # triggered when both the main thread and this thread are in
  869. # the wait() call at once. The delay here is to allow the
  870. # main thread to most likely be blocked in its wait() call.
  871. t = threading.Timer(0.2, kill_proc_timer_thread)
  872. t.start()
  873. if mswindows:
  874. expected_errorcode = 1
  875. else:
  876. # Should be -9 because of the proc.kill() from the thread.
  877. expected_errorcode = -9
  878. # Wait for the process to finish; the thread should kill it
  879. # long before it finishes on its own. Supplying a timeout
  880. # triggers a different code path for better coverage.
  881. proc.wait(timeout=20)
  882. self.assertEqual(proc.returncode, expected_errorcode,
  883. msg="unexpected result in wait from main thread")
  884. # This should be a no-op with no change in returncode.
  885. proc.wait()
  886. self.assertEqual(proc.returncode, expected_errorcode,
  887. msg="unexpected result in second main wait.")
  888. t.join()
  889. # Ensure that all of the thread results are as expected.
  890. # When a race condition occurs in wait(), the returncode could
  891. # be set by the wrong thread that doesn't actually have it
  892. # leading to an incorrect value.
  893. self.assertEqual([('thread-start-poll-result', None),
  894. ('thread-after-kill-and-wait', expected_errorcode),
  895. ('thread-after-second-wait', expected_errorcode)],
  896. results)
  897. def test_issue8780(self):
  898. # Ensure that stdout is inherited from the parent
  899. # if stdout=PIPE is not used
  900. code = ';'.join((
  901. 'import subprocess32, sys',
  902. 'retcode = subprocess32.call('
  903. "[sys.executable, '-c', 'print(\"Hello World!\")'])",
  904. 'assert retcode == 0'))
  905. output = subprocess.check_output([sys.executable, '-c', yenv + code])
  906. self.assert_(output.startswith('Hello World!'), output)
  907. def test_communicate_epipe(self):
  908. # Issue 10963: communicate() should hide EPIPE
  909. p = subprocess.Popen([sys.executable, "-c", yenv + 'pass'],
  910. stdin=subprocess.PIPE,
  911. stdout=subprocess.PIPE,
  912. stderr=subprocess.PIPE)
  913. self.addCleanup(p.stdout.close)
  914. self.addCleanup(p.stderr.close)
  915. self.addCleanup(p.stdin.close)
  916. p.communicate(b"x" * 2**20)
  917. def test_communicate_epipe_only_stdin(self):
  918. # Issue 10963: communicate() should hide EPIPE
  919. p = subprocess.Popen([sys.executable, "-c", yenv + 'pass'],
  920. stdin=subprocess.PIPE)
  921. self.addCleanup(p.stdin.close)
  922. p.wait()
  923. p.communicate(b"x" * 2**20)
  924. if not mswindows: # Signal tests are POSIX specific.
  925. def test_communicate_eintr(self):
  926. # Issue #12493: communicate() should handle EINTR
  927. def handler(signum, frame):
  928. pass
  929. old_handler = signal.signal(signal.SIGALRM, handler)
  930. self.addCleanup(signal.signal, signal.SIGALRM, old_handler)
  931. # the process is running for 2 seconds
  932. args = [sys.executable, "-c", yenv + 'import time; time.sleep(2)']
  933. for stream in ('stdout', 'stderr'):
  934. kw = {stream: subprocess.PIPE}
  935. process = subprocess.Popen(args, **kw)
  936. try:
  937. signal.alarm(1)
  938. # communicate() will be interrupted by SIGALRM
  939. process.communicate()
  940. finally:
  941. process.__exit__(None, None, None)
  942. # This test is Linux-ish specific for simplicity to at least have
  943. # some coverage. It is not a platform specific bug.
  944. #@unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
  945. # "Linux specific")
  946. def test_failed_child_execute_fd_leak(self):
  947. """Test for the fork() failure fd leak reported in issue16327."""
  948. if not os.path.isdir('/proc/%d/fd' % os.getpid()):
  949. self.skipTest("Linux specific")
  950. fd_directory = '/proc/%d/fd' % os.getpid()
  951. fds_before_popen = os.listdir(fd_directory)
  952. try:
  953. PopenExecuteChildRaises(
  954. [sys.executable, '-c', yenv + 'pass'], stdin=subprocess.PIPE,
  955. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  956. except PopenTestException:
  957. pass # Yay! Because 2.4 doesn't support with statements.
  958. else:
  959. self.fail("PopenTestException expected but not raised.")
  960. # NOTE: This test doesn't verify that the real _execute_child
  961. # does not close the file descriptors itself on the way out
  962. # during an exception. Code inspection has confirmed that.
  963. fds_after_exception = os.listdir(fd_directory)
  964. self.assertEqual(fds_before_popen, fds_after_exception)
  965. class RunFuncTestCase(BaseTestCase):
  966. def run_python(self, code, **kwargs):
  967. """Run Python code in a subprocess using subprocess.run"""
  968. argv = [sys.executable, "-c", yenv + code]
  969. return subprocess.run(argv, **kwargs)
  970. def test_returncode(self):
  971. # call() function with sequence argument
  972. cp = self.run_python("import sys; sys.exit(47)")
  973. self.assertEqual(cp.returncode, 47)
  974. try:
  975. cp.check_returncode()
  976. except subprocess.CalledProcessError:
  977. pass
  978. else:
  979. self.fail("CalledProcessError not raised")
  980. def test_check(self):
  981. try:
  982. self.run_python("import sys; sys.exit(47)", check=True)
  983. except subprocess.CalledProcessError, exception:
  984. self.assertEqual(exception.returncode, 47)
  985. else:
  986. self.fail("CalledProcessError not raised")
  987. def test_check_zero(self):
  988. # check_returncode shouldn't raise when returncode is zero
  989. cp = self.run_python("import sys; sys.exit(0)", check=True)
  990. self.assertEqual(cp.returncode, 0)
  991. def test_timeout(self):
  992. # run() function with timeout argument; we want to test that the child
  993. # process gets killed when the timeout expires. If the child isn't
  994. # killed, this call will deadlock since subprocess.run waits for the
  995. # child.
  996. try:
  997. self.run_python("while True: pass", timeout=0.0001)
  998. except subprocess.TimeoutExpired:
  999. pass
  1000. else:
  1001. self.fail("TimeoutExpired not raised")
  1002. def test_capture_stdout(self):
  1003. # capture stdout with zero return code
  1004. cp = self.run_python("print('BDFL')", stdout=subprocess.PIPE)
  1005. self.assertIn('BDFL', cp.stdout)
  1006. def test_capture_stderr(self):
  1007. cp = self.run_python("import sys; sys.stderr.write('BDFL')",
  1008. stderr=subprocess.PIPE)
  1009. self.assertIn('BDFL', cp.stderr)
  1010. def test_check_output_stdin_arg(self):
  1011. # run() can be called with stdin set to a file
  1012. tf = tempfile.TemporaryFile()
  1013. self.addCleanup(tf.close)
  1014. tf.write('pear')
  1015. tf.seek(0)
  1016. cp = self.run_python(
  1017. "import sys; sys.stdout.write(sys.stdin.read().upper())",
  1018. stdin=tf, stdout=subprocess.PIPE)
  1019. self.assertIn('PEAR', cp.stdout)
  1020. def test_check_output_input_arg(self):
  1021. # check_output() can be called with input set to a string
  1022. cp = self.run_python(
  1023. "import sys; sys.stdout.write(sys.stdin.read().upper())",
  1024. input='pear', stdout=subprocess.PIPE)
  1025. self.assertIn('PEAR', cp.stdout)
  1026. def test_check_output_stdin_with_input_arg(self):
  1027. # run() refuses to accept 'stdin' with 'input'
  1028. tf = tempfile.TemporaryFile()
  1029. self.addCleanup(tf.close)
  1030. tf.write('pear')
  1031. tf.seek(0)
  1032. try:
  1033. output = self.run_python("print('will not be run')",
  1034. stdin=tf, input='hare')
  1035. except ValueError, exception:
  1036. self.assertIn('stdin', exception.args[0])
  1037. self.assertIn('input', exception.args[0])
  1038. else:
  1039. self.fail("Expected ValueError when stdin and input args supplied.")
  1040. def test_check_output_timeout(self):
  1041. try:
  1042. cp = self.run_python((
  1043. "import sys, time\n"
  1044. "sys.stdout.write('BDFL')\n"
  1045. "sys.stdout.flush()\n"
  1046. "time.sleep(3600)"),
  1047. # Some heavily loaded buildbots (sparc Debian 3.x) require
  1048. # this much time to start and print.
  1049. timeout=3, stdout=subprocess.PIPE)
  1050. except subprocess.TimeoutExpired, exception:
  1051. self.assertEqual(exception.output, 'BDFL')
  1052. # output is aliased to stdout
  1053. self.assertEqual(exception.stdout, 'BDFL')
  1054. else:
  1055. self.fail("TimeoutExpired not raised")
  1056. def test_run_kwargs(self):
  1057. newenv = os.environ.copy()
  1058. newenv["FRUIT"] = "banana"
  1059. cp = self.run_python(('import sys, os;'
  1060. 'os.getenv("FRUIT")=="banana" and sys.exit(33) or sys.exit(31)'),
  1061. env=newenv)
  1062. self.assertEqual(cp.returncode, 33)
  1063. # context manager
  1064. class _SuppressCoreFiles(object):
  1065. """Try to prevent core files from being created."""
  1066. old_limit = None
  1067. def __enter__(self):
  1068. """Try to save previous ulimit, then set it to (0, 0)."""
  1069. try:
  1070. import resource
  1071. self.old_limit = resource.getrlimit(resource.RLIMIT_CORE)
  1072. resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
  1073. except (ImportError, ValueError, resource.error):
  1074. pass
  1075. def __exit__(self, *args):
  1076. """Return core file behavior to default."""
  1077. if self.old_limit is None:
  1078. return
  1079. try:
  1080. import resource
  1081. resource.setrlimit(resource.RLIMIT_CORE, self.old_limit)
  1082. except (ImportError, ValueError, resource.error):
  1083. pass
  1084. #@unittest.skipIf(mswindows, "POSIX specific tests")
  1085. class POSIXProcessTestCase(BaseTestCase):
  1086. def setUp(self):
  1087. BaseTestCase.setUp(self)
  1088. self._nonexistent_dir = "/_this/pa.th/does/not/exist"
  1089. def _get_chdir_exception(self):
  1090. try:
  1091. os.chdir(self._nonexistent_dir)
  1092. except OSError, e:
  1093. # This avoids hard coding the errno value or the OS perror()
  1094. # string and instead capture the exception that we want to see
  1095. # below for comparison.
  1096. desired_exception = e
  1097. desired_exception.strerror += ': ' + repr(self._nonexistent_dir)
  1098. else:
  1099. self.fail("chdir to nonexistant directory %s succeeded." %
  1100. self._nonexistent_dir)
  1101. return desired_exception
  1102. def test_exception_cwd(self):
  1103. """Test error in the child raised in the parent for a bad cwd."""
  1104. desired_exception = self._get_chdir_exception()
  1105. try:
  1106. p = subprocess.Popen([sys.executable, "-c", yenv + ""],
  1107. cwd=self._nonexistent_dir)
  1108. except OSError, e:
  1109. # Test that the child process chdir failure actually makes
  1110. # it up to the parent process as the correct exception.
  1111. self.assertEqual(desired_exception.errno, e.errno)
  1112. self.assertEqual(desired_exception.strerror, e.strerror)
  1113. else:
  1114. self.fail("Expected OSError: %s" % desired_exception)
  1115. def test_exception_bad_executable(self):
  1116. """Test error in the child raised in the parent for a bad executable."""
  1117. desired_exception = self._get_chdir_exception()
  1118. try:
  1119. p = subprocess.Popen([sys.executable, "-c", yenv + ""],
  1120. executable=self._nonexistent_dir)
  1121. except OSError, e:
  1122. # Test that the child process exec failure actually makes
  1123. # it up to the parent process as the correct exception.
  1124. self.assertEqual(desired_exception.errno, e.errno)
  1125. self.assertEqual(desired_exception.strerror, e.strerror)
  1126. else:
  1127. self.fail("Expected OSError: %s" % desired_exception)
  1128. def test_exception_bad_args_0(self):
  1129. """Test error in the child raised in the parent for a bad args[0]."""
  1130. desired_exception = self._get_chdir_exception()
  1131. try:
  1132. p = subprocess.Popen([self._nonexistent_dir, "-c", yenv + ""])
  1133. except OSError, e:
  1134. # Test that the child process exec failure actually makes
  1135. # it up to the parent process as the correct exception.
  1136. self.assertEqual(desired_exception.errno, e.errno)
  1137. self.assertEqual(desired_exception.strerror, e.strerror)
  1138. else:
  1139. self.fail("Expected OSError: %s" % desired_exception)
  1140. #@unittest.skipIf(not os.path.exists('/proc/self/status'))
  1141. def test_restore_signals(self):
  1142. if not os.path.exists('/proc/self/status'):
  1143. print("SKIP - Functional test requires /proc/self/status.")
  1144. return
  1145. # Blindly assume that cat exists on systems with /proc/self/status...
  1146. default_proc_status = subprocess.check_output(
  1147. ['cat', '/proc/self/status'],
  1148. restore_signals=False)
  1149. for line in default_proc_status.splitlines():
  1150. if line.startswith(b'SigIgn'):
  1151. default_sig_ign_mask = line
  1152. break
  1153. else:
  1154. self.skipTest("SigIgn not found in /proc/self/status.")
  1155. restored_proc_status = subprocess.check_output(
  1156. ['cat', '/proc/self/status'],
  1157. restore_signals=True)
  1158. for line in restored_proc_status.splitlines():
  1159. if line.startswith(b'SigIgn'):
  1160. restored_sig_ign_mask = line
  1161. break
  1162. # restore_signals=True should've unblocked SIGPIPE and friends.
  1163. self.assertNotEqual(default_sig_ign_mask, restored_sig_ign_mask)
  1164. def test_start_new_session(self):
  1165. # For code coverage of calling setsid(). We don't care if we get an
  1166. # EPERM error from it depending on the test execution environment, that
  1167. # still indicates that it was called.
  1168. try:
  1169. output = subprocess.check_output(
  1170. [sys.executable, "-c", yenv +
  1171. "import os; print(os.getpgid(os.getpid()))"],
  1172. start_new_session=True)
  1173. except OSError, e:
  1174. if e.errno != errno.EPERM:
  1175. raise
  1176. else:
  1177. parent_pgid = os.getpgid(os.getpid())
  1178. child_pgid = int(output)
  1179. self.assertNotEqual(parent_pgid, child_pgid)
  1180. def test_run_abort(self):
  1181. # returncode handles signal termination
  1182. scf = _SuppressCoreFiles()
  1183. scf.__enter__()
  1184. try:
  1185. p = subprocess.Popen([sys.executable, "-c", yenv +
  1186. "import os; os.abort()"])
  1187. p.wait()
  1188. finally:
  1189. scf.__exit__()
  1190. self.assertEqual(-p.returncode, signal.SIGABRT)
  1191. def test_preexec(self):
  1192. # DISCLAIMER: Setting environment variables is *not* a good use
  1193. # of a preexec_fn. This is merely a test.
  1194. p = subprocess.Popen([sys.executable, "-c", yenv +
  1195. "import sys, os;"
  1196. "sys.stdout.write(os.getenv('FRUIT'))"],
  1197. stdout=subprocess.PIPE,
  1198. preexec_fn=lambda: os.putenv("FRUIT", "apple"))
  1199. self.assertEqual(p.stdout.read(), "apple")
  1200. def test_preexec_exception(self):
  1201. def raise_it():
  1202. raise ValueError("What if two swallows carried a coconut?")
  1203. try:
  1204. p = subprocess.Popen([sys.executable, "-c", yenv + ""],
  1205. preexec_fn=raise_it)
  1206. except RuntimeError, e:
  1207. self.assertTrue(
  1208. subprocess._posixsubprocess,
  1209. "Expected a ValueError from the preexec_fn")
  1210. except ValueError, e:
  1211. self.assertIn("coconut", e.args[0])
  1212. else:
  1213. self.fail("Exception raised by preexec_fn did not make it "
  1214. "to the parent process.")
  1215. class _TestExecuteChildPopen(subprocess.Popen):
  1216. """Used to test behavior at the end of _execute_child."""
  1217. def __init__(self, testcase, *args, **kwargs):
  1218. self._testcase = testcase
  1219. subprocess.Popen.__init__(self, *args, **kwargs)
  1220. def _execute_child(self, *args, **kwargs):
  1221. try:
  1222. subprocess.Popen._execute_child(self, *args, **kwargs)
  1223. finally:
  1224. # Open a bunch of file descriptors and verify that
  1225. # none of them are the same as the ones the Popen
  1226. # instance is using for stdin/stdout/stderr.
  1227. devzero_fds = [os.open("/dev/zero", os.O_RDONLY)
  1228. for _ in range(8)]
  1229. try:
  1230. for fd in devzero_fds:
  1231. self._testcase.assertNotIn(
  1232. fd, (self.stdin.fileno(), self.stdout.fileno(),
  1233. self.stderr.fileno()),
  1234. msg="At least one fd was closed early.")
  1235. finally:
  1236. map(os.close, devzero_fds)
  1237. #@unittest.skipIf(not os.path.exists("/dev/zero"), "/dev/zero required.")
  1238. def test_preexec_errpipe_does_not_double_close_pipes(self):
  1239. """Issue16140: Don't double close pipes on preexec error."""
  1240. def raise_it():
  1241. raise RuntimeError("force the _execute_child() errpipe_data path.")
  1242. try:
  1243. self._TestExecuteChildPopen(
  1244. self, [sys.executable, "-c", yenv + "pass"],
  1245. stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  1246. stderr=subprocess.PIPE, preexec_fn=raise_it)
  1247. except RuntimeError:
  1248. pass # Yay! Because 2.4 doesn't support with statements.
  1249. else:
  1250. self.fail("RuntimeError expected but not raised.")
  1251. #@unittest.skipUnless(gc, "Requires a gc module.")
  1252. def test_preexec_gc_module_failure(self):
  1253. # This tests the code that disables garbage collection if the child
  1254. # process will execute any Python.
  1255. def raise_runtime_error():
  1256. raise RuntimeError("this shouldn't escape")
  1257. enabled = gc.isenabled()
  1258. orig_gc_disable = gc.disable
  1259. orig_gc_isenabled = gc.isenabled
  1260. try:
  1261. gc.disable()
  1262. self.assertFalse(gc.isenabled())
  1263. subprocess.call([sys.executable, '-c', yenv + ''],
  1264. preexec_fn=lambda: None)
  1265. self.assertFalse(gc.isenabled(),
  1266. "Popen enabled gc when it shouldn't.")
  1267. gc.enable()
  1268. self.assertTrue(gc.isenabled())
  1269. subprocess.call([sys.executable, '-c', yenv + ''],
  1270. preexec_fn=lambda: None)
  1271. self.assertTrue(gc.isenabled(), "Popen left gc disabled.")
  1272. gc.disable = raise_runtime_error
  1273. self.assertRaises(RuntimeError, subprocess.Popen,
  1274. [sys.executable, '-c', yenv + ''],
  1275. preexec_fn=lambda: None)
  1276. del gc.isenabled # force an AttributeError
  1277. self.assertRaises(AttributeError, subprocess.Popen,
  1278. [sys.executable, '-c', yenv + ''],
  1279. preexec_fn=lambda: None)
  1280. finally:
  1281. gc.disable = orig_gc_disable
  1282. gc.isenabled = orig_gc_isenabled
  1283. if not enabled:
  1284. gc.disable()
  1285. def test_args_string(self):
  1286. # args is a string
  1287. f, fname = mkstemp()
  1288. os.write(f, "#!/bin/sh\n")
  1289. os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
  1290. sys.executable)
  1291. os.close(f)
  1292. os.chmod(fname, 0700)
  1293. p = subprocess.Popen(fname)
  1294. p.wait()
  1295. os.remove(fname)
  1296. self.assertEqual(p.returncode, 47)
  1297. def test_invalid_args(self):
  1298. # invalid arguments should raise ValueError
  1299. self.assertRaises(ValueError, subprocess.call,
  1300. [sys.executable, "-c", yenv +
  1301. "import sys; sys.exit(47)"],
  1302. startupinfo=47)
  1303. self.assertRaises(ValueError, subprocess.call,
  1304. [sys.executable, "-c", yenv +
  1305. "import sys; sys.exit(47)"],
  1306. creationflags=47)
  1307. def test_shell_sequence(self):
  1308. # Run command through the shell (sequence)
  1309. newenv = os.environ.copy()
  1310. newenv["FRUIT"] = "apple"
  1311. p = subprocess.Popen(["echo $FRUIT"], shell=1,
  1312. stdout=subprocess.PIPE,
  1313. env=newenv)
  1314. self.assertEqual(p.stdout.read().strip(), "apple")
  1315. def test_shell_string(self):
  1316. # Run command through the shell (string)
  1317. newenv = os.environ.copy()
  1318. newenv["FRUIT"] = "apple"
  1319. p = subprocess.Popen("echo $FRUIT", shell=1,
  1320. stdout=subprocess.PIPE,
  1321. env=newenv)
  1322. self.assertEqual(p.stdout.read().strip(), "apple")
  1323. def test_call_string(self):
  1324. # call() function with string argument on UNIX
  1325. f, fname = mkstemp()
  1326. os.write(f, "#!/bin/sh\n")
  1327. os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
  1328. sys.executable)
  1329. os.close(f)
  1330. os.chmod(fname, 0700)
  1331. rc = subprocess.call(fname)
  1332. os.remove(fname)
  1333. self.assertEqual(rc, 47)
  1334. def test_specific_shell(self):
  1335. # Issue #9265: Incorrect name passed as arg[0].
  1336. shells = []
  1337. for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']:
  1338. for name in ['bash', 'ksh']:
  1339. sh = os.path.join(prefix, name)
  1340. if os.path.isfile(sh):
  1341. shells.append(sh)
  1342. if not shells: # Will probably work for any shell but csh.
  1343. self.skipTest("bash or ksh required for this test")
  1344. sh = '/bin/sh'
  1345. if os.path.isfile(sh) and not os.path.islink(sh):
  1346. # Test will fail if /bin/sh is a symlink to csh.
  1347. shells.append(sh)
  1348. for sh in shells:
  1349. p = subprocess.Popen("echo $0", executable=sh, shell=True,
  1350. stdout=subprocess.PIPE)
  1351. self.assertEqual(p.stdout.read().strip(), sh)
  1352. def _kill_process(self, method, *args):
  1353. # Do not inherit file handles from the parent.
  1354. # It should fix failures on some platforms.
  1355. p = subprocess.Popen([sys.executable, "-c", yenv + """if 1:
  1356. import sys, time
  1357. sys.stdout.write('x\\n')
  1358. sys.stdout.flush()
  1359. time.sleep(30)
  1360. """],
  1361. close_fds=True,
  1362. stdin=subprocess.PIPE,
  1363. stdout=subprocess.PIPE,
  1364. stderr=subprocess.PIPE)
  1365. # Wait for the interpreter to be completely initialized before
  1366. # sending any signal.
  1367. p.stdout.read(1)
  1368. getattr(p, method)(*args)
  1369. return p
  1370. def test_send_signal(self):
  1371. p = self._kill_process('send_signal', signal.SIGINT)
  1372. _, stderr = p.communicate()
  1373. self.assertIn('KeyboardInterrupt', stderr)
  1374. self.assertNotEqual(p.wait(), 0)
  1375. def test_kill(self):
  1376. p = self._kill_process('kill')
  1377. _, stderr = p.communicate()
  1378. self.assertStderrEqual(stderr, '')
  1379. self.assertEqual(p.wait(), -signal.SIGKILL)
  1380. def test_terminate(self):
  1381. p = self._kill_process('terminate')
  1382. _, stderr = p.communicate()
  1383. self.assertStderrEqual(stderr, '')
  1384. self.assertEqual(p.wait(), -signal.SIGTERM)
  1385. def check_close_std_fds(self, fds):
  1386. # Issue #9905: test that subprocess pipes still work properly with
  1387. # some standard fds closed
  1388. stdin = 0
  1389. newfds = []
  1390. for a in fds:
  1391. b = os.dup(a)
  1392. newfds.append(b)
  1393. if a == 0:
  1394. stdin = b
  1395. try:
  1396. for fd in fds:
  1397. os.close(fd)
  1398. out, err = subprocess.Popen([sys.executable, "-c", yenv +
  1399. 'import sys;'
  1400. 'sys.stdout.write("apple");'
  1401. 'sys.stdout.flush();'
  1402. 'sys.stderr.write("orange")'],
  1403. stdin=stdin,
  1404. stdout=subprocess.PIPE,
  1405. stderr=subprocess.PIPE).communicate()
  1406. err = strip_python_stderr(err)
  1407. self.assertEqual((out, err), ('apple', 'orange'))
  1408. finally:
  1409. for b, a in zip(newfds, fds):
  1410. os.dup2(b, a)
  1411. for b in newfds:
  1412. os.close(b)
  1413. def test_close_fd_0(self):
  1414. self.check_close_std_fds([0])
  1415. def test_close_fd_1(self):
  1416. self.check_close_std_fds([1])
  1417. def test_close_fd_2(self):
  1418. self.check_close_std_fds([2])
  1419. def test_close_fds_0_1(self):
  1420. self.check_close_std_fds([0, 1])
  1421. def test_close_fds_0_2(self):
  1422. self.check_close_std_fds([0, 2])
  1423. def test_close_fds_1_2(self):
  1424. self.check_close_std_fds([1, 2])
  1425. def test_close_fds_0_1_2(self):
  1426. # Issue #10806: test that subprocess pipes still work properly with
  1427. # all standard fds closed.
  1428. self.check_close_std_fds([0, 1, 2])
  1429. def check_swap_fds(self, stdin_no, stdout_no, stderr_no):
  1430. # open up some temporary files
  1431. temps = [mkstemp() for i in range(3)]
  1432. temp_fds = [fd for fd, fname in temps]
  1433. try:
  1434. # unlink the files -- we won't need to reopen them
  1435. for fd, fname in temps:
  1436. os.unlink(fname)
  1437. # save a copy of the standard file descriptors
  1438. saved_fds = [os.dup(fd) for fd in range(3)]
  1439. try:
  1440. # duplicate the temp files over the standard fd's 0, 1, 2
  1441. for fd, temp_fd in enumerate(temp_fds):
  1442. os.dup2(temp_fd, fd)
  1443. # write some data to what will become stdin, and rewind
  1444. os.write(stdin_no, "STDIN")
  1445. os.lseek(stdin_no, 0, 0)
  1446. # now use those files in the given order, so that subprocess
  1447. # has to rearrange them in the child
  1448. p = subprocess.Popen([sys.executable, "-c", yenv +
  1449. 'import sys; got = sys.stdin.read();'
  1450. 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
  1451. stdin=stdin_no,
  1452. stdout=stdout_no,
  1453. stderr=stderr_no)
  1454. p.wait()
  1455. for fd in temp_fds:
  1456. os.lseek(fd, 0, 0)
  1457. out = os.read(stdout_no, 1024)
  1458. err = os.read(stderr_no, 1024)
  1459. finally:
  1460. for std, saved in enumerate(saved_fds):
  1461. os.dup2(saved, std)
  1462. os.close(saved)
  1463. self.assertEqual(out, "got STDIN")
  1464. self.assertStderrEqual(err, "err")
  1465. finally:
  1466. for fd in temp_fds:
  1467. os.close(fd)
  1468. # When duping fds, if there arises a situation where one of the fds is
  1469. # either 0, 1 or 2, it is possible that it is overwritten (#12607).
  1470. # This tests all combinations of this.
  1471. def test_swap_fds(self):
  1472. self.check_swap_fds(0, 1, 2)
  1473. self.check_swap_fds(0, 2, 1)
  1474. self.check_swap_fds(1, 0, 2)
  1475. self.check_swap_fds(1, 2, 0)
  1476. self.check_swap_fds(2, 0, 1)
  1477. self.check_swap_fds(2, 1, 0)
  1478. def test_small_errpipe_write_fd(self):
  1479. """Issue #15798: Popen should work when stdio fds are available."""
  1480. new_stdin = os.dup(0)
  1481. new_stdout = os.dup(1)
  1482. try:
  1483. os.close(0)
  1484. os.close(1)
  1485. subprocess.Popen([
  1486. sys.executable, "-c", yenv + "pass"]).wait()
  1487. finally:
  1488. # Restore original stdin and stdout
  1489. os.dup2(new_stdin, 0)
  1490. os.dup2(new_stdout, 1)
  1491. os.close(new_stdin)
  1492. os.close(new_stdout)
  1493. def test_remapping_std_fds(self):
  1494. # open up some temporary files
  1495. temps = [mkstemp() for i in range(3)]
  1496. try:
  1497. temp_fds = [fd for fd, fname in temps]
  1498. # unlink the files -- we won't need to reopen them
  1499. for fd, fname in temps:
  1500. os.unlink(fname)
  1501. # write some data to what will become stdin, and rewind
  1502. os.write(temp_fds[1], "STDIN")
  1503. os.lseek(temp_fds[1], 0, 0)
  1504. # move the standard file descriptors out of the way
  1505. saved_fds = [os.dup(fd) for fd in range(3)]
  1506. try:
  1507. # duplicate the file objects over the standard fd's
  1508. for fd, temp_fd in enumerate(temp_fds):
  1509. os.dup2(temp_fd, fd)
  1510. # now use those files in the "wrong" order, so that subprocess
  1511. # has to rearrange them in the child
  1512. p = subprocess.Popen([sys.executable, "-c", yenv +
  1513. 'import sys; got = sys.stdin.read();'
  1514. 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
  1515. stdin=temp_fds[1],
  1516. stdout=temp_fds[2],
  1517. stderr=temp_fds[0])
  1518. p.wait()
  1519. finally:
  1520. # restore the original fd's underneath sys.stdin, etc.
  1521. for std, saved in enumerate(saved_fds):
  1522. os.dup2(saved, std)
  1523. os.close(saved)
  1524. for fd in temp_fds:
  1525. os.lseek(fd, 0, 0)
  1526. out = os.read(temp_fds[2], 1024)
  1527. err = os.read(temp_fds[0], 1024)
  1528. self.assertEqual(out, "got STDIN")
  1529. self.assertStderrEqual(err, "err")
  1530. finally:
  1531. for fd in temp_fds:
  1532. os.close(fd)
  1533. # NOTE: test_surrogates_error_message makes no sense on python 2.x. omitted.
  1534. # NOTE: test_undecodable_env makes no sense on python 2.x. omitted.
  1535. # NOTE: test_bytes_program makes no sense on python 2.x. omitted.
  1536. if sys.version_info[:2] >= (2,7):
  1537. # Disabling this test on 2.6 and earlier as it fails on Travis CI regardless
  1538. # of LANG=C being set and is not worth the time to figure out why in such a
  1539. # legacy environment..
  1540. # https://travis-ci.org/google/python-subprocess32/jobs/290065729
  1541. def test_fs_encode_unicode_error(self):
  1542. fs_encoding = sys.getfilesystemencoding()
  1543. if fs_encoding.upper() not in ("ANSI_X3.4-1968", "ASCII"):
  1544. self.skipTest(
  1545. "Requires a restictive sys.filesystemencoding(), "
  1546. "not %s. Run python with LANG=C" % fs_encoding)
  1547. highbit_executable_name = os.path.join(
  1548. test_support.findfile("testdata"), u"Does\\Not\uDCff\\Exist")
  1549. try:
  1550. subprocess.call([highbit_executable_name])
  1551. except UnicodeEncodeError:
  1552. return
  1553. except RuntimeError, e:
  1554. # The ProcessTestCasePOSIXPurePython version ends up here. It
  1555. # can't re-construct the unicode error from the child because it
  1556. # doesn't have all the arguments. BFD. One doesn't use
  1557. # subprocess32 for the old pure python implementation...
  1558. if "UnicodeEncodeError" not in str(e):
  1559. self.fail("Expected a RuntimeError whining about how a "
  1560. "UnicodeEncodeError from the child could not "
  1561. "be reraised. Not: %s" % e)
  1562. return
  1563. self.fail("Expected a UnicodeEncodeError to be raised.")
  1564. def test_pipe_cloexec(self):
  1565. sleeper = test_support.findfile("testdata/input_reader.py")
  1566. fd_status = test_support.findfile("testdata/fd_status.py")
  1567. p1 = subprocess.Popen([sys.executable, sleeper],
  1568. stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  1569. stderr=subprocess.PIPE, close_fds=False)
  1570. self.addCleanup(p1.communicate, '')
  1571. p2 = subprocess.Popen([sys.executable, fd_status],
  1572. stdout=subprocess.PIPE, close_fds=False)
  1573. output, error = p2.communicate()
  1574. result_fds = set(map(int, output.split(',')))
  1575. unwanted_fds = set([p1.stdin.fileno(), p1.stdout.fileno(),
  1576. p1.stderr.fileno()])
  1577. self.assertFalse(result_fds & unwanted_fds,
  1578. "Expected no fds from %r to be open in child, "
  1579. "found %r" %
  1580. (unwanted_fds, result_fds & unwanted_fds))
  1581. def test_pipe_cloexec_real_tools(self):
  1582. qcat = test_support.findfile("testdata/qcat.py")
  1583. qgrep = test_support.findfile("testdata/qgrep.py")
  1584. subdata = 'zxcvbn'
  1585. data = subdata * 4 + '\n'
  1586. p1 = subprocess.Popen([sys.executable, qcat],
  1587. stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  1588. close_fds=False)
  1589. p2 = subprocess.Popen([sys.executable, qgrep, subdata],
  1590. stdin=p1.stdout, stdout=subprocess.PIPE,
  1591. close_fds=False)
  1592. self.addCleanup(p1.wait)
  1593. self.addCleanup(p2.wait)
  1594. def kill_p1():
  1595. try:
  1596. p1.terminate()
  1597. except ProcessLookupError:
  1598. pass
  1599. def kill_p2():
  1600. try:
  1601. p2.terminate()
  1602. except ProcessLookupError:
  1603. pass
  1604. self.addCleanup(kill_p1)
  1605. self.addCleanup(kill_p2)
  1606. p1.stdin.write(data)
  1607. p1.stdin.close()
  1608. readfiles, ignored1, ignored2 = select.select([p2.stdout], [], [], 10)
  1609. self.assertTrue(readfiles, "The child hung")
  1610. self.assertEqual(p2.stdout.read(), data)
  1611. p1.stdout.close()
  1612. p2.stdout.close()
  1613. def test_close_fds(self):
  1614. fd_status = test_support.findfile("testdata/fd_status.py")
  1615. fds = os.pipe()
  1616. self.addCleanup(os.close, fds[0])
  1617. self.addCleanup(os.close, fds[1])
  1618. open_fds = set(fds)
  1619. # add a bunch more fds
  1620. for _ in range(9):
  1621. fd = os.open("/dev/null", os.O_RDONLY)
  1622. self.addCleanup(os.close, fd)
  1623. open_fds.add(fd)
  1624. p = subprocess.Popen([sys.executable, fd_status],
  1625. stdout=subprocess.PIPE, close_fds=False)
  1626. output, ignored = p.communicate()
  1627. remaining_fds = set(map(int, output.split(',')))
  1628. self.assertEqual(remaining_fds & open_fds, open_fds,
  1629. "Some fds were closed")
  1630. p = subprocess.Popen([sys.executable, fd_status],
  1631. stdout=subprocess.PIPE, close_fds=True)
  1632. output, ignored = p.communicate()
  1633. remaining_fds = set(map(int, output.split(',')))
  1634. self.assertFalse(remaining_fds & open_fds,
  1635. "Some fds were left open")
  1636. self.assertIn(1, remaining_fds, "Subprocess failed")
  1637. # Keep some of the fd's we opened open in the subprocess.
  1638. # This tests _posixsubprocess.c's proper handling of fds_to_keep.
  1639. fds_to_keep = set(open_fds.pop() for _ in range(8))
  1640. p = subprocess.Popen([sys.executable, fd_status],
  1641. stdout=subprocess.PIPE, close_fds=True,
  1642. pass_fds=())
  1643. output, ignored = p.communicate()
  1644. remaining_fds = set(map(int, output.split(',')))
  1645. self.assertFalse(remaining_fds & fds_to_keep & open_fds,
  1646. "Some fds not in pass_fds were left open")
  1647. self.assertIn(1, remaining_fds, "Subprocess failed")
  1648. def test_close_fds_when_max_fd_is_lowered(self):
  1649. """Confirm that issue21618 is fixed (may fail under valgrind)."""
  1650. fd_status = test_support.findfile("testdata/fd_status.py")
  1651. open_fds = set()
  1652. # Add a bunch more fds to pass down.
  1653. for _ in range(40):
  1654. fd = os.open("/dev/null", os.O_RDONLY)
  1655. open_fds.add(fd)
  1656. # Leave a two pairs of low ones available for use by the
  1657. # internal child error pipe and the stdout pipe.
  1658. # We also leave 10 more open for use by the Python 2 startup
  1659. # import machinery which tends to consume several at once.
  1660. for fd in sorted(open_fds)[:14]:
  1661. os.close(fd)
  1662. open_fds.remove(fd)
  1663. for fd in open_fds:
  1664. self.addCleanup(os.close, fd)
  1665. max_fd_open = max(open_fds)
  1666. import resource
  1667. rlim_cur, rlim_max = resource.getrlimit(resource.RLIMIT_NOFILE)
  1668. try:
  1669. # 29 is lower than the highest fds we are leaving open.
  1670. resource.setrlimit(resource.RLIMIT_NOFILE, (29, rlim_max))
  1671. # Launch a new Python interpreter with our low fd rlim_cur that
  1672. # inherits open fds above that limit. It then uses subprocess
  1673. # with close_fds=True to get a report of open fds in the child.
  1674. # An explicit list of fds to check is passed to fd_status.py as
  1675. # letting fd_status rely on its default logic would miss the
  1676. # fds above rlim_cur as it normally only checks up to that limit.
  1677. p = subprocess.Popen(
  1678. [sys.executable, '-c', yenv +
  1679. textwrap.dedent("""
  1680. import subprocess32, sys
  1681. subprocess32.Popen([sys.executable, %(fd_status)r] +
  1682. [str(x) for x in range(%(max_fd)d)],
  1683. close_fds=True).wait()
  1684. """ % dict(fd_status=fd_status, max_fd=max_fd_open+1))],
  1685. stdout=subprocess.PIPE, close_fds=False)
  1686. finally:
  1687. resource.setrlimit(resource.RLIMIT_NOFILE, (rlim_cur, rlim_max))
  1688. output, unused_stderr = p.communicate()
  1689. remaining_fds = set(map(int, output.strip().split(',')))
  1690. self.assertFalse(remaining_fds & open_fds,
  1691. msg="Some fds were left open.")
  1692. def test_pass_fds(self):
  1693. fd_status = test_support.findfile("testdata/fd_status.py")
  1694. open_fds = set()
  1695. for x in range(5):
  1696. fds = os.pipe()
  1697. self.addCleanup(os.close, fds[0])
  1698. self.addCleanup(os.close, fds[1])
  1699. open_fds.update(fds)
  1700. for fd in open_fds:
  1701. p = subprocess.Popen([sys.executable, fd_status],
  1702. stdout=subprocess.PIPE, close_fds=True,
  1703. pass_fds=(fd, ))
  1704. output, ignored = p.communicate()
  1705. remaining_fds = set(map(int, output.split(',')))
  1706. to_be_closed = open_fds - set((fd,))
  1707. self.assertIn(fd, remaining_fds, "fd to be passed not passed")
  1708. self.assertFalse(remaining_fds & to_be_closed,
  1709. "fd to be closed passed")
  1710. # Syntax requires Python 2.5, assertWarns requires Python 2.7.
  1711. #with self.assertWarns(RuntimeWarning) as context:
  1712. # self.assertFalse(subprocess.call(
  1713. # [sys.executable, "-c", yenv + "import sys; sys.exit(0)"],
  1714. # close_fds=False, pass_fds=(fd, )))
  1715. #self.assertIn('overriding close_fds', str(context.warning))
  1716. def test_stdout_stdin_are_single_inout_fd(self):
  1717. inout = open(os.devnull, "r+")
  1718. try:
  1719. p = subprocess.Popen([sys.executable, "-c", yenv + "import sys; sys.exit(0)"],
  1720. stdout=inout, stdin=inout)
  1721. p.wait()
  1722. finally:
  1723. inout.close()
  1724. def test_stdout_stderr_are_single_inout_fd(self):
  1725. inout = open(os.devnull, "r+")
  1726. try:
  1727. p = subprocess.Popen([sys.executable, "-c", yenv + "import sys; sys.exit(0)"],
  1728. stdout=inout, stderr=inout)
  1729. p.wait()
  1730. finally:
  1731. inout.close()
  1732. def test_stderr_stdin_are_single_inout_fd(self):
  1733. inout = open(os.devnull, "r+")
  1734. try:
  1735. p = subprocess.Popen([sys.executable, "-c", yenv + "import sys; sys.exit(0)"],
  1736. stderr=inout, stdin=inout)
  1737. p.wait()
  1738. finally:
  1739. inout.close()
  1740. def test_wait_when_sigchild_ignored(self):
  1741. # NOTE: sigchild_ignore.py may not be an effective test on all OSes.
  1742. sigchild_ignore = test_support.findfile("testdata/sigchild_ignore.py")
  1743. p = subprocess.Popen([sys.executable, sigchild_ignore],
  1744. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  1745. stdout, stderr = p.communicate()
  1746. self.assertEqual(0, p.returncode, "sigchild_ignore.py exited"
  1747. " non-zero with this error:\n%s" % stderr)
  1748. def test_select_unbuffered(self):
  1749. # Issue #11459: bufsize=0 should really set the pipes as
  1750. # unbuffered (and therefore let select() work properly).
  1751. p = subprocess.Popen([sys.executable, "-c", yenv +
  1752. 'import sys;'
  1753. 'sys.stdout.write("apple")'],
  1754. stdout=subprocess.PIPE,
  1755. bufsize=0)
  1756. f = p.stdout
  1757. self.addCleanup(f.close)
  1758. try:
  1759. self.assertEqual(f.read(4), "appl")
  1760. self.assertIn(f, select.select([f], [], [], 0.0)[0])
  1761. finally:
  1762. p.wait()
  1763. def test_zombie_fast_process_del(self):
  1764. # Issue #12650: on Unix, if Popen.__del__() was called before the
  1765. # process exited, it wouldn't be added to subprocess._active, and would
  1766. # remain a zombie.
  1767. # spawn a Popen, and delete its reference before it exits
  1768. p = subprocess.Popen([sys.executable, "-c", yenv +
  1769. 'import sys, time;'
  1770. 'time.sleep(0.2)'],
  1771. stdout=subprocess.PIPE,
  1772. stderr=subprocess.PIPE)
  1773. self.addCleanup(p.stdout.close)
  1774. self.addCleanup(p.stderr.close)
  1775. ident = id(p)
  1776. pid = p.pid
  1777. del p
  1778. # check that p is in the active processes list
  1779. self.assertIn(ident, [id(o) for o in subprocess._active])
  1780. def test_leak_fast_process_del_killed(self):
  1781. # Issue #12650: on Unix, if Popen.__del__() was called before the
  1782. # process exited, and the process got killed by a signal, it would never
  1783. # be removed from subprocess._active, which triggered a FD and memory
  1784. # leak.
  1785. # spawn a Popen, delete its reference and kill it
  1786. p = subprocess.Popen([sys.executable, "-c", yenv +
  1787. 'import time;'
  1788. 'time.sleep(3)'],
  1789. stdout=subprocess.PIPE,
  1790. stderr=subprocess.PIPE)
  1791. self.addCleanup(p.stdout.close)
  1792. self.addCleanup(p.stderr.close)
  1793. ident = id(p)
  1794. pid = p.pid
  1795. del p
  1796. os.kill(pid, signal.SIGKILL)
  1797. # check that p is in the active processes list
  1798. self.assertIn(ident, [id(o) for o in subprocess._active])
  1799. # let some time for the process to exit, and create a new Popen: this
  1800. # should trigger the wait() of p
  1801. time.sleep(0.2)
  1802. try:
  1803. proc = subprocess.Popen(['nonexisting_i_hope'],
  1804. stdout=subprocess.PIPE,
  1805. stderr=subprocess.PIPE)
  1806. proc.__exit__(None, None, None)
  1807. except EnvironmentError:
  1808. pass
  1809. else:
  1810. self.fail("EnvironmentError not raised.")
  1811. # p should have been wait()ed on, and removed from the _active list
  1812. self.assertRaises(OSError, os.waitpid, pid, 0)
  1813. self.assertNotIn(ident, [id(o) for o in subprocess._active])
  1814. def test_close_fds_after_preexec(self):
  1815. fd_status = test_support.findfile("testdata/fd_status.py")
  1816. # this FD is used as dup2() target by preexec_fn, and should be closed
  1817. # in the child process
  1818. fd = os.dup(1)
  1819. self.addCleanup(os.close, fd)
  1820. p = subprocess.Popen([sys.executable, fd_status],
  1821. stdout=subprocess.PIPE, close_fds=True,
  1822. preexec_fn=lambda: os.dup2(1, fd))
  1823. output, ignored = p.communicate()
  1824. remaining_fds = set(map(int, output.split(',')))
  1825. self.assertNotIn(fd, remaining_fds)
  1826. def test_child_terminated_in_stopped_state(self):
  1827. """Test wait() behavior when waitpid returns WIFSTOPPED; issue29335."""
  1828. if not ctypes:
  1829. sys.stderr.write('ctypes module required.\n')
  1830. return
  1831. if not sys.executable:
  1832. self.stderr.write('Test requires sys.executable.\n')
  1833. return
  1834. PTRACE_TRACEME = 0 # From glibc and MacOS (PT_TRACE_ME).
  1835. libc_name = ctypes.util.find_library('c')
  1836. libc = ctypes.CDLL(libc_name)
  1837. if not hasattr(libc, 'ptrace'):
  1838. self.stderr.write('ptrace() required.\n')
  1839. return
  1840. test_ptrace = subprocess.Popen(
  1841. [sys.executable, '-c', yenv + """if True:
  1842. import ctypes
  1843. libc = ctypes.CDLL({libc_name!r})
  1844. libc.ptrace({PTRACE_TRACEME}, 0, 0)
  1845. """.format(libc_name=libc_name, PTRACE_TRACEME=PTRACE_TRACEME)
  1846. ])
  1847. if test_ptrace.wait() != 0:
  1848. sys.stderr.write('ptrace() failed - unable to test.\n')
  1849. return
  1850. child = subprocess.Popen(
  1851. [sys.executable, '-c', yenv + """if True:
  1852. import ctypes
  1853. libc = ctypes.CDLL({libc_name!r})
  1854. libc.ptrace({PTRACE_TRACEME}, 0, 0)
  1855. libc.printf(ctypes.c_char_p(0xdeadbeef)) # Crash the process.
  1856. """.format(libc_name=libc_name, PTRACE_TRACEME=PTRACE_TRACEME)
  1857. ])
  1858. try:
  1859. returncode = child.wait()
  1860. except Exception, e:
  1861. child.kill() # Clean up the hung stopped process.
  1862. raise e
  1863. self.assertNotEqual(0, returncode)
  1864. self.assert_(returncode < 0, msg=repr(returncode)) # signal death, likely SIGSEGV.
  1865. if mswindows:
  1866. class POSIXProcessTestCase(unittest.TestCase): pass
  1867. #@unittest.skipUnless(mswindows, "Windows specific tests")
  1868. class Win32ProcessTestCase(BaseTestCase):
  1869. def test_startupinfo(self):
  1870. # startupinfo argument
  1871. # We uses hardcoded constants, because we do not want to
  1872. # depend on win32all.
  1873. STARTF_USESHOWWINDOW = 1
  1874. SW_MAXIMIZE = 3
  1875. startupinfo = subprocess.STARTUPINFO()
  1876. startupinfo.dwFlags = STARTF_USESHOWWINDOW
  1877. startupinfo.wShowWindow = SW_MAXIMIZE
  1878. # Since Python is a console process, it won't be affected
  1879. # by wShowWindow, but the argument should be silently
  1880. # ignored
  1881. subprocess.call([sys.executable, "-c", yenv + "import sys; sys.exit(0)"],
  1882. startupinfo=startupinfo)
  1883. def test_creationflags(self):
  1884. # creationflags argument
  1885. CREATE_NEW_CONSOLE = 16
  1886. sys.stderr.write(" a DOS box should flash briefly ...\n")
  1887. subprocess.call(sys.executable +
  1888. ' -c "import time; time.sleep(0.25)"',
  1889. creationflags=CREATE_NEW_CONSOLE)
  1890. def test_invalid_args(self):
  1891. # invalid arguments should raise ValueError
  1892. self.assertRaises(ValueError, subprocess.call,
  1893. [sys.executable, "-c", yenv +
  1894. "import sys; sys.exit(47)"],
  1895. preexec_fn=lambda: 1)
  1896. self.assertRaises(ValueError, subprocess.call,
  1897. [sys.executable, "-c", yenv +
  1898. "import sys; sys.exit(47)"],
  1899. stdout=subprocess.PIPE,
  1900. close_fds=True)
  1901. def test_close_fds(self):
  1902. # close file descriptors
  1903. rc = subprocess.call([sys.executable, "-c", yenv +
  1904. "import sys; sys.exit(47)"],
  1905. close_fds=True)
  1906. self.assertEqual(rc, 47)
  1907. def test_shell_sequence(self):
  1908. # Run command through the shell (sequence)
  1909. newenv = os.environ.copy()
  1910. newenv["FRUIT"] = "physalis"
  1911. p = subprocess.Popen(["set"], shell=1,
  1912. stdout=subprocess.PIPE,
  1913. env=newenv)
  1914. self.assertIn("physalis", p.stdout.read())
  1915. def test_shell_string(self):
  1916. # Run command through the shell (string)
  1917. newenv = os.environ.copy()
  1918. newenv["FRUIT"] = "physalis"
  1919. p = subprocess.Popen("set", shell=1,
  1920. stdout=subprocess.PIPE,
  1921. env=newenv)
  1922. self.assertIn("physalis", p.stdout.read())
  1923. def test_call_string(self):
  1924. # call() function with string argument on Windows
  1925. rc = subprocess.call(sys.executable +
  1926. ' -c "import sys; sys.exit(47)"')
  1927. self.assertEqual(rc, 47)
  1928. def _kill_process(self, method, *args):
  1929. # Some win32 buildbot raises EOFError if stdin is inherited
  1930. p = subprocess.Popen([sys.executable, "-c", yenv + "input()"],
  1931. stdin=subprocess.PIPE, stderr=subprocess.PIPE)
  1932. # Let the process initialize (Issue #3137)
  1933. time.sleep(0.1)
  1934. # The process should not terminate prematurely
  1935. self.assert_(p.poll() is None)
  1936. # Retry if the process do not receive the signal.
  1937. count, maxcount = 0, 3
  1938. while count < maxcount and p.poll() is None:
  1939. getattr(p, method)(*args)
  1940. time.sleep(0.1)
  1941. count += 1
  1942. returncode = p.poll()
  1943. self.assert_(returncode is not None, "the subprocess did not terminate")
  1944. if count > 1:
  1945. print >>sys.stderr, ("p.{}{} succeeded after "
  1946. "{} attempts".format(method, args, count))
  1947. _, stderr = p.communicate()
  1948. self.assertStderrEqual(stderr, '')
  1949. self.assertEqual(p.wait(), returncode)
  1950. self.assertNotEqual(returncode, 0)
  1951. def test_send_signal(self):
  1952. self._kill_process('send_signal', signal.SIGTERM)
  1953. def test_kill(self):
  1954. self._kill_process('kill')
  1955. def test_terminate(self):
  1956. self._kill_process('terminate')
  1957. if not mswindows:
  1958. class Win32ProcessTestCase(unittest.TestCase): pass
  1959. #@unittest.skipUnless(getattr(subprocess, '_has_poll', False),
  1960. # "poll system call not supported")
  1961. class ProcessTestCaseNoPoll(ProcessTestCase):
  1962. def setUp(self):
  1963. subprocess._has_poll = False
  1964. ProcessTestCase.setUp(self)
  1965. def tearDown(self):
  1966. subprocess._has_poll = True
  1967. ProcessTestCase.tearDown(self)
  1968. if not getattr(subprocess, '_has_poll', False):
  1969. class ProcessTestCaseNoPoll(unittest.TestCase): pass
  1970. #@unittest.skipUnless(getattr(subprocess, '_posixsubprocess', False),
  1971. # "_posixsubprocess extension module not found.")
  1972. class ProcessTestCasePOSIXPurePython(ProcessTestCase, POSIXProcessTestCase):
  1973. def setUp(self):
  1974. subprocess._posixsubprocess = None
  1975. ProcessTestCase.setUp(self)
  1976. POSIXProcessTestCase.setUp(self)
  1977. def tearDown(self):
  1978. subprocess._posixsubprocess = sys.modules['_posixsubprocess32']
  1979. POSIXProcessTestCase.tearDown(self)
  1980. ProcessTestCase.tearDown(self)
  1981. class POSIXSubprocessModuleTestCase(unittest.TestCase):
  1982. def test_fork_exec_sorted_fd_sanity_check(self):
  1983. # Issue #23564: sanity check the fork_exec() fds_to_keep sanity check.
  1984. _posixsubprocess = subprocess._posixsubprocess
  1985. gc_enabled = gc.isenabled()
  1986. try:
  1987. gc.enable()
  1988. for fds_to_keep in (
  1989. (-1, 2, 3, 4, 5), # Negative number.
  1990. ('str', 4), # Not an int.
  1991. (18, 23, 42, 2**63), # Out of range.
  1992. (5, 4), # Not sorted.
  1993. (6, 7, 7, 8), # Duplicate.
  1994. ):
  1995. try:
  1996. _posixsubprocess.fork_exec(
  1997. ["false"], ["false"],
  1998. True, fds_to_keep, None, ["env"],
  1999. -1, -1, -1, -1,
  2000. 1, 2, 3, 4,
  2001. True, True, None)
  2002. except ValueError, exception:
  2003. self.assertTrue('fds_to_keep' in str(exception),
  2004. msg=str(exception))
  2005. else:
  2006. self.fail("ValueError not raised, fds_to_keep=%s" %
  2007. (fds_to_keep,))
  2008. finally:
  2009. if not gc_enabled:
  2010. gc.disable()
  2011. def test_cloexec_pass_fds(self):
  2012. if not os.path.exists('/dev/null') or not os.path.isdir('/dev/fd'):
  2013. print("Skipped - This test requires /dev/null and /dev/fd/*.")
  2014. return
  2015. null_reader_proc = subprocess.Popen(
  2016. ["cat"],
  2017. stdin=open('/dev/null', 'rb'),
  2018. stdout=subprocess.PIPE)
  2019. try:
  2020. data = null_reader_proc.stdout
  2021. fd_name = '/dev/fd/%d' % data.fileno()
  2022. fd_reader_proc = subprocess.Popen(
  2023. ["cat", fd_name],
  2024. stdout=subprocess.PIPE,
  2025. stderr=subprocess.STDOUT, # Capture any error from cat.
  2026. pass_fds=(data.fileno(),))
  2027. try:
  2028. fddata = fd_reader_proc.stdout
  2029. self.assertEqual('', fddata.read())
  2030. finally:
  2031. fd_reader_proc.wait()
  2032. finally:
  2033. null_reader_proc.wait()
  2034. if not getattr(subprocess, '_posixsubprocess', False):
  2035. print >>sys.stderr, "_posixsubprocess extension module not found."
  2036. class ProcessTestCasePOSIXPurePython(unittest.TestCase): pass
  2037. class POSIXSubprocessModuleTestCase(unittest.TestCase): pass
  2038. class HelperFunctionTests(unittest.TestCase):
  2039. #@unittest.skipIf(mswindows, "errno and EINTR make no sense on windows")
  2040. def test_eintr_retry_call(self):
  2041. record_calls = []
  2042. def fake_os_func(*args):
  2043. record_calls.append(args)
  2044. if len(record_calls) == 2:
  2045. raise OSError(errno.EINTR, "fake interrupted system call")
  2046. return tuple(reversed(args))
  2047. self.assertEqual((999, 256),
  2048. subprocess._eintr_retry_call(fake_os_func, 256, 999))
  2049. self.assertEqual([(256, 999)], record_calls)
  2050. # This time there will be an EINTR so it will loop once.
  2051. self.assertEqual((666,),
  2052. subprocess._eintr_retry_call(fake_os_func, 666))
  2053. self.assertEqual([(256, 999), (666,), (666,)], record_calls)
  2054. if mswindows:
  2055. del test_eintr_retry_call
  2056. if not hasattr(unittest.TestCase, 'assertSequenceEqual'):
  2057. def assertSequenceEqual(self, seq1, seq2):
  2058. self.assertEqual(list(seq1), list(seq2))
  2059. def test_get_exec_path(self):
  2060. defpath_list = os.defpath.split(os.pathsep)
  2061. test_path = ['/monty', '/python', '', '/flying/circus']
  2062. test_env = {'PATH': os.pathsep.join(test_path)}
  2063. get_exec_path = subprocess._get_exec_path
  2064. saved_environ = os.environ
  2065. try:
  2066. os.environ = dict(test_env)
  2067. # Test that defaulting to os.environ works.
  2068. self.assertSequenceEqual(test_path, get_exec_path())
  2069. self.assertSequenceEqual(test_path, get_exec_path(env=None))
  2070. finally:
  2071. os.environ = saved_environ
  2072. # No PATH environment variable
  2073. self.assertSequenceEqual(defpath_list, get_exec_path({}))
  2074. # Empty PATH environment variable
  2075. self.assertSequenceEqual(('',), get_exec_path({'PATH':''}))
  2076. # Supplied PATH environment variable
  2077. self.assertSequenceEqual(test_path, get_exec_path(test_env))
  2078. def test_args_from_interpreter_flags(self):
  2079. if sys.version_info[:2] < (2,6):
  2080. print "Skipped - only useful on 2.6 and higher."
  2081. return
  2082. # Mostly just to call it for code coverage.
  2083. args_list = subprocess32._args_from_interpreter_flags()
  2084. self.assertTrue(isinstance(args_list, list), msg=repr(args_list))
  2085. def test_timeout_expired_unpickling(self):
  2086. """https://github.com/google/python-subprocess32/issues/57"""
  2087. t = subprocess32.TimeoutExpired(['command', 'arg1'], 5,
  2088. output='stdout!', stderr='err')
  2089. t_pickled = pickle.dumps(t)
  2090. t2 = pickle.loads(t_pickled)
  2091. self.assertEqual(t.cmd, t2.cmd)
  2092. self.assertEqual(t.timeout, t2.timeout)
  2093. self.assertEqual(t.output, t2.output)
  2094. self.assertEqual(t.stderr, t2.stderr)
  2095. def test_called_process_error_unpickling(self):
  2096. """https://github.com/google/python-subprocess32/issues/57"""
  2097. e = subprocess32.CalledProcessError(
  2098. 2, ['command', 'arg1'], output='stdout!', stderr='err')
  2099. e_pickled = pickle.dumps(e)
  2100. e2 = pickle.loads(e_pickled)
  2101. self.assertEqual(e.returncode, e2.returncode)
  2102. self.assertEqual(e.cmd, e2.cmd)
  2103. self.assertEqual(e.output, e2.output)
  2104. self.assertEqual(e.stderr, e2.stderr)
  2105. def reap_children():
  2106. """Use this function at the end of test_main() whenever sub-processes
  2107. are started. This will help ensure that no extra children (zombies)
  2108. stick around to hog resources and create problems when looking
  2109. for refleaks.
  2110. """
  2111. # Reap all our dead child processes so we don't leave zombies around.
  2112. # These hog resources and might be causing some of the buildbots to die.
  2113. if hasattr(os, 'waitpid'):
  2114. any_process = -1
  2115. while True:
  2116. try:
  2117. # This will raise an exception on Windows. That's ok.
  2118. pid, status = os.waitpid(any_process, os.WNOHANG)
  2119. if pid == 0:
  2120. break
  2121. except:
  2122. break
  2123. class ContextManagerTests(BaseTestCase):
  2124. def test_pipe(self):
  2125. proc = subprocess.Popen([sys.executable, "-c", yenv +
  2126. "import sys;"
  2127. "sys.stdout.write('stdout');"
  2128. "sys.stderr.write('stderr');"],
  2129. stdout=subprocess.PIPE,
  2130. stderr=subprocess.PIPE)
  2131. try:
  2132. self.assertEqual(proc.stdout.read(), "stdout")
  2133. self.assertStderrEqual(proc.stderr.read(), "stderr")
  2134. finally:
  2135. proc.__exit__(None, None, None)
  2136. self.assertTrue(proc.stdout.closed)
  2137. self.assertTrue(proc.stderr.closed)
  2138. def test_returncode(self):
  2139. proc = subprocess.Popen([sys.executable, "-c", yenv +
  2140. "import sys; sys.exit(100)"])
  2141. proc.__exit__(None, None, None)
  2142. # __exit__ calls wait(), so the returncode should be set
  2143. self.assertEqual(proc.returncode, 100)
  2144. def test_communicate_stdin(self):
  2145. proc = subprocess.Popen([sys.executable, "-c", yenv +
  2146. "import sys;"
  2147. "sys.exit(sys.stdin.read() == 'context')"],
  2148. stdin=subprocess.PIPE)
  2149. try:
  2150. proc.communicate("context")
  2151. self.assertEqual(proc.returncode, 1)
  2152. finally:
  2153. proc.__exit__(None, None, None)
  2154. def test_invalid_args(self):
  2155. try:
  2156. proc = subprocess.Popen(['nonexisting_i_hope'],
  2157. stdout=subprocess.PIPE,
  2158. stderr=subprocess.PIPE)
  2159. proc.__exit__(None, None, None)
  2160. except EnvironmentError, exception:
  2161. # ignore errors that indicate the command was not found
  2162. if exception.errno not in (errno.ENOENT, errno.EACCES):
  2163. raise
  2164. else:
  2165. self.fail("Expected an EnvironmentError exception.")
  2166. if sys.version_info[:2] <= (2,4):
  2167. # The test suite hangs during the pure python test on 2.4. No idea why.
  2168. # That is not the implementation anyone is using this module for anyways.
  2169. class ProcessTestCasePOSIXPurePython(unittest.TestCase): pass
  2170. def main():
  2171. unit_tests = (ProcessTestCase,
  2172. POSIXProcessTestCase,
  2173. POSIXSubprocessModuleTestCase,
  2174. Win32ProcessTestCase,
  2175. ProcessTestCasePOSIXPurePython,
  2176. ProcessTestCaseNoPoll,
  2177. HelperFunctionTests,
  2178. ContextManagerTests,
  2179. RunFuncTestCase,
  2180. )
  2181. test_support.run_unittest(*unit_tests)
  2182. reap_children()
  2183. if __name__ == "__main__":
  2184. main()