| 1156 | return self |
| 1157 | |
| 1158 | def __exit__(self, exc_type, value, traceback): |
| 1159 | if self.stdout: |
| 1160 | self.stdout.close() |
| 1161 | if self.stderr: |
| 1162 | self.stderr.close() |
| 1163 | try: # Flushing a BufferedWriter may raise an error |
| 1164 | if self.stdin: |
| 1165 | self.stdin.close() |
| 1166 | finally: |
| 1167 | if exc_type == KeyboardInterrupt: |
| 1168 | # https://bugs.python.org/issue25942 |
| 1169 | # In the case of a KeyboardInterrupt we assume the SIGINT |
| 1170 | # was also already sent to our child processes. We can't |
| 1171 | # block indefinitely as that is not user friendly. |
| 1172 | # If we have not already waited a brief amount of time in |
| 1173 | # an interrupted .wait() or .communicate() call, do so here |
| 1174 | # for consistency. |
| 1175 | if self._sigint_wait_secs > 0: |
| 1176 | try: |
| 1177 | self._wait(timeout=self._sigint_wait_secs) |
| 1178 | except TimeoutExpired: |
| 1179 | pass |
| 1180 | self._sigint_wait_secs = 0 # Note that this has been done. |
| 1181 | else: |
| 1182 | # Wait for the process to terminate, to avoid zombies. |
| 1183 | self.wait() |
| 1184 | |
| 1185 | def __del__(self, _maxsize=sys.maxsize, _warn=warnings.warn): |
| 1186 | if not self._child_created: |