From 12d0ea062d92a15baee69c4be0e4ae00fcdee416 Mon Sep 17 00:00:00 2001 From: Jochen Sprickerhof Date: Sun, 16 May 2021 10:37:25 +0200 Subject: Check for cookie attribute before trying to delete it __del__ is called regardless of whether __init__ passed, so the cookie attribute may not exits. This can be seen with: python3 -c "import magic; magic.Magic(foo=None)" Also discussed in https://github.com/ahupp/python-magic/pull/222#issuecomment-675354824 --- magic/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/magic/__init__.py b/magic/__init__.py index 17456c1..363e88f 100644 --- a/magic/__init__.py +++ b/magic/__init__.py @@ -150,7 +150,7 @@ class Magic: # incorrect fix for a threading problem, however I'm leaving # it in because it's harmless and I'm slightly afraid to # remove it. - if self.cookie and magic_close: + if hasattr(self, 'cookie') and self.cookie and magic_close: magic_close(self.cookie) self.cookie = None -- cgit v1.2.1