From bfc2ade9b3784469a5f7b80119fbd1679b6268c8 Mon Sep 17 00:00:00 2001 From: Adrian Tschira Date: Sun, 15 Apr 2018 17:24:25 +0200 Subject: [PATCH 1/2] Make event properties raise AttributeError instead They raised KeyError before. I'm changing this because the code uses hasattr() to check for the presence of a key. This worked accidentally before, because hasattr() silences all exceptions in python 2. However, in python3, this isn't the case anymore. I had a look around to see if anything depended on this raising a KeyError and I couldn't find anything. Of course, I could have simply missed it. Signed-off-by: Adrian Tschira --- synapse/events/__init__.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/synapse/events/__init__.py b/synapse/events/__init__.py index e673e96cc..d4d1b92f7 100644 --- a/synapse/events/__init__.py +++ b/synapse/events/__init__.py @@ -48,13 +48,22 @@ class _EventInternalMetadata(object): def _event_dict_property(key): def getter(self): - return self._event_dict[key] + try: + return self._event_dict[key] + except KeyError: + raise AttributeError(key) def setter(self, v): - self._event_dict[key] = v + try: + self._event_dict[key] = v + except KeyError: + raise AttributeError(key) def delete(self): - del self._event_dict[key] + try: + del self._event_dict[key] + except KeyError: + raise AttributeError(key) return property( getter, From 0c9db26260210bd2066048333b2644a2511b1801 Mon Sep 17 00:00:00 2001 From: Adrian Tschira Date: Mon, 30 Apr 2018 09:49:10 +0200 Subject: [PATCH 2/2] add comment explaining attributeerror --- synapse/events/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/synapse/events/__init__.py b/synapse/events/__init__.py index d4d1b92f7..c3ff85c49 100644 --- a/synapse/events/__init__.py +++ b/synapse/events/__init__.py @@ -47,6 +47,9 @@ class _EventInternalMetadata(object): def _event_dict_property(key): + # We want to be able to use hasattr with the event dict properties. + # However, (on python3) hasattr expects AttributeError to be raised. Hence, + # we need to transform the KeyError into an AttributeError def getter(self): try: return self._event_dict[key]