Return an object representing a RAII-like access to a temp file. The the result is a context manager object that can be used in a 'with' statement: 'with TempFiles.get_file(..) as filename:'. The file will be deleted immediately once the 'with' block is exited.
(self, suffix)
| 28 | return named_file |
| 29 | |
| 30 | def get_file(self, suffix): |
| 31 | """Return an object representing a RAII-like access to a temp file. |
| 32 | |
| 33 | The the result is a context manager object that can be used in a 'with' statement: |
| 34 | |
| 35 | 'with TempFiles.get_file(..) as filename:'. |
| 36 | |
| 37 | The file will be deleted immediately once the 'with' block is exited. |
| 38 | """ |
| 39 | class TempFileObject: |
| 40 | def __enter__(self_): # noqa: DC02 |
| 41 | self_.file = tempfile.NamedTemporaryFile(dir=self.tmpdir, suffix=suffix, delete=False) |
| 42 | self_.file.close() # NamedTemporaryFile passes out open file handles, but callers prefer filenames (and open their own handles manually if needed) |
| 43 | return self_.file.name |
| 44 | |
| 45 | def __exit__(self_, _type, _value, _traceback): # noqa: DC02 |
| 46 | if not self.save_debug_files: |
| 47 | utils.delete_file(self_.file.name) |
| 48 | return TempFileObject() |
| 49 | |
| 50 | def get_dir(self): |
| 51 | """Return a named temp directory with the given prefix.""" |
no test coverage detected