| 1102 | return self |
| 1103 | |
| 1104 | def __exit__(self, exc_type, value, traceback): |
| 1105 | if self.stdout: |
| 1106 | self.stdout.close() |
| 1107 | if self.stderr: |
| 1108 | self.stderr.close() |
| 1109 | try: # Flushing a BufferedWriter may raise an error |
| 1110 | if self.stdin: |
| 1111 | self.stdin.close() |
| 1112 | finally: |
| 1113 | if exc_type == KeyboardInterrupt: |
| 1114 | # https://bugs.python.org/issue25942 |
| 1115 | # In the case of a KeyboardInterrupt we assume the SIGINT |
| 1116 | # was also already sent to our child processes. We can't |
| 1117 | # block indefinitely as that is not user friendly. |
| 1118 | # If we have not already waited a brief amount of time in |
| 1119 | # an interrupted .wait() or .communicate() call, do so here |
| 1120 | # for consistency. |
| 1121 | if self._sigint_wait_secs > 0: |
| 1122 | try: |
| 1123 | self._wait(timeout=self._sigint_wait_secs) |
| 1124 | except TimeoutExpired: |
| 1125 | pass |
| 1126 | self._sigint_wait_secs = 0 # Note that this has been done. |
| 1127 | else: |
| 1128 | # Wait for the process to terminate, to avoid zombies. |
| 1129 | self.wait() |
| 1130 | |
| 1131 | def __del__(self, _maxsize=sys.maxsize, _warn=warnings.warn): |
| 1132 | if not self._child_created: |