xflr6 / graphviz

Simple Python interface for Graphviz
https://graphviz.readthedocs.io
MIT License
1.63k stars 211 forks source link

file render does not cleanup source file when backend render has error #136

Closed gdub closed 3 years ago

gdub commented 3 years ago

File.render(..., cleanup=True) does not cleanup the written source file when backend.render() raises an exception, e.g. if graphviz executables not installed.

xflr6 commented 3 years ago

Thanks. I think it is expected that the cleanup=True deletion of intermediate files is only executed if the rendering was successful. E.g. the first thing one might try if there is an error about the DOT source is to dot the .gv-file on the command-line. I think it's better if we adapt the documentation for the argument to clarify this is for successful rendering, WDYT?

https://github.com/xflr6/graphviz/blob/bb1af2e4eb0a3f47ae6193b1aa3dae319ec7c2bf/graphviz/files.py#L211

xflr6 commented 3 years ago

Closed via 6e9774b578d86e8048bec90db6c745cedbaa768b (clarify clean=True requires successful rendering).

gdub commented 3 years ago

@xflr6, in my case I was using graphviz via another library, pytransitions. IMHO, it's a bit rude to leave behind unexpected files on a system, especially when explicitly asking to cleanup. Indirect users of the library have no idea about this behavior.

Whether or not the existing behavior of leaving file around on error is kept, perhaps would be good to create the temporary file using Python's built-in tempfile library. If the file is kept, it at least would be in the system's default tempfile directory rather than polluting code/output directories.