From 505941259e4e7181a176e74b3de0b7d96c444e29 Mon Sep 17 00:00:00 2001 From: Romuald Brunet Date: Mon, 17 Sep 2018 11:44:50 -0400 Subject: [PATCH] Look for dict builtin in InstanceState cleanup Added a check within the weakref cleanup for the :class:`.InstanceState` object to check for the presence of the ``dict`` builtin, in an effort to reduce error messages generated when these cleanups occur during interpreter shutdown. Pull request courtesy Romuald Brunet. Change-Id: If27b94d50a32767de8b4147c09fa423f71596004 Pull-request: https://github.com/zzzeek/sqlalchemy/pull/472 --- doc/build/changelog/unreleased_12/pr472.rst | 7 +++++++ lib/sqlalchemy/orm/state.py | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 doc/build/changelog/unreleased_12/pr472.rst diff --git a/doc/build/changelog/unreleased_12/pr472.rst b/doc/build/changelog/unreleased_12/pr472.rst new file mode 100644 index 0000000000..baa5741260 --- /dev/null +++ b/doc/build/changelog/unreleased_12/pr472.rst @@ -0,0 +1,7 @@ +.. change:: + :tags: bug, orm + + Added a check within the weakref cleanup for the :class:`.InstanceState` + object to check for the presence of the ``dict`` builtin, in an effort to + reduce error messages generated when these cleanups occur during interpreter + shutdown. Pull request courtesy Romuald Brunet. diff --git a/lib/sqlalchemy/orm/state.py b/lib/sqlalchemy/orm/state.py index 70420ca509..03d68ab820 100644 --- a/lib/sqlalchemy/orm/state.py +++ b/lib/sqlalchemy/orm/state.py @@ -369,6 +369,13 @@ class InstanceState(interfaces.InspectionAttr): Will not work otherwise! """ + + # Python builtins become undefined during interpreter shutdown. + # Guard against exceptions during this phase, as the method cannot + # proceed in any case if builtins have been undefined. + if dict is None: + return + instance_dict = self._instance_dict() if instance_dict is not None: instance_dict._fast_discard(self) -- 2.47.2