(
reader,
suffix='',
# gh-93353: Keep a reference to call os.remove() in late Python
# finalization.
*,
_os_remove=os.remove,
)
| 86 | |
| 87 | @contextlib.contextmanager |
| 88 | def _tempfile( |
| 89 | reader, |
| 90 | suffix='', |
| 91 | # gh-93353: Keep a reference to call os.remove() in late Python |
| 92 | # finalization. |
| 93 | *, |
| 94 | _os_remove=os.remove, |
| 95 | ): |
| 96 | # Not using tempfile.NamedTemporaryFile as it leads to deeper 'try' |
| 97 | # blocks due to the need to close the temporary file to work on Windows |
| 98 | # properly. |
| 99 | fd, raw_path = tempfile.mkstemp(suffix=suffix) |
| 100 | try: |
| 101 | try: |
| 102 | os.write(fd, reader()) |
| 103 | finally: |
| 104 | os.close(fd) |
| 105 | del reader |
| 106 | yield pathlib.Path(raw_path) |
| 107 | finally: |
| 108 | try: |
| 109 | _os_remove(raw_path) |
| 110 | except FileNotFoundError: |
| 111 | pass |
| 112 | |
| 113 | |
| 114 | def _temp_file(path): |
no test coverage detected
searching dependent graphs…