From 9a6c514369318759d63e8612d222db61eac57cab Mon Sep 17 00:00:00 2001 From: "H.Shay" Date: Thu, 23 Sep 2021 10:41:30 -0700 Subject: [PATCH 1/2] add logic to catch attribute error on frozendict --- canonicaljson.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/canonicaljson.py b/canonicaljson.py index 8dee682..85e7e69 100644 --- a/canonicaljson.py +++ b/canonicaljson.py @@ -26,7 +26,10 @@ def _default(obj): if type(obj) is frozendict: # fishing the protected dict out of the object is a bit nasty, # but we don't really want the overhead of copying the dict. - return obj._dict + try: + return obj._dict + except AttributeError: + return dict(obj) raise TypeError( "Object of type %s is not JSON serializable" % obj.__class__.__name__ ) From 30adfb6d694834545e92021fe905de678db091d7 Mon Sep 17 00:00:00 2001 From: Hillery Shay Date: Fri, 24 Sep 2021 07:50:41 -0700 Subject: [PATCH 2/2] add comment Co-authored-by: reivilibre --- canonicaljson.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/canonicaljson.py b/canonicaljson.py index 85e7e69..5a4c2ef 100644 --- a/canonicaljson.py +++ b/canonicaljson.py @@ -29,6 +29,9 @@ def _default(obj): try: return obj._dict except AttributeError: + # When the C implementation of frozendict is used, + # there isn't a `_dict` attribute with a dict + # so we resort to making a copy of the frozendict return dict(obj) raise TypeError( "Object of type %s is not JSON serializable" % obj.__class__.__name__