(
reader,
suffix='',
# gh-93353: Keep a reference to call os.remove() in late Python
# finalization.
*,
_os_remove=os.remove,
)
| 120 | |
| 121 | @contextlib.contextmanager |
| 122 | def _tempfile( |
| 123 | reader, |
| 124 | suffix='', |
| 125 | # gh-93353: Keep a reference to call os.remove() in late Python |
| 126 | # finalization. |
| 127 | *, |
| 128 | _os_remove=os.remove, |
| 129 | ): |
| 130 | # Not using tempfile.NamedTemporaryFile as it leads to deeper 'try' |
| 131 | # blocks due to the need to close the temporary file to work on Windows |
| 132 | # properly. |
| 133 | fd, raw_path = tempfile.mkstemp(suffix=suffix) |
| 134 | try: |
| 135 | try: |
| 136 | os.write(fd, reader()) |
| 137 | finally: |
| 138 | os.close(fd) |
| 139 | del reader |
| 140 | yield pathlib.Path(raw_path) |
| 141 | finally: |
| 142 | try: |
| 143 | _os_remove(raw_path) |
| 144 | except FileNotFoundError: |
| 145 | pass |
| 146 | |
| 147 | |
| 148 | def _temp_file(path): |
no test coverage detected