Resource not released (ARCH2080)
Ce contenu n’est pas encore disponible dans votre langue.
Key python.performance.disposable-not-disposed · Severity error · Category performance
When a resource-owning object is created without a with block, an error before cleanup leaves it unreleased and leaks it. Use a context manager.
Incorrect
Section titled “Incorrect”def write_report(path, data): lock = acquire_lock("report") f = open(path, "w") f.write(data) f.close() lock.release()Correct
Section titled “Correct”def write_report(path, data): with acquire_lock("report"), open(path, "w") as f: f.write(data)Wrap resource-owning objects in a with context manager so they are released on exit.