2 ms·
> it is very clear in this code that a reference to the file isn't leaked Is it? What if an exception is thrown? See e.g. this SO answer[1]: > However if an e
by rcthompson 4y ago
> it is very clear in this code that a reference to the file isn't leaked
Is it? What if an exception is thrown? See e.g. this SO answer[1]:
> However if an exception is thrown [...] then a reference to the file will be kept as part of the stack trace in the traceback object and the file will not be closed, at least until the next exception is thrown.
So your code, if called from somewhere else inside a try block, leaks a filehandle potentially indefinitely.
[1]: https://stackoverflow.com/a/2404671/125921 https://stackoverflow.com/a/2404671/125921