MCPcopy Index your code
hub / github.com/python/cpython / _run_child

Method _run_child

Lib/test/test_builtin.py:2632–2692  ·  view source on GitHub ↗
(self, child, terminal_input)

Source from the content-addressed store, hash-verified

2630
2631 @warnings_helper.ignore_fork_in_thread_deprecation_warnings()
2632 def _run_child(self, child, terminal_input):
2633 r, w = os.pipe() # Pipe test results from child back to parent
2634 try:
2635 pid, fd = pty.fork()
2636 except (OSError, AttributeError) as e:
2637 os.close(r)
2638 os.close(w)
2639 self.skipTest("pty.fork() raised {}".format(e))
2640 raise
2641
2642 if pid == 0:
2643 # Child
2644 try:
2645 os.close(r)
2646 with open(w, "w") as wpipe:
2647 child(wpipe)
2648 except:
2649 traceback.print_exc()
2650 finally:
2651 # We don't want to return to unittest...
2652 os._exit(0)
2653
2654 # Parent
2655 os.close(w)
2656 os.write(fd, terminal_input)
2657
2658 # Get results from the pipe
2659 with open(r, encoding="utf-8") as rpipe:
2660 lines = []
2661 while True:
2662 line = rpipe.readline().strip()
2663 if line == "":
2664 # The other end was closed => the child exited
2665 break
2666 lines.append(line)
2667
2668 # Check the result was got and corresponds to the user's terminal input
2669 if len(lines) != 2:
2670 # Something went wrong, try to get at stderr
2671 # Beware of Linux raising EIO when the slave is closed
2672 child_output = bytearray()
2673 while True:
2674 try:
2675 chunk = os.read(fd, 3000)
2676 except OSError: # Assume EIO
2677 break
2678 if not chunk:
2679 break
2680 child_output.extend(chunk)
2681 os.close(fd)
2682 child_output = child_output.decode("ascii", "ignore")
2683 self.fail("got %d lines in pipe but expected 2, child output was:\n%s"
2684 % (len(lines), child_output))
2685
2686 # bpo-40155: Close the PTY before waiting for the child process
2687 # completion, otherwise the child process hangs on AIX.
2688 os.close(fd)
2689

Callers 1

run_childMethod · 0.95

Calls 15

skipTestMethod · 0.80
print_excMethod · 0.80
openFunction · 0.50
pipeMethod · 0.45
closeMethod · 0.45
formatMethod · 0.45
_exitMethod · 0.45
writeMethod · 0.45
stripMethod · 0.45
readlineMethod · 0.45
appendMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected