From 7e7a3dba5fd4262269f713dfe21ba7e4746fc2dd Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 10 Apr 2015 13:24:41 +0300 Subject: Issue #23865: close() methods in multiple modules now are idempotent and more robust at shutdown. If needs to release multiple resources, they are released even if errors are occured. --- Lib/shelve.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'Lib/shelve.py') diff --git a/Lib/shelve.py b/Lib/shelve.py index cef580e5cd..581baf1e6f 100644 --- a/Lib/shelve.py +++ b/Lib/shelve.py @@ -138,17 +138,21 @@ class Shelf(collections.MutableMapping): self.close() def close(self): - self.sync() - try: - self.dict.close() - except AttributeError: - pass - # Catch errors that may happen when close is called from __del__ - # because CPython is in interpreter shutdown. + if self.dict is None: + return try: - self.dict = _ClosedDict() - except (NameError, TypeError): - self.dict = None + self.sync() + try: + self.dict.close() + except AttributeError: + pass + finally: + # Catch errors that may happen when close is called from __del__ + # because CPython is in interpreter shutdown. + try: + self.dict = _ClosedDict() + except: + self.dict = None def __del__(self): if not hasattr(self, 'writeback'): -- cgit v1.2.1