|
50 | 50 | import sys |
51 | 51 | import threading |
52 | 52 | import warnings |
| 53 | +import contextlib |
53 | 54 | from time import monotonic as _time |
54 | 55 |
|
55 | 56 |
|
@@ -1072,28 +1073,28 @@ def _close_pipe_fds(self, |
1072 | 1073 | # self._devnull is not always defined. |
1073 | 1074 | devnull_fd = getattr(self, '_devnull', None) |
1074 | 1075 |
|
1075 | | - if _mswindows: |
1076 | | - if p2cread != -1: |
1077 | | - p2cread.Close() |
1078 | | - if c2pwrite != -1: |
1079 | | - c2pwrite.Close() |
1080 | | - if errwrite != -1: |
1081 | | - errwrite.Close() |
1082 | | - else: |
1083 | | - if p2cread != -1 and p2cwrite != -1 and p2cread != devnull_fd: |
1084 | | - os.close(p2cread) |
1085 | | - if c2pwrite != -1 and c2pread != -1 and c2pwrite != devnull_fd: |
1086 | | - os.close(c2pwrite) |
1087 | | - if errwrite != -1 and errread != -1 and errwrite != devnull_fd: |
1088 | | - os.close(errwrite) |
| 1076 | + with contextlib.ExitStack() as stack: |
| 1077 | + if _mswindows: |
| 1078 | + if p2cread != -1: |
| 1079 | + stack.callback(p2cread.Close) |
| 1080 | + if c2pwrite != -1: |
| 1081 | + stack.callback(c2pwrite.Close) |
| 1082 | + if errwrite != -1: |
| 1083 | + stack.callback(errwrite.Close) |
| 1084 | + else: |
| 1085 | + if p2cread != -1 and p2cwrite != -1 and p2cread != devnull_fd: |
| 1086 | + stack.callback(os.close, p2cread) |
| 1087 | + if c2pwrite != -1 and c2pread != -1 and c2pwrite != devnull_fd: |
| 1088 | + stack.callback(os.close, c2pwrite) |
| 1089 | + if errwrite != -1 and errread != -1 and errwrite != devnull_fd: |
| 1090 | + stack.callback(os.close, errwrite) |
1089 | 1091 |
|
1090 | | - if devnull_fd is not None: |
1091 | | - os.close(devnull_fd) |
| 1092 | + if devnull_fd is not None: |
| 1093 | + stack.callback(os.close, devnull_fd) |
1092 | 1094 |
|
1093 | 1095 | # Prevent a double close of these handles/fds from __init__ on error. |
1094 | 1096 | self._closed_child_pipe_fds = True |
1095 | 1097 |
|
1096 | | - |
1097 | 1098 | if _mswindows: |
1098 | 1099 | # |
1099 | 1100 | # Windows methods |
|
0 commit comments