From b89da5dec2988d7de39efa840f7d3e75c0459080 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 13 Mar 2018 02:20:17 -0700 Subject: [PATCH] Add hasattr checks to prevent faulty __del__. --- etiquette/photodb.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/etiquette/photodb.py b/etiquette/photodb.py index 4f34130..65f5791 100644 --- a/etiquette/photodb.py +++ b/etiquette/photodb.py @@ -706,9 +706,15 @@ class PDBSQLMixin: self.savepoints = [] def close(self): - self.sql.close() - if self.ephemeral: - self.ephemeral_directory.cleanup() + # Wrapped in hasattr because if the object fails __init__, Python will + # still call __del__ and thus close(), even though the attributes + # we're trying to clean up never got set. + if hasattr(self, 'sql'): + self.sql.close() + + if hasattr(self, 'ephemeral'): + if self.ephemeral: + self.ephemeral_directory.cleanup() def commit(self): while len(self.on_commit_queue) > 0: