From 15e75cb4de1487acbd03dfaa0df8c507f1ed4d9b Mon Sep 17 00:00:00 2001 From: Michael Chisholm Date: Thu, 28 May 2020 18:08:47 -0400 Subject: [PATCH] Python 2 compatibility fix in versionability detection. In python2, Mapping.keys() returns a list instead of a set! --- stix2/versioning.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/stix2/versioning.py b/stix2/versioning.py index b94babe..33b1c59 100644 --- a/stix2/versioning.py +++ b/stix2/versioning.py @@ -1,6 +1,7 @@ import copy import datetime as dt +import six from six.moves.collections_abc import Mapping import stix2.base @@ -86,9 +87,18 @@ def _is_versionable(data): # Then, determine versionability. + if six.PY2: + # dumb python2 compatibility: map.keys() returns a list, not a set! + # six.viewkeys() compatibility function uses dict.viewkeys() on + # python2, which is not a Mapping mixin method, so that doesn't + # work either (for our stix2 objects). + keys = set(data) + else: + keys = data.keys() + # This should be sufficient for STIX objects; maybe we get lucky with # dicts here but probably not. - if data.keys() >= _VERSIONING_PROPERTIES: + if keys >= _VERSIONING_PROPERTIES: is_versionable = True # Tougher to handle dicts. We need to consider STIX version, map to a